Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
rsuite-schema
Advanced tools
rsuite-schema
是一个数据建模及数据验证工具
版本与状态
npm i rsuite-schema --save
// 或者
yarn add rsuite-schema
import { SchemaModel, StringType, DateType, NumberType } from 'rsuite-schema';
const userModel = SchemaModel({
username: StringType().isRequired('用户名不能为空'),
email: StringType().isEmail('请输入正确的邮箱'),
age: NumberType('年龄应该是一个数字').range(18, 30, '年应该在 18 到 30 岁')
});
const checkResult = userModel.check({
username: 'foobar',
email: 'foo@bar.com',
age: 40
});
console.log(checkResult);
checkResult
返回结构是:
{
username: { hasError: false },
email: { hasError: false },
age: { hasError: true, errorMessage: '年应该在 18 到 30 岁' }
}
StringType()
.minLength(6, '不能少于 6 个字符')
.maxLength(30, '不能大于 30 个字符')
.isRequired('该字段不能为空');
通过 addRule
函数自定义一个规则。
如果是对一个字符串类型的数据进行验证,可以通过 pattern
方法设置一个正则表达式进行自定义验证。
const myModel = SchemaModel({
field1: StringType().addRule((value, data) => {
return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
}, '请输入合法字符'),
field2: StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, '请输入合法字符')
});
schema.check({ field1: '', field2: '' });
/**
{
field1: {
hasError: true,
errorMessage: '请输入合法字符'
},
field2: {
hasError: true,
errorMessage: '请输入合法字符'
}
};
**/
例如,验证两次输入密码是否一致
const schema = SchemaModel({
password1: StringType().isRequired('该字段不能为空'),
password2: StringType().addRule((value, data) => {
if (value !== data.password1) {
return false;
}
return true;
}, '两次密码不一致')
});
schema.check({ password1: '123456', password2: 'root' });
/**
{
password1: { hasError: false },
password2: {
hasError: true,
errorMessage: '两次密码不一致'
}
};
**/
StringType().isRequired('该字段不能为空');
StringType().isEmail('请输入正确的邮箱地址');
StringType().isURL('请输入正确的URL地址');
StringType().isOneOf(['Javascript', 'CSS'], '只能输入 `Javascript`和 `CSS`');
StringType().containsLetter('必须包含英文字符');
StringType().containsUppercaseLetter('必须包含大写的英文字符');
StringType().containsLowercaseLetter('必须包含小写的英文字符');
StringType().containsLetterOnly('只能包含的英文字符');
StringType().containsNumber('必须包含数字');
StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, '请输入合法字符');
StringType().rangeLength(6, 30, '字符个数只能在 6 - 30 之间');
StringType().minLength(6, '最小需要6个字符');
StringType().minLength(30, '最大只能30个字符');
StringType().addRule((value, data) => {
return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
}, '请输入合法字符');
NumbserType().isRequired('该字段必填');
NumbserType().isInteger('只能是整型');
NumbserType().isOneOf([5, 10, 15], '只能是`5`,`10`,`15`');
NumbserType().pattern(/^[1-9][0-9]{3}$/, '请输入合法字符');
NumbserType().range(18, 40, '请输入 18 - 40 之间的数字');
NumbserType().min(18, '最小值 18');
NumbserType().max(40, '最大值 40');
NumbserType().addRule((value, data) => {
return value % 5 === 0;
}, '请输入有效的数字');
ArrayType().isRequired('该字段必填');
ArrayType().rangeLength(1, 3, '至少选择1个,但不能超过3个');
ArrayType().minLength(1, '至少选择1个');
ArrayType().maxLength(3, '不能超过3个');
ArrayType().unrepeatable('不能出现重复选项');
ArrayType().of(StringType().isEmail(), '格式错误');
ArrayType().addRule((value, data) => {
return value.length % 2 === 0;
}, '好事成双');
DateType().isRequired('日期不能为空');
DateType().range(
new Date('08/01/2017'),
new Date('08/30/2017'),
'时间应该在 08/01/2017 - 08/30/2017 之间'
);
DateType().min(new Date('08/01/2017'), '时间的最小值 08/01/2017');
DateType().max(new Date('08/30/2017'), '时间的最大值 08/30/2017');
DateType().addRule((value, data) => {
return value.getDay() === 2;
}, '只能选择周二');
ObjectType().isRequired('该对象不能为空');
ObjectType().shape({
email: StringType().isEmail('应该是一个 email'),
age: NumberType().min(18, '年龄应该大于18岁')
});
ObjectType().addRule((value, data) => {
if (value.id || value.email) {
return true;
}
return false;
}, 'id 与 email 必须有一个不能为空');
BooleanType().isRequired('该字段不能为空');
ObjectType().addRule((value, data) => {
if (typeof value === 'undefined' && A === 10) {
return false;
}
return true;
}, '当 A 等于 10 的时候,该值必须为空');
FAQs
Schema for data modeling & validation
The npm package rsuite-schema receives a total of 9 weekly downloads. As such, rsuite-schema popularity was classified as not popular.
We found that rsuite-schema demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.