JSON validator for Node.js and browser by using simple configuration rule
Parameter Class
constructor([options])- New ClassParameterinstanceoptions.isUseDefault- If use default value, default totrueoptions.isRemoveAdditional- Delete is not defined in the rule as an attribute, default tofalseoptions.isCoerceTypes- Change data type of data to match type keyword, default tofalseoptions.emptyValues- The contained value will be treated as a empty value, default to[null, undefined, NaN, '']
schema(rule, [options])- Returns a validate with fixed parametersvalidate(rule, value, [options])- Validate thevalueconforms torule. return an array of errors if break rulerule- The rule of the verified jsonvalue- JSON to be verifiedoptions- Override the definition ofconstructor([options])
addRule(type, checkFunction)- Add custom rules.type- Rule type, required and must be string type.checkFunction(rule, value)- Custom check rule function.
import { Parameter } from '@ckpack/parameter';
const parameter = new Parameter();
// check rule
const rule = {
isAdmin: {
type: 'boolean',
},
age: {
type: 'int',
min: 0,
},
role: {
type: 'enum',
enum: ['am', 'pm'],
}
ids: {
type: 'array',
itemType: 'int',
itemRule: {
min: 1
}
}
}
// check value
const data = {
isAdmin: true,
age: 18,
role: 'am',
ids: [1, 2, 3]
};
const errors = parameter.validate(rule, data);setLocale 设置错误信息语言
设置默认语言为中文
import { zhLocale, setLocale, Parameter } from '@ckpack/parameter';
setLocale(zhLocale);
const parameter = new Parameter();
// parameter.validate(rule, data);- type: The rule type
- message: Custom error message
- isRequired: If check the empty value, default to
true - default: Is
isUseDefaultistrue, the empty value will be set default value, default toundefined
If type is int, There are the following options rules
max- The maximum of the value, value must <=maxmin- The minimum of the value, value must >=min
{
score: 'int',
}
// or
{
score: {
type: 'int',
min: 0,
max: 200,
}
}If type is number, There are the following options rules
max- The maximum of the value, value must <=maxmin- The minimum of the value, value must >=min
example
{
score: 'number',
}
// or
{
score: {
type: 'number',
min: 0,
max: 100,
}
}If type is string, There are the following options rules
regexp- A RegExp to check string's formatmax- The maximum length of the stringmin- The minimum length of the string
example
{
username: 'string',
}
// or
{
username: /\S{4,20}/,
}
// or
{
username: {
type: 'string',
regexp: /\S{4,20}/
}
}If type is boolean
example
{
isAll: 'boolean',
}
// or
{
isAll: {
type: 'boolean',
}
}If type is array, There are the following options rules
itemType- The type of every item in this arrayitemRule- The rule of every item in this RuleitemChecker- The checker of every item, in this case you can omititemTypeanditemRulemax- The maximun length of the arraymin- The minimum length of the array
example
{
ids: {
itemType: 'int',
itemRule: {
min: 1,
max: 1000,
},
min: 0,
max: 100,
}
}If type is enum, There are the following rules
enum- An array of data, value must be one on them
example
{
sex: ['man', 'woman']
}
// or
{
sex: {
type: 'enum'
enum: ['man', 'woman']
}
}If type is object, There are the following rules
rule- An object that validate the properties ot the object
example
{
people: {
type: 'object',
rule: {
name: 'string',
age: {
isRequired: false,
type: 'int',
min: 1,
max: 200
}
}
}
}definition custom rule, example
// custom check
parameter.addRule('even', (rule, value) => {
return value % 2 === 0 ? null : `${value} is not even`;
});use the custom rule
{
someNumber: 'even'
}
//or
{
someNumber: {
type: "even",
}
}you alse can add options rules
import { Parameter } from '@ckpack/parameter';
const parameter = new Parameter();
parameter.addRule('times', (rule, value) => {
const { times } = rule;
return value % times === 0 ? null : `not an integer multiple of ${times}`;
});
// rule
{
someNumber: {
type: "times",
times: 3,
}
}