Skip to content

Seasonley/sultana-validator

Repository files navigation

NPM

NPM version Build Status Coverage Status Downloads FOSSA Status

A library for validating as same as validator.py

Documentation

This README has some basic usage information, but more detailed documentation may be found at here.

Install

npm install sultana-vaidator

Usage Example

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']
//  })

License

MIT

FOSSA Status

Change Log

click here for more details.

About

🔍 A library for validating as same as validator.py (JS的数据校验框架,无依赖)

Resources

License

Stars

Watchers

Forks

Packages

No packages published