https://github.com/nomocas/aright
Small and fast JS Object and Type validation.
https://github.com/nomocas/aright
Last synced: 3 months ago
JSON representation
Small and fast JS Object and Type validation.
- Host: GitHub
- URL: https://github.com/nomocas/aright
- Owner: nomocas
- License: mit
- Created: 2015-10-07T15:03:53.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-06-29T22:18:41.000Z (almost 9 years ago)
- Last Synced: 2024-04-27T10:08:12.933Z (about 1 year ago)
- Language: JavaScript
- Size: 85 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# aright
Small and really fast js objects and types validation (browser or server side).
Allow to describe validation rules with compact chained API.
Easy i18n.
Really fast because it doesn't need schema parsing and interpretation. Rules holds directly an array of simple functions that do the job as fast as possible.
Pure vanilla js, no dependencies.
Small size (4.0 Ko minified, 1.4 Ko min/gzip).
## Install
```shell
npm i aright
# or
bower i aright
# or
git clone https://github.com/nomocas/aright.git
```## Examples
```javascript
var v = aright.v;var rule = v()
.isObject()
.string('email', v().format('email').required(false))
.number('index', v().equal(24))
.bool('flag')
.array('collection',
v().item(
v().isString()
)
)
.object('child',
v().string('title')
)
.bool('test');rule.validate({
email:'[email protected]',
index:24,
flag:true,
collection:['hello'],
child:{
title:'hello'
},
test:true
});
// => return true
``````javascript
aright.rules.email.validate('abcdef'); // return error report
// equivalent to :
v().rule('email').validate('abcdef'); // return error report
``````javascript
v().isString().format(/abc/).minLength(6).validate('abcdef'); // return true
v().isString().enumerable(['bloupi', 'foo']).validate('bloup'); // return error report
```## Full API
### is* Family
.isString(), isNumber(), .isBool(), .isObject(), .isArray(), .isFunction(), .isNull()
```javascript
v().isNull().validate(null); //return true
```### .type('string', 'object', ...)
Sugar for .or(v().isString(), v().isObject(), ...)
### .instanceOf(Class)
```javascript
var rule = v().instanceOf(Date),
result = rule.validate(new Date()); // return true
```### properties validation
.bool(propName, rule)
.number(propName, rule)
.string(propName, rule)
.func(propName, rule)
.null(propName, rule)
.object(propName, rule)
.array(propName, rule)```javascript
v().string('title', v().required())
.bool('published', v().equal(false))
.number('count')
.validate({
title:'hello world',
published:false,
count:12
}); //return true
```### value constraints
.required(false), .minLength(5), .maxLength(3), .minimum(7), .maximum(9), .enumerable(['foo', 12]), .equal('my value')
Any value is required by default. Only undefined will be seen as missing.
```javascript
v().required(false).validate(undefined); // return truev().equal(12).validate(1); // return error report
//...
```#### value format
Validate value against regExp```javascript
v().format(/abc/gi).validate('abc'); // return true
```To define custom format :
```javascript
aright.formats.myFormat = /abc/gi;
v().format('myFormat').validate('abc'); // return true
```As predefined format there is only email for the moment...
```javascript
v().format('email').validate('[email protected]'); // return true
```### array and items
Both work together :```javascript
var o = {
collection:['foo', 'bar', 'zoo']
};v().isObject()
.array('collection',
v().item(
v().isString()
)
)
.validate(o); // return true
```### .not and .or
```javascript
var rule = v().or(v().isString(), v().isNumber()),
result = rule.validate('[email protected]') && rule.validate(1); // return true
``````javascript
var rule = v().not(v().isString(), v().isNumber()),
result = rule.validate([]) && rule.validate(true); // return true
```### validation
Any value could be validated by calling .validate( valueToTest ) on any aright rule.
It returns true if rule is satisfied or an error report as follow if something fails :
```javascript
var rule = v()
.isObject()
.string('email', v().format('email').required(false))
.number('index', v().equal(24))
.bool('flag')
.array('collection',
v().item(
v().isString()
)
)
.object('child',
v().string('title')
)
.bool('test');var result = rule.validate({
email: 'aaa@bbb',
index: 1,
flag: 'hello',
collection: [1],
child: null,
test: 3
});/* result == {
"valid": false,
"map": {
"email": {
"value": "aaa@bbb",
"errors": [
"format failed"
]
},
"index": {
"value": 1,
"errors": [
"equality failed (should be : 24)"
]
},
"flag": {
"value": "hello",
"errors": [
"should be a boolean"
]
},
"collection.0": {
"value": 1,
"errors": [
"should be a string"
]
},
"child.title": {
"value": null,
"errors": [
"missing property"
]
},
"test": {
"value": 3,
"errors": [
"should be a boolean"
]
}
},
"value": {
"email": "aaa@bbb",
"index": 1,
"flag": "hello",
"collection": [
1
],
"child": null,
"test": 3
}
}*/
```### custom rule
To define custom rules :
```javascript
aright.rules.myRule = v().isString().required();
v().rule('myRule').validate('hello'); // return true
```### custom 'this' handler
```javascript
// handler that act on 'this' (as is* family)
aright.Validator.prototype.myRule = function(){
return this.enqueue('this', function(input, path){
// input is the value to test, and path is its path from root object
if(input ...){
//...
return true;
}
else
return aright.error(this, 'myRule', null, null, path, 'string' /* what it should be */)
});
};v().myRule().validate(...);
```### custom property handler
```javascript
// handler that act on choosen property
aright.Validator.prototype.myOtherRule = function(propertyName){
return this.enqueue(propertyName, function(input, path){
// input is the value to test, and path is its path from root object
if(input[propertyName] ...){
//...
return true;
}
else
return aright.error(this, 'myOtherRule', input, propertyName, path, 'string' /* what it should be */)
});
};v().myRule().validate(...);
```## i18n
Take a look to `aright/i18n/fr.js` to have an idea on how customise
```javascript
aright.i18n.data.fr = require('aright/i18n/fr');
aright.i18n.currentLanguage = 'fr';
// aright errors messages will now be in french
```## Tests
### Under nodejs
You need to have mocha installed globally before launching test.
```
> npm install -g mocha
```
Do not forget to install dev-dependencies. i.e. : from 'aright' folder, type :
```
> npm install
```then, always in 'aright' folder simply enter :
```
> mocha
```## Licence
The [MIT](http://opensource.org/licenses/MIT) License
Copyright (c) 2015 Gilles Coomans
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.