value-validator
Version:
Low-level rule manager for validating values.
154 lines (114 loc) • 3.95 kB
Markdown
[](https://travis-ci.org/kaelzhang/value-validator)
<!-- optional appveyor tst
[](https://ci.appveyor.com/project/kaelzhang/value-validator)
-->
<!-- optional npm version
[](http://badge.fury.io/js/value-validator)
-->
<!-- optional npm downloads
[](https://www.npmjs.org/package/value-validator)
-->
<!-- optional dependency status
[](https://david-dm.org/kaelzhang/value-validator)
-->
Low-level rule manager for validating values.
```sh
$ npm install value-validator --save
```
```js
const Validator = require('value-validator')
const validator = new Validator([
/.{4,}/,
/^[a-z0-9_]+$/i,
function (v) {
return checkExistsPromise(v)
}
])
validator
.valiate(value)
.then((result) => {
result // whether passes the validation
})
.catch((err) => {
err // if any error throws or returns
})
// Examples
validator.validate('foo').then(pass => {
pass // false, to short
})
validator.validate('foo.bar').then(pass => {
pass // false, only letters, numbers and underscores.
})
validator.validate('steve').catch(err => {
err // maybe `new Error('username "steve" already exists.')`
})
```
- **rule** `RegExp|function()|String|Array.<mixed>` rule could be
- regular expression,
- function either returns a `Promise` or normal value
- string (validator preset),
- or an array of mixed-type of the former three.
- **value** `any` value to be validated
- **callback** `function(err, pass)=` using callback is deprecated since `2.2.0`, and the parameter will be removed in the next major version.
returns a `Promise` if no `callback`, or `undefined`
- **context** `Object` specify this object for all validator functions.
Returns `this`
The function should accept only one argument, which is the value to be validated.
If the function returns a `Boolean`, it indicates whether the validation is passed, and the `err` will be `null`
```js
const validator = new Validator(v => v > 10)
validator.validate(5).then(pass => {
pass // false
})
```
If the function returns an `Error`, it means the validation fails, and the error will passed to the callback function of `validate(v, callback)` as the first parameter `err`.
```js
const validator = new Validator(v => {
if (v > 10) {
return true
}
return new Error('should larger than 10')
})
validator.validate(5).catch(err => {
err // new Error('should larger than 10')
})
```
To define an asynchronous validator, just returns a [`Promise`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise).
Pre-defines certain option of `Validator` and returns a constructor.
Validator presets are an abbreviation of a certain validation, or a set of validations.
```js
const presets = {
// To define a function-typed preset
unique: function (v) {
return new Promise((resolve, reject) => {
asyncCheckExists(v, exists => {
if (exists) {
return reject(new Error(`username "${v}" already exists.`))
}
resolve(true)
})
})
},
min4: /.{4,}/,
// A preset could be a set of presets.
username: [
'min4',
/^[a-z0-9_]+$/i,
'unique'
]
}
const MyValidator = Validator.defaults({presets})
// Then we could use `username` as the test rule.
const validator = new MyValidator('username')
```
MIT