It would be great if one could define a validation schema that is passed to a function that validates one or more objects.
_i.e:_ validator.validate(schema, ...objectsToValidate)
A validation schema would look like this:
const schema = {
username: {
isAlphanumeric: true, // checks username is alphanumeric with default locale and default error message
isLength: {
options: [{min: 3, max: 33}], // options passed to isLength
message: 'Username should be 3 to 33 characters long' // display this message if isLength returns false
},
}
email: {
optional: true, // if email isEmpty, just ignore it == dont return false
isEmail: {
options: [{ allow_display_name: true}]
}
}
}
express-validator uses this. I was hoping it could be directly integrated into this library.
I've previously said no to such requests, saying that it was outside the scope of the library, however I'm warming up to the idea. It seems like people end up implementing their own scheme which is error prone.
That's the exact reason why I suggested it.I thought it would be better if it was supported in this library, which a lot of other libraries depend on.
This would put this library one giant step closer to being easily implemented both client and server side, too, as validation schemas could be easily passed and applied.
As we desperately needed something like this half a year ago, I did exactly this plus added additional features in the json-inspecotor library.
I'd appreciate if I could hear from you whether the library meets your expectation in conjunction with this feature request.
Edit: As I think more about it, @IOAyman probably means a lot more lightweight functionality, like a dynamic hash table with mappings of individual validators and their options?
Most helpful comment
I've previously said no to such requests, saying that it was outside the scope of the library, however I'm warming up to the idea. It seems like people end up implementing their own scheme which is error prone.