value-validator

2.2.2 • Public • Published

Build Status

value-validator

Low-level rule manager for validating values.

Install

$ npm install value-validator --save

Usage

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.')`
})

new Validator(rule, options)

  • 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.

.validate(value [, callback])

  • 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(context)

  • context Object specify this object for all validator functions.

Returns this

Sync Function-type rule

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

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.

const validator = new Validator(v => {
  if (> 10) {
    return true
  }
 
  return new Error('should larger than 10')
})
validator.validate(5).catch(err => {
  err // new Error('should larger than 10')
})

Async validator

To define an asynchronous validator, just returns a Promise.

Validator.defaults({preset=, codec=})

Pre-defines certain option of Validator and returns a constructor.

Validator presets are an abbreviation of a certain validation, or a set of validations.

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')

License

MIT

Package Sidebar

Install

npm i value-validator

Weekly Downloads

1

Version

2.2.2

License

MIT

Last publish

Collaborators

  • kael