My proposal:
each schema can have a validators property which is an array of validator objects. A validator object have the following properties:
message(string) the error message shown if validation fails
fn(function) validation function that can return boolean, accept the user input and a callback or returns promise that resolve to boolean
The validation functions will get executed with the array order and if all validations passed the entry is valid. The validator starts with leafs of the Schema tree and crawls up to the root of the schema.
Example
{
type: "object",
"properties": {
"email": {
"type": "email",
"validators": [
{
message: 'You were unlucky!',
fn: function(){
return Math.random() > 0.4; // :D
}
}
]
},
"password": {
"type": "password",
"validators": [
{
message: 'Password can not have letter M in it!',
fn: function(password) {
return password.indexOf('M') === -1; // ?!?
}
]
}
},
"validators": [{
message: 'email and password should not be equal',
fn: function(obj) {
return obj.password != obj.email;
}
}]
}
My proposal:
each schema can have a
validatorsproperty which is an array of validator objects. A validator object have the following properties:message(string) the error message shown if validation failsfn(function) validation function that can return boolean, accept the user input and a callback or returns promise that resolve to booleanThe validation functions will get executed with the array order and if all validations passed the entry is valid. The validator starts with leafs of the Schema tree and crawls up to the root of the schema.
Example