easy-validator
v0.2.0
Published
Simple object validator for NodeJS
Downloads
1
Readme
easy-validator
Simple object validator for NodeJS (as Symfony Annotation validation system)
Status
Installation
$ npm install easy-validator
To run the unit tests with mocha from your project :
npm test
Validating Objects
Sample of simple validating a not null string from an object :
var _ = require('underscore');
var eValidator = require('easy-validator');
//Creating sample object
var objectToValidate = {
name: "Foo",
email: "[email protected]",
age: 33
};
//Create the constraint collection (The name property should pass the 2 validations , NotNull and String type
var constraintCollection = eValidator.Assert({
"name": ['@Assert:NotNull()', '@Assert:Type("string")'], // <-- Array of asserts for each property required to validate
"email": ['@Assert:Email()'],
"age": ['@Assert:Range(min=18,max=99)']
});
//Validate the object, and retrieve the list of errors
violationList = constraintCollection.perform.validate(objectToValidate);
//You can see all errors on the violationsList (array)
_.each(violationList, function(violation){
console.log("Property ["+violation.propertyPath+"] Error ["+violation.message+"]");
});
The same validation can be achieved wit object definition, instead string definition :
//String definition of the validators
var constraintCollection = eValidator.Assert({
"name": ['@Assert:NotNull()', '@Assert:Type("string")'], // <-- Array of asserts for each property required to validate
"email": ['@Assert:Email()'],
"age": ['@Assert:Range(min=18,max=99)']
});
//The same object defined with object by each property
var constraintCollection = eValidator.Assert({
"name": {"assert":'NotNull'},
"email": {"assert": 'Email'},
"age": {"assert": 'Range', "min":18, "max":99}
});
List of Asserts
@Assert:Email() For string values
Forces a value to be a valid email
Sample :
var constraintCollection = eValidator.Assert({
email_address: ['@Assert:Email()']
})
No configuration
@Assert:NotNull()
Forces a value to be not null value, and to be defined. Will add an error to violation list if null or undefined
No configuration
@Assert:Null()
Forces a value to be null value, or be undefined. Will add an error to violation list if not null or defined
No configuration
@Assert:Range() For numeric values
Forces a value to be defined in a range of numbers. Should be between min and max configurations if defined. Both configuration 'min' and 'max' are optional configurations.
Sample :
var constraintCollection = eValidator.Assert({
name: ['@Assert:Range(min=18,max=99)'] //Should be between 18 and 99 the value to be matched
})
@Assert:Type()
Forces a value to be null value, or be undefined. Will add an error to violation list if not null or defined
Allowed configuration of type (default configured to 'string')
Sample for array type
var constraintCollection = eValidator.Assert({
name: ['@Assert:Type("array")']
})
Allowed types :
- string
- array
- object
- boolean
- numeric
TODO
Keep implementing Asserts implemented on Symfony2 current validation system
http://symfony.com/doc/current/book/validation.html