I am very sorry to announce that I will no longer maintain this project, please refer to other similar projects
A Laravel styled JavaScript Object/Form/JSON validation library. Laravel Validation
- Support mixin validation rules
- Support recursive validation on complicate objects
- Support user-defined validator
validator.js is easy to use in form or JSON validation, and it is extensible.
var example = {
text: 'Hello world!',
date: '2015-07-07',
attachment: {
name: 'note1',
count: 1,
},
comments: null,
},
rules = {
text: 'required|string',
date: 'date|date_format:yyyy-MM-dd',
attachment: {
name: 'required|string',
content: 'integer',
},
comments: 'array',
};
console.log(Validator.validate(example, rules));
// => {status: 'failed', [{object: [Object], field: "comments", rule: "array"}]}
<script type="text/javascript" src="./validator.js"></script>
Or
<script type="text/javascript" src="./dist/validator.min.js"></script>
npm install js-validator --save
var validator = require('js-validator');
requirejs(["./validator"], function(validator) {
...
});
define(function (require, exports, module) {
var validator = require('./validator');
...
});
You can have different rules for a single field, using |
as the separator.
var rules = {
text: 'required|string',
date: 'date|date_format:yyyy-MM-dd',
comments: 'integer',
};
About string escape
When '|', ':' or ',' has to be in your rule's values, please add '\\' in front of them, just like this:
var person = {
nickname: 'Harry|Poter'
},
rules = {
nickname: 'in:Harry\\|Potter,Hermione\\:Granger,Ron\\,Weasley'
}
// Validator.validate if you are using native JavaScript code
validator.validate(object_to_be_tested, rules);
Result
Return an object with 'status' and 'rejects' properties.
If the validated object meets all the rules, the 'status' property will be 'success' and the 'rejects' array will be empty; otherwise 'status' will be 'failed' and 'rejects' will contain details that causes the failure.
You can use add() Function to add a validator, along with a name as its first argument and a validation method as second argument. Validation method can either be RegExp object or Function. When it's a Function, its first argument is the object to be tested, the second argument is the value of current validating field, and it should return true
when the validation succeeded.
// Validator.add if you are using native JavaScript code
validator.add('older_than', function (object, value, age) {
return value > age;
});
var rules = {
age: 'integer|older_than:17',
};
// Validator.setConfig if you are using native JavaScript code
validator.setConfig({...});
Default: false
Whether the validation continues when it failed on any rule.
The field under validation must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance.
The field under validation must be a value after a given date.
The field under validation must be entirely alphabetic characters.
The field under validation must be entirely alphabetic characters.
The field under validation must be entirely alpha-numeric characters.
The field under validation must be of type array.
The field under validation must be a value preceding the given date.
The field under validation must have a size between the given min and max. Strings, numerics and files(FileList/File) are evaluated. Files are evaluated in kilobytes.