ForgJs是一个JavaScript轻量级对象验证器. 请检查access-date=中的日期值




请参看更多现场实例 这里
快开始
通过 npm 运行安装 npm i @cesium133/forgjs
您的第一个验证符
const { Validator, Rule } = require('@cesium133/forgjs');
const emailRule = new Rule({
type: 'email',
user: user => user === 'dedede',
domain: domain => ['outlook', 'gmail', 'yahoo'].indexOf(domain) !== -1,
}, null);
const passwordRule = new Rule({
type: 'password',
minLength: 8,
uppercase: 1,
numbers: 1,
matchesOneOf: ['@', '_', '-', '.', '!'],
}, null);
const vComplex = new Validator({
age: new Rule({ type: 'int', min: 18, max: 99 }),
dateOfBirth: new Rule({ type: 'date' }),
array: new Rule({ type: 'array', of: new Rule({ type: 'string' }) }),
email: emailRule,
password: passwordRule
});
vComplex.test({
age: 26,
dateOfBirth: new Date(1995, 10, 3),
array: ['1'],
email: '[email protected];',
password: 'ad1_A@@Axs',
}); /// returns true
处理错误
您可以通过这样做获取自定义错误消息 :
const vComplexe = new Validator({
age: new Rule({
type: 'int', min: 18, max: 99,
}, 'age must be integer and between 18 and 99'),
dateOfBirth: new Rule({ type: 'date' }, 'date must be a date'),
});
vComplexe.getErrors({
age: 16,
dateOfBirth: 123,
}); // ['age must be integer and between 18 and 99', 'date must be a date']
在一个阵列上测试
您可以使用这种方法在多个阵列上测试 :
const vComplexe = new Validator({
age: new Rule({
type: 'int', min: 18, max: 99,
}),
});
vComplexe.testAll([{
age: 19,
}, {
age: 16,
}]); // returns 1
规则
A 类 Rule 对象验证单个值,可以像这样使用 :
const { Validator, Rule } = require('@cesium133/forgjs');
const floatRule = new Rule({
type: 'float',
min: 100,
}, null);
floatRule.test(2.001); /// returns true;
唯一需要的值是 type来啊!
只需检查类型即可简单地通过字符串来制定规则 :
new Rule('int');
单位
- 分钟( int)
- 最大值( int)
- 等于( int)
布尔
- 改为 (布尔)
字符串
- 分钟( int)
- 最大倾角 (int)
- 等 (字符串)
- 匹配:(正则)
- 非Empty( bool ) (波尔语)
电子邮件
- 分钟( int)
- 最大倾角 (int)
- 等 (字符串)
- 匹配:(正则)
- 非Empty( bool ) (波尔语)
- 用户( e)
function(user)(中文(简体) ) - 域( 选项)
function(domain)(中文(简体) )
const emailRule = new Rule({
type: 'email',
user: user => user === 'dedede',
domain: domain => ['outlook', 'gmail', 'yahoo'].indexOf(domain) !== -1,
}, null);
emailRule.test('[email protected]'); // returns true
密码
- 分钟( int)
- 最大倾角 (int)
- 等 (字符串)
- 匹配:(正则)
- 非Empty( bool ) (波尔语)
- 大写 (int)
- 数字(int)
- mathes OneOf (阵列)
- 匹配 AllOf (箭头)
const passwordRule = new Rule({
type: 'password',
minLength: 8,
uppercase: 1,
numbers: 1,
matchesOneOf: ['@', '_', '-', '.', '!'],
}, null);
passwordRule.test('@_-bddcd6A'); // returns true
url 密码
- 分钟( int)
- 最大倾角 (int)
- 等 (字符串)
- 匹配:(正则)
- 非Empty( bool ) (波尔语)
- 协议
function(protocol)(中文(简体) ) - 域( 选项)
function(domain)(中文(简体) )
const urlRule = new Rule({
type: 'url',
protocol: prot => prot === 'https',
domain: domain => domain === 'google.fr',
}, null);
urlRule.test('https://google.fr'); // returns true
日期
- (日期后)
- (日期之前)
- (像这样的日期的排列[日期,日期])
- 等于( 日期)
浮动
- 分钟( 次数)
- 最大(数量)
- 等 (浮动)
数组
- (规则或验证对象)
- 非Empty( bool ) (波尔语)
- 长度( int)
这个 of 规则对照规则检查数组的每个元素.
函数
- 结果
为了解释结果,什么比一个例子更好:
const { Validator, Rule } = require('@cesium133/forgjs');
function someFunctionThatReturnsAnInt(int) {
return int * 5;
}
const functionTest = new Rule({
type: 'function',
result: {
of: 5,
toBe: new Rule('int'),
},
}, null);
functionTest.test(someFunctionThatReturnsAnInt); /// returns true;
字符串插入、字符串浮动、字符串日期、字符串布尔
这些“ 继承” 类型来自字符串, 它们有两种属性, 例如:
字符串加
const stringInt = new Rule({
type: 'string-int',
minLength: 2,
min: 5,
}, null);
stringInt.test(2) // returns false 2 is not a string
stringInt.test('2a') // returns false '2a' is not a int
stringInt.test('2.1') // returns false '2.1' is not a int
stringInt.test('5') // returns false length of '5' is smaller than 2
stringInt.test('50') // returns true
字符串布尔
const stringBoolean = new Rule({
type: 'string-boolean',
toBe: true
}, null);
stringBoolean.test(true) // returns false true is not a boolean
stringBoolean.test('false') // returns false 'false' is not true
stringBoolean.test('true') // returns true
const stringDate = new Rule({
type: 'string-date',
after: new Date(2019, 11, 1),
}, null);
stringDate.test(new Date(2018, 11, 1)) // returns false new Date(2018, 11, 1) is not a string
stringDate.test('some string') // returns false 'some string' is not a valid date
stringDate.test('2018-12-17') // returns false '2018-12-17' is not after new Date(2019, 11, 1)
stringDate.test('2020-01-01') // returns true
Forgjs试图将数值投向正确的类型,然后传递给验证函数,请小心!
以下是Javascript行为使测试错误的例子:
const stringDate = new Rule({
type: 'string-date',
equal: new Date(2019, 10, 1), // month in js strart at 0
}, null);
stringDate.test('2019-11-01') // returns false
stringDate.test('2019-11-01T00:00') // returns true
// this is because:
new Date(2019, 10, 1) - new Date('2019-11-01') // equals 3600000 thats exactly 1 hour
new Date(2019, 10, 1) - new Date('2019-11-01T00:00') // equals 0
多个类型
您可以检查多个类型 OR 或者说 AND 像这样的运算符 :
const intRule = new Rule({
type: 'int|float|number',
}, null);
intRule.test(2) // returns true
这意味着试验应核查 int, (中文(简体) ), float 或者说 number 规则
const intRule = new Rule({
type: 'int&number',
}, null);
intRule.test(2.1); // returns false
结果是不匹配的 int 规则
共同属性
每个类型都有这些属性:
- 选项
- 自定义
- 其中一个
选项
如果设置为 true 该元素是可选的和一个 undefined 数值被认为是正确的.
示例
const { Validator, Rule } = require('@cesium133/forgjs');
const intRule = new Rule({
type: 'int',
optional: true,
}, null);
intRule.test(); // returns true
自定义
自定义允许您写自己的规则, 例比长的解释要好 :
const { Validator, Rule } = require('@cesium133/forgjs');
function isCorrectAge(age, object) {
if (age === Math.floor((new Date() - object.dateOfBirth) / 1000 / 60 / 60 / 24 / 30 / 12)) {
return true;
}
return false;
}
const vComplexe = new Validator({
age: new Rule({
type: 'int', min: 18, max: 99, custom: isCorrectAge,
}),
dateOfBirth: new Rule({ type: 'date' }),
});
vComplexe.test({
age: 23,
dateOfBirth: new Date(1995, 10, 3),
array: ['1'],
}); // returns true
其中一个
如果元素处于数组中, 请检查其中之一
const floatRule = new Rule({
type: 'float',
oneOf: [3.5, 100.1, 7.2, 0.1],
}, null);
floatRule.test(100.1); // returns true
制作新类型
使用这样的规则类创建新类型 :
const { Validator, Rule } = require('@cesium133/forgjs');
Rule.addCustom('customInteger', {
min: (val, min) => val - min > 0,
max: (val, max) => val - max < 0,
equal: (val, equal) => val === equal,
type: val => Number.isInteger(val) && val > 0 && val < 100,
});
const customInteger = new Rule({
type: 'customInteger',
min: 10,
}, null);
customInteger.test(11) // returns true
customInteger.test(200) // returns false
如何捐款
感谢大家为改进这个代码所做的贡献, 如果你有改进代码的建议或想法, 请在这里留下评论#29. 规则:
1 请使用此模板来帮助开发者测试和更好地理解您的请求
const someRule= new Rule({
type: 'yourType',
prop1: val1,
prop2: val2, ...
}, null);
someRule.test(validValue) // returns true
someRule.test(invalidValue) // returns false
2,如果您认为一个评论是像评论一样添加的好特征,而不是创建一个新的评论.
3 如果同一评论尚未提交,在提交新的评论检查之前
4 如果您提交 PR( 推请求) 而您只更改 Readme 请添加 [ci skip] 发送到您承诺的消息
5,如果你有任何问题 在FAQ中问他们
6,请玩得开心,如果你不想遵守规则 那就不要遵守规则
联系人
在推特上跟我来 @forg js (英语)
