Validator for recursive JS structures
var validator = require('vstack-validator');
var constraints = validator.constraints;
var authorValidator = constraints.object({
mapping: {
name: constraints.all({
validators: {
notEmpty: constraints.notEmpty({
message: 'Name should not be empty'
}),
minLength: constraints.minLength({
minLength: 3,
message: 'Name is too short'
})
}
})
}
});
var authorsValidator = constraints.array({
itemValidator: authorValidator
});
var postValidator = constraints.object({
mapping: {
name: constraints.notEmpty(),
authors: constraints.all({
validators: {
minLength: constraints.minLength({
minLength: 1,
message: 'Post should have authors'
}),
authors: authorsValidator
}
})
}
});
var validity = postValidator({
name: '',
authors: []
});
console.log(validity);
/*
{
name: {
valid: true,
constraint: 'notEmpty',
invalid: false,
message: null
},
authors: {
minLength: {
valid: true,
constraint: 'minLength',
invalid: false,
message: null
},
authors: {
0: {
name: {
notEmpty: {
valid: true,
constraint: 'notEmpty',
invalid: false,
message: null
},
minLength: {
valid: true,
constraint: 'minLength',
invalid: false,
message: null
},
valid: true,
constraint: 'all',
invalid: false,
message: null
},
valid: true,
constraint: 'object',
invalid: false,
message: null
},
1: {
name: {
notEmpty: {
valid: true,
constraint: 'notEmpty',
invalid: false,
message: null
},
minLength: {
valid: false,
constraint: 'minLength',
invalid: true,
message: 'Name is too short'
},
valid: false,
constraint: 'all',
invalid: true,
message: 'Name is too short'
},
valid: false,
constraint: 'object',
invalid: true,
message: 'Object is invalid'
},
length: 2,
valid: false,
constraint: 'array',
invalid: true,
message: 'Some items are invalid'
},
valid: false,
constraint: 'all',
invalid: true,
message: 'Some items are invalid'
},
valid: false,
constraint: 'object',
invalid: true,
message: 'Object is invalid'
}
*/
// Constraint is validator factory with configurable specification.
var minLength = validator.createConstraint({
// Each constraint should have name.
name: 'minLength',
// Error message could contain properties from specification.
message: 'Value length should be greather than {{minLength}}',
// Specification are extendable.
minLength: 1,
// First argument is value.
// Second argument is specification.
// Third argument is value proxied from top called validator.
validator: function(value, spec, root) {
return validator.utilities.isNull(value) || value.length >= spec.minLength;
}
});
validator.constraints.notNull();
validator.constraints.notEmpty();
validator.constraints.isTrue();
validator.constraints.isFalse();
validator.constraints.isNumber();
validator.constraints.isString();
validator.constraints.isBoolean();
validator.constraints.isObject();
validator.constraints.isArray();
validator.constraints.isRegexp();
validator.constraints.isDate();
validator.constraints.isMoment();
validator.constraints.regexp({
regexp: /./
});
validator.constraints.email();
validator.constraints.uppercase();
validator.constraints.lowercase();
validator.constraints.minLength({
minLength: 1
});
validator.constraints.maxLength({
maxLength: 255
});
validator.constraints.all({
validators: [],
joinMessages: true
});
validator.constraints.object({
mapping: {},
joinMessages: true,
overrideRoot: false
});
validator.constraints.array({
itemValidator: validator.constraints.notNull()
});