Skip to content

schematic/validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

validator

validator

Validates schemas with support for async callbacks, promises, and thunks This project is still in it's infancy, documentation and a stable API coming soon

Installation

Install with NPM:

$ npm install ilsken/validator

API

Validator([validators])

Creates a new validator, you may optionally pass an object with names/rules

#.rule(name, fn)

Adds a new rule and returns itself for chaining. There are 4 main types of rules

Plain Function

v.rule('min', function(value, min_value) {
  if (min_value !=== false && value < min_value) throw new TypeError('must be greater than ' + min_value)
})
.validate(18, {min: 21}, function(errors) {
  console.log(errors[0].message) // `min`: must be greater than 21
})

Async Functions

v.rule('min', function(value, min_value, next) {
  // call next(error) to report an error, next() if the value is valid
})

Promises

v.rule('min', function (value, min_value) {
  return functionThatReturnsAPromise(value, min_value)
})

Thunks

v.rule('min', function (value, min_value) {
    return function (next) {
      next(new Error('something went wrong'))
    }
})

String Functions (via component/to-function)

v.rule('can drink', 'age >= 21')
.enable('can drink')
.validate({age: 18}, callback)

#.validate(value, [settings, context,] callback)

Validates a value with the rules/settings from settings. You may optionally supply a context object which will be used to call the rule functions (rule.call(context, value, settings[rule_name], next)) If you don't pass a settings object it'll use the settings set via Configurable methods such as set, enable, or disable

Configurable Methods

You can enable rules and set their configuration values the methods defined by visionmedia/configurable.js.

By default validator will execute all the rules you've chosen settings for even if they throw errors (so you can get the full list of errors). If you wish to fail on the first validation error you can use v.enable('strict')

Async validator function must either take 3 arguments (value, options, next), return a promise, or return a thunk

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •