A constructor function for data validation on both the server and client, inspired by the Laravel PHP framework. The API is very similar to that of the Laravel 3 Validator class.
Install the Validator package from the NPM registry https://npmjs.org/package/laravel-validator-for-js
npm install laravel-validator-for-js
var Validator = ;
Since version 0.4, the Validator constructor is immediately returned from the require call. I.e. You no longer have to do require('laravel-validator-for-js').Validator
The 1st argument to the constructor is an object that contains the data you want to validate.
The 2nd argument is an object that contains the validation rules.
var data =email: 'firstname.lastname@example.org';var rules =email: 'email';var validation = data rules;validation // truevalidation // false
To apply validation rules to the input object, use the same object key names for the rules object.
var rules =name: 'required|size:3'email: 'required|email';var data =name: ''email: '';var validation = data rules;validation; // true
Note: All minimum and maximum checks are inclusive.
This contructor will automatically generate error messages for validation rules that failed. You can use the first method to fetch the first error message of a failing attribute. You can access all of the errors through the errors property on the Validator instance.
There is also an errorCount property on the validation instance to specify the number of validation errors.
If callbackFn returns a truthy value, the validation will pass for this rule. Otherwise, this validation rule will fail. errorMessage is an optional string where you can specify a custom error message. :attribute inside errorMessage will be replaced with the attribute name.
Install node module dependencies
See SpecRunner.html for Jasmine tests in the browser.
You can also run the jasmine tests via Node.js once you've installed the NPM package jasmine-node.
jasmine-node spec/ --verbose --color//ORnpm test (which calls the above command)
Once the above test passes, run the following command which will in turn run JSHint and minify the source