supervalidation
Easy validation for your forms. Based on Laravel 3.x validation module
Installation
npm install --save supervalidation
Usage
The response could or could not be a promise. If you provide only synchronous methods (which be default all rule definitions is), there will be no promise at the end, but if you do, you will totally need to deal with a promise.
Example 1
var validator = name: 'Victor Queiroz' email: 'youremail@domain.com' email: 'string|required|email'; ifvalidator // do some work else if validator // do more work
Example 2
var Q = ; /* There is no limit for parameters in your rule definition */ Validator;
controller.js
moduleexports = { var rules = email: 'required|unique:users,email' ; var validator = reqbody rules templatePath: 'my-custom-language-variables.js' ; validator; };
When you fire err.getMessages()
it will return a bunch of messages which by default are in english, but you can change at any time your template.
Example 3 (validating complex objects)
var validator = address: streetNumber: 1234 'address.streetNumber': 'number|required'; ifvalidator return resstatus400; // do the work
Changing your language variables
All the messages are consumed from a file, which by default are this. It just return a big object (just like Grunt), which symbolizes each RULE that is defined.
moduleexports = min: numeric: 'The :attribute must be at least :min.' file: 'The :attribute must be at least :min kilobytes.' string: 'The :attribute must be at least :min characters.' array: 'The :attribute must have at least :min items.'
If the rule is an object and not a string, it will make a check through the type of the value typed in the field, and match with the right one. And this can be used with any rule.
:attribute
will always be replaced by the name of the attribute, and :min
in this case will be replaced by the first argument of the rule usage, and you can use this for any rule (expect for those who has no parameters, like required
)
moduleexports = myCustomRule: 'The :attribute must have at least :myCustomRule items.'
Changing your language variables template
Example 1 (Globally)
var path = ;var Translator = ;Translator;
Example 2 (Privately)
var validator = reqbody email: 'required|unique:users,email' templatePath: 'my-custom-language-variables.js';
Predefined rule definitions
- required
- max:length
- min:length
- string
- number
- url
(Soon more)