A library for validating as same as validator.py
This README has some basic usage information, but more detailed documentation may be found at here.
npm install sultana-vaidator
import {
Required, Not, Truthy, Blank, Range, Equals, In, validate
} from 'sultana-validator'
// let's say that my dictionary needs to meet the following rules...
let rules = {
foo: [Required, Equals(123)],
bar: [Required, Truthy],
baz: [In(['spam', 'eggs', 'bacon'])],
qux: [Not(Range(1, 100))], // by default, Range is inclusive
}
// then this following dict would pass:
let passes = {
foo: 123,
bar: true, // or a non-empty string, or a non-zero int, etc...
baz: 'spam',
qux: 101,
}
validate(rules, passes)
// [true, {}]
// but this one would fail
let fails = {
foo: 321,
bar: false, // or 0, or [], or an empty string, etc...
baz: 'barf',
qux: 99,
}
validate(rules, fails)
// (false,
// {
// foo: ["must be equal to '123'"],
// bar: ['must be True-equivalent value'],
// baz: ["must be one of ['spam', 'eggs', 'bacon']"],
// qux: ['must not fall between 1 and 100']
// })