5 Star 2 Fork 0

Gitee 极速下载 / ts-rules

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/kinogam/ts-rules/releases
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ts-rules

Build Status

A powerful JSON validator that design to validate complex JSON object.

How to install

npm install ts-rules --save

How to use

es6 import

import {rules} from 'ts-rules';

Simple

let r = rules({
  p1: 'required | maxLen: 5',
  p2: 'eq: {{p1}}'
});

let result = r({
  p1: '123456',
  p2: ''
});

result.valid; //false

result = r({
  p1: '12345',
  p2: '12345'
});

result.valid; //true

Rules

required

{
    'propName': 'required'
}

number

{
    'propName': 'number'
}

email

{
    'propName': 'email'
}

max length

{
    'propName': 'maxLen: 8'
}

eq

// equal
{
    'propName': `eq: 'kino'`
}

gt

// greater than
{
    'propName': `gt: 23`
}

gte

// greater than or equal to
{
    'propName': `gte: 23`
}

lt

// less than
{
    'propName': `lt: 23`
}

lte

// less than or equal to
{
    'propName': `lte: 23`
}

custom rule

r = rules({
    p: 'myRule: "kinogam"'
});


r.register('myRule', (val, name) => {
    return val.indexOf(name) !== -1;
});

json = {
    p: 'hello, kinogam!'
};

r(json).valid; // true

multiple rules

use | to separate rules

r = rules({
    p: 'email | maxLen: 14'
});

json = {
    p: 'kino@gmail.com'
};

r(json).valid; // true

json = {
    p: 'kinogam@gmail.com'
};

r(json).valid; // false

wildcard *

you can use * as a wildcard to match all fields

r = rules({
    '*': 'required'
});

json = {
    p1: 'hello',
    p2: 'world'
};

r(json).valid; // true

json = {
    p1: 'hello',
    p2: ''
};

r(json).valid; // false

validation info

when you use rFn(json) then will return a validation result

result.valid;

you also can see each field's validation info

r = rules({
    p1: 'required',
    p2: 'required'
});

json = {
    p1: 'kino',
    p2: ''
};

let result = r(json);

result.fields.p1.invalid; // false
result.fields.p2.invalid; // true;

and you can set error message, the 'labels' property is optional

let info = {
    labels: {
        p1: 'my field',
        p2: 'your field'
    },
    message: {
        p1: {
            'required': '{{p1}} is required',
            'maxLen': '{{p1}} can not longer than 5 characters'
        },
        p2: {
            'eq': '{{p2}} must equal to {{p1}}'
        }
    }
};

r = rules({
    p1: 'required | maxLen: 5',
    p2: 'eq: {{p1}}'
}, info);

json = {
    p1: '',
    p2: ''
};

let result = r(json);

result.fields.p1.message; // 'my field is required'

json = {
    p1: '123456',
    p2: ''
};

result = r(json);

result.fields.p1.message; // 'my field can not longer than 5 characters'

json = {
    p1: '123',
    p2: '234'
};

result = r(json);

result.fields.p2.message; // 'your field must equal to my field';
MIT License Copyright (c) 2016 kinogam Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Typescript JSON 验证工具,主要设计来验证复杂的约束需求 展开 收起
TypeScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/mirrors/ts-rules.git
git@gitee.com:mirrors/ts-rules.git
mirrors
ts-rules
ts-rules
master

搜索帮助