1.0.4 • Published 7 years ago

kserver-check v1.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

kserver-check

a powerful data validator use a set of simple template rule syntax

Installation

  • npm
npm install kserver-check --save-dev
  • or use yarn
yarn add kserver-check --dev

Usage:

check(templateRule, data, options?) : CheckResult

Rules

Object

  • key start with '*' indicate this prop is required

  • value type indicate this prop value type

  • value equal null means this prop value can be any type

Note: if value is function with parameters, it's a custom validator, and the function receive data value and options arguments and return boolean or CheckResult

// example
const templateRule = {
    '*name': 'name'
    'address': 'here address',
    'number': 10,
    'external': null,
    
    'hotel': { '*name': 'hotel name' },

    'callback'(){}
    'days'(days, options){ return days<10&&days>=3 },

    'list': [{}]
}
/* 
this Rule means data 
    require 'name' prop
    'name'/'address' expect String value
    'number' expect Number value (if strict set true, see Options)
    'external' can be all Types
    if exists 'hotel', 'hotel.name' is required
    'callback' expect a function
    'days' validate by custom function
*/

Array

  • first item: item rules
  • second item: options
const templateRule = [
    { '*name': 'name' },
    { min: 3, max: 10 }
]

/*
this Rule means data
    is Array
    Array item require 'name' prop
    Array need at least 3 item and at most 10 item;
*/

CheckResult

  • valid: indicate data is valid
  • data: if valid is true, data give you a filtered and converted result data (see Options)
  • error: if valid is false, error show you which prop/field check valid and error type
// example
{ valid: false, error: { 'name': 1, 'list.0.name': 2 } }

Options

fielddescriptiondefault
requirePrefixrequired field prefix'*'
filterif set true, the data in CheckResult will filter all non-defined props in template Rulefalse
strictif set false, the validator will try convert String to Number(or Number to String) before compare value type, and output the converted value in CheckResultfalse

Error Code

codedescription
1required field
2incorrect type
3array out of range
4custom check valid
5null value

Example:

// or use es6
// import check from 'kserver-check';
const check = require('kserver-check');

check({'*name':'name'}, {}) // result: {valid:false, error:{ 'name':1 } }
check([{'*name':'name'}], [{name:'wang'}, {}]) // result: {valid:false, error:{'1.name':1}}

License

MIT