custom messages on validations (and validations in general)
Validations seem to be a little lacking in thinky, I have a few questions/thoughts/proposals:
-
make validation messages more consistent Currently we just get a string like "One or more validations failed, [thisthing] isn't allowed to be something", which I have to regex all around in a non-consistent way to give more meaningful results to the user of my api.
proposal amend ValidationError to include a fields/properties/attributes array member containing the name of the thing that failed validation, as well as the message for why it failed validation (including, potentially, a custom message)
-
validations seem to fail at the first failed validation This is pretty annoying from a user perspective as well. If a user has screwed up a number of a fields, having to submit multiple requests to figure out everything that's gone awry is wasteful and inconvenient
proposal not really sure about this one, as there seem to be a mix of validators that just return a boolean vs validators that throw an error (which probably stop the validation checks).
-
custom validation messages Currently, there is no way to easily specify a custom validation message for e.g. validators provided by validate.js. In order to add your own custom message, one needs to throw a ValidationError manually. There is also no way to override the validation messages of existing builtin validators.
-
lastly, I propose that we straight up depend on validate.js a la sequelize, so that thinky can support more out-of-the box. I don't imagine the overhead of including that as a dependency would be very great, and certainly not greater than the convenience provided to the user
Thoughts?
- Hum yea there should be error code. I was kind of hoping that RethinkDB would push that for 2.1, but they pushed only new types. Thinking about it, we don't really need code, we could just use string and we won't have to worry about collisions later.
- Yep we could add an option. I was suggested in #228
- You mean for type error? Now the only way to provide special errors is to call
validator. - The main reason thinky doesn't use validate is similar to the reason why we don't use hapi/joy for schema. There are a few special cases that break everything.
Typically
r.now()is a valid date.
1 and 2 are important IMO, and they have been more or less always on top of my TODO list, but I somehow never had time to do it -_-
Note that 1 and 2 are kind of tricky because of possible asynchronous validations...