schema-typed
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "schema-typed", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Schema for data modeling & validation", | ||
@@ -10,4 +10,5 @@ "main": "lib/index.js", | ||
"prepublish": "npm run build", | ||
"tdd": "mocha --compilers js:babel-register test/*Spec.js", | ||
"test": "npm run lint && npm run tdd" | ||
"tdd": "mocha --watch --compilers js:babel-register test/*Spec.js ", | ||
"test-once": "mocha --compilers js:babel-register test/*Spec.js ", | ||
"test": "npm run lint && npm run test-once" | ||
}, | ||
@@ -54,4 +55,5 @@ "repository": { | ||
"istanbul": "^0.4.5", | ||
"mocha": "^2.5.3" | ||
"mocha": "^2.5.3", | ||
"object-flaser": "^0.1.1" | ||
} | ||
} |
261
README.md
# schema-typed | ||
`schema-typed` 是一个数据建模及数据验证工具 | ||
Schema for data modeling & validation | ||
版本与状态 | ||
[![npm][npm-badge]][npm] | ||
[![Travis][build-badge]][build] | ||
## 安装 | ||
English | [中文版][readm-cn] | ||
## Installation | ||
``` | ||
@@ -16,3 +16,3 @@ npm install schema-typed --save | ||
## 示例 | ||
## Usage | ||
@@ -22,9 +22,13 @@ ```js | ||
const userModel = SchemaModel({ | ||
username: StringType().isRequired('用户名不能为空'), | ||
email: StringType().isEmail('请输入正确的邮箱'), | ||
age: NumberType('年龄应该是一个数字').range(18, 30, '年应该在 18 到 30 岁') | ||
const model = SchemaModel({ | ||
username: StringType().isRequired('Username required'), | ||
email: StringType().isEmail('Email required'), | ||
age: NumberType('Age should be a number').range( | ||
18, | ||
30, | ||
'Age should be between 18 and 30 years old' | ||
) | ||
}); | ||
const checkResult = userModel.check({ | ||
const checkResult = model.check({ | ||
username: 'foobar', | ||
@@ -38,3 +42,3 @@ email: 'foo@bar.com', | ||
`checkResult` 返回结构是: | ||
`checkResult` return structure is: | ||
@@ -45,30 +49,30 @@ ```js | ||
email: { hasError: false }, | ||
age: { hasError: true, errorMessage: '年应该在 18 到 30 岁' } | ||
age: { hasError: true, errorMessage: 'Age should be between 18 and 30 years old' } | ||
} | ||
``` | ||
## 多重验证 | ||
## Multiple verification | ||
```js | ||
StringType() | ||
.minLength(6, '不能少于 6 个字符') | ||
.maxLength(30, '不能大于 30 个字符') | ||
.isRequired('该字段不能为空'); | ||
.minLength(6, "Can't be less than 6 characters") | ||
.maxLength(30, 'Cannot be greater than 30 characters') | ||
.isRequired('This field required'); | ||
``` | ||
## 自定义验证 | ||
## Custom verification | ||
通过 `addRule` 函数自定义一个规则。 | ||
Customize a rule with the `addRule` function. | ||
如果是对一个字符串类型的数据进行验证,可以通过 `pattern` 方法设置一个正则表达式进行自定义验证。 | ||
If you are validating a string type of data, you can set a regular expression for custom validation by the `pattern` method. | ||
```js | ||
const myModel = SchemaModel({ | ||
const model = 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}$/, '请输入合法字符') | ||
}, 'Please enter legal characters'), | ||
field2: StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, 'Please enter legal characters') | ||
}); | ||
schema.check({ field1: '', field2: '' }); | ||
model.check({ field1: '', field2: '' }); | ||
@@ -79,7 +83,7 @@ /** | ||
hasError: true, | ||
errorMessage: '请输入合法字符' | ||
errorMessage: 'Please enter legal characters' | ||
}, | ||
field2: { | ||
hasError: true, | ||
errorMessage: '请输入合法字符' | ||
errorMessage: 'Please enter legal characters' | ||
} | ||
@@ -90,9 +94,9 @@ }; | ||
## 自定义验证 - 多字段交叉验证 | ||
## Custom verification - multi-field cross validation | ||
例如,验证两次输入密码是否一致 | ||
E.g: verify that the two passwords are the same. | ||
```js | ||
const schema = SchemaModel({ | ||
password1: StringType().isRequired('该字段不能为空'), | ||
const model = SchemaModel({ | ||
password1: StringType().isRequired('This field required'), | ||
password2: StringType().addRule((value, data) => { | ||
@@ -103,6 +107,6 @@ if (value !== data.password1) { | ||
return true; | ||
}, '两次密码不一致') | ||
}, 'The passwords are inconsistent twice') | ||
}); | ||
schema.check({ password1: '123456', password2: 'root' }); | ||
model.check({ password1: '123456', password2: 'root' }); | ||
@@ -114,3 +118,3 @@ /** | ||
hasError: true, | ||
errorMessage: '两次密码不一致' | ||
errorMessage: 'The passwords are inconsistent twice' | ||
} | ||
@@ -121,2 +125,44 @@ }; | ||
## Validate nested objects | ||
Validate nested objects, which can be defined using the `ObjectType().shape` method. E.g: | ||
```js | ||
const model = SchemaModel({ | ||
id: NumberType().isRequired('This field required'), | ||
name: StringType().isRequired('This field required'), | ||
info: ObjectType().shape({ | ||
email: StringType().isEmail('Should be an email'), | ||
age: numberType().min(18, 'Age should be greater than 18 years old') | ||
}); | ||
}); | ||
``` | ||
It is more recommended to flatten the object. | ||
```js | ||
import { flaser } from 'object-flaser'; | ||
const model = SchemaModel({ | ||
id: NumberType().isRequired('This field required'), | ||
name: StringType().isRequired('This field required'), | ||
'info.email': StringType().isEmail('Should be an email'), | ||
'info.age': numberType().min(18, 'Age should be greater than 18 years old') | ||
}); | ||
const user = flaser({ | ||
id: 1, | ||
name: 'schema-type', | ||
info: { | ||
email: 'schema-type@gmail.com', | ||
age: 17 | ||
} | ||
}); | ||
model.check(data); | ||
``` | ||
## API | ||
@@ -129,78 +175,78 @@ | ||
```js | ||
StringType().isRequired('该字段不能为空'); | ||
StringType().isRequired('This field required'); | ||
``` | ||
- isEmail(String: errorMessage) | ||
- isEmail(errorMessage: string) | ||
```js | ||
StringType().isEmail('请输入正确的邮箱地址'); | ||
StringType().isEmail('Please input the correct email address'); | ||
``` | ||
- isURL(String: errorMessage) | ||
- isURL(errorMessage: string) | ||
```js | ||
StringType().isURL('请输入正确的URL地址'); | ||
StringType().isURL('Please enter the correct URL address'); | ||
``` | ||
- isOneOf(Array: items, String: errorMessage) | ||
- isOneOf(items: Array, errorMessage: string) | ||
```js | ||
StringType().isOneOf(['Javascript', 'CSS'], '只能输入 `Javascript`和 `CSS`'); | ||
StringType().isOneOf(['Javascript', 'CSS'], 'Can only type `Javascript` and `CSS`'); | ||
``` | ||
- containsLetter(String: errorMessage) | ||
- containsLetter(errorMessage: string) | ||
```js | ||
StringType().containsLetter('必须包含英文字符'); | ||
StringType().containsLetter('Must contain English characters'); | ||
``` | ||
- containsUppercaseLetter(String: errorMessage) | ||
- containsUppercaseLetter(errorMessage: string) | ||
```js | ||
StringType().containsUppercaseLetter('必须包含大写的英文字符'); | ||
StringType().containsUppercaseLetter('Must contain uppercase English characters'); | ||
``` | ||
- containsLowercaseLetter(String: errorMessage) | ||
- containsLowercaseLetter(errorMessage: string) | ||
```js | ||
StringType().containsLowercaseLetter('必须包含小写的英文字符'); | ||
StringType().containsLowercaseLetter('Must contain lowercase English characters'); | ||
``` | ||
- containsLetterOnly(String: errorMessage) | ||
- containsLetterOnly(errorMessage: string) | ||
```js | ||
StringType().containsLetterOnly('只能包含的英文字符'); | ||
StringType().containsLetterOnly('English characters that can only be included'); | ||
``` | ||
- containsNumber(String: errorMessage) | ||
- containsNumber(errorMessage: string) | ||
```js | ||
StringType().containsNumber('必须包含数字'); | ||
StringType().containsNumber('Must contain numbers'); | ||
``` | ||
- pattern(Object: regexp, String: errorMessage) | ||
- pattern(regExp: RegExp, errorMessage: string) | ||
```js | ||
StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, '请输入合法字符'); | ||
StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, 'Please enter legal characters'); | ||
``` | ||
- rangeLength(Number: minLength, Number: maxLength, String: errorMessage) | ||
- rangeLength(minLength: number, maxLength: number, errorMessage: string) | ||
```js | ||
StringType().rangeLength(6, 30, '字符个数只能在 6 - 30 之间'); | ||
StringType().rangeLength(6, 30, 'The number of characters can only be between 6 and 30'); | ||
``` | ||
- minLength(Number: minLength, String: errorMessage) | ||
- minLength(minLength: number, errorMessage: string) | ||
```js | ||
StringType().minLength(6, '最小需要6个字符'); | ||
StringType().minLength(6, 'Minimum 6 characters required'); | ||
``` | ||
- maxLength(Number: maxLength, String: errorMessage) | ||
- maxLength(maxLength: number, errorMessage: string) | ||
```js | ||
StringType().minLength(30, '最大只能30个字符'); | ||
StringType().minLength(30, 'The maximum is only 30 characters.'); | ||
``` | ||
- addRule(Function: onValid, String: errorMessage) | ||
- addRule(onValid: Function, errorMessage: string) | ||
@@ -210,6 +256,6 @@ ```js | ||
return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value); | ||
}, '请输入合法字符'); | ||
}, 'Please enter a legal character.'); | ||
``` | ||
### NumbserType | ||
### NumberType | ||
@@ -219,47 +265,47 @@ - isRequired() | ||
```js | ||
NumbserType().isRequired('该字段必填'); | ||
NumberType().isRequired('This field required'); | ||
``` | ||
- isInteger(String: errorMessage) | ||
- isInteger(errorMessage: string) | ||
```js | ||
NumbserType().isInteger('只能是整型'); | ||
NumberType().isInteger('It can only be an integer'); | ||
``` | ||
- isOneOf(Array: items, String: errorMessage) | ||
- isOneOf(items: Array, errorMessage: string) | ||
```js | ||
NumbserType().isOneOf([5, 10, 15], '只能是`5`,`10`,`15`'); | ||
NumberType().isOneOf([5, 10, 15], 'Can only be `5`, `10`, `15`'); | ||
``` | ||
- pattern(Object: regexp, String: errorMessage) | ||
- pattern(regExp: RegExp, errorMessage: string) | ||
```js | ||
NumbserType().pattern(/^[1-9][0-9]{3}$/, '请输入合法字符'); | ||
NumberType().pattern(/^[1-9][0-9]{3}$/, 'Please enter a legal character.'); | ||
``` | ||
- range(Number: minLength, Number: maxLength, String: errorMessage) | ||
- range(minLength: number, maxLength: number, errorMessage: string) | ||
```js | ||
NumbserType().range(18, 40, '请输入 18 - 40 之间的数字'); | ||
NumberType().range(18, 40, 'Please enter a number between 18 - 40'); | ||
``` | ||
- min(Number: min, String: errorMessage) | ||
- min(min: number, errorMessage: string) | ||
```js | ||
NumbserType().min(18, '最小值 18'); | ||
NumberType().min(18, 'Minimum 18'); | ||
``` | ||
- max(Number: min, String: errorMessage) | ||
- max(max: number, errorMessage: string) | ||
```js | ||
NumbserType().max(40, '最大值 40'); | ||
NumberType().max(40, 'Maximum 40'); | ||
``` | ||
- addRule(Function: onValid, String: errorMessage) | ||
- addRule(onValid: Function, errorMessage: string) | ||
```js | ||
NumbserType().addRule((value, data) => { | ||
NumberType().addRule((value, data) => { | ||
return value % 5 === 0; | ||
}, '请输入有效的数字'); | ||
}, 'Please enter a valid number'); | ||
``` | ||
@@ -272,36 +318,36 @@ | ||
```js | ||
ArrayType().isRequired('该字段必填'); | ||
ArrayType().isRequired('This field required'); | ||
``` | ||
- rangeLength(Number: minLength, Number: maxLength, String: errorMessage) | ||
- rangeLength(minLength: number, maxLength: number, errorMessage: string) | ||
```js | ||
ArrayType().rangeLength(1, 3, '至少选择1个,但不能超过3个'); | ||
ArrayType().rangeLength(1, 3, 'Choose at least one, but no more than three'); | ||
``` | ||
- minLength(Number: minLength, String: errorMessage) | ||
- minLength(minLength: number, errorMessage: string) | ||
```js | ||
ArrayType().minLength(1, '至少选择1个'); | ||
ArrayType().minLength(1, 'Choose at least one'); | ||
``` | ||
- maxLength(Number: maxLength, String: errorMessage) | ||
- maxLength(maxLength: number, errorMessage: string) | ||
```js | ||
ArrayType().maxLength(3, '不能超过3个'); | ||
ArrayType().maxLength(3, "Can't exceed three"); | ||
``` | ||
- unrepeatable(String: errorMessage) | ||
- unrepeatable(errorMessage: string) | ||
```js | ||
ArrayType().unrepeatable('不能出现重复选项'); | ||
ArrayType().unrepeatable('Duplicate options cannot appear'); | ||
``` | ||
- of(Object: type, String: errorMessage) | ||
- of(type: Object, errorMessage: string) | ||
```js | ||
ArrayType().of(StringType().isEmail(), '格式错误'); | ||
ArrayType().of(StringType().isEmail(), 'wrong format'); | ||
``` | ||
- addRule(Function: onValid, String: errorMessage) | ||
- addRule(onValid: Function, errorMessage: string) | ||
@@ -311,3 +357,3 @@ ```js | ||
return value.length % 2 === 0; | ||
}, '好事成双'); | ||
}, 'Good things are in pairs'); | ||
``` | ||
@@ -320,6 +366,6 @@ | ||
```js | ||
DateType().isRequired('日期不能为空'); | ||
DateType().isRequired('This field required'); | ||
``` | ||
- range(Date: min, Date: max, String: errorMessage) | ||
- range(min: Date, max: Date, errorMessage: string) | ||
@@ -330,19 +376,19 @@ ```js | ||
new Date('08/30/2017'), | ||
'时间应该在 08/01/2017 - 08/30/2017 之间' | ||
'Date should be between 08/01/2017 - 08/30/2017' | ||
); | ||
``` | ||
- min(Date: min, String: errorMessage) | ||
- min(min: Date, errorMessage: string) | ||
```js | ||
DateType().min(new Date('08/01/2017'), '时间的最小值 08/01/2017'); | ||
DateType().min(new Date('08/01/2017'), 'Minimum date 08/01/2017'); | ||
``` | ||
- max(Date: max, String: errorMessage) | ||
- max(max: Date, errorMessage: string) | ||
```js | ||
DateType().max(new Date('08/30/2017'), '时间的最大值 08/30/2017'); | ||
DateType().max(new Date('08/30/2017'), 'Maximum date 08/30/2017'); | ||
``` | ||
- addRule(Function: onValid, String: errorMessage) | ||
- addRule(onValid: Function, errorMessage: string) | ||
@@ -352,3 +398,3 @@ ```js | ||
return value.getDay() === 2; | ||
}, '只能选择周二'); | ||
}, 'Can only choose Tuesday'); | ||
``` | ||
@@ -361,15 +407,15 @@ | ||
```js | ||
ObjectType().isRequired('该对象不能为空'); | ||
ObjectType().isRequired('This field required'); | ||
``` | ||
- shape(Object: types) | ||
- shape(type: Object) | ||
```js | ||
ObjectType().shape({ | ||
email: StringType().isEmail('应该是一个 email'), | ||
age: NumberType().min(18, '年龄应该大于18岁') | ||
email: StringType().isEmail('Should be an email'), | ||
age: NumberType().min(18, 'Age should be greater than 18 years old') | ||
}); | ||
``` | ||
- addRule(Function: onValid, String: errorMessage) | ||
- addRule(onValid: Function, errorMessage: string) | ||
@@ -382,3 +428,3 @@ ```js | ||
return false; | ||
}, 'id 与 email 必须有一个不能为空'); | ||
}, 'Id and email must have one that cannot be empty'); | ||
``` | ||
@@ -391,6 +437,6 @@ | ||
```js | ||
BooleanType().isRequired('该字段不能为空'); | ||
BooleanType().isRequired('This field required'); | ||
``` | ||
- addRule(Function: onValid, String: errorMessage) | ||
- addRule(onValid: Function, errorMessage: string) | ||
@@ -403,5 +449,6 @@ ```js | ||
return true; | ||
}, '当 A 等于 10 的时候,该值必须为空'); | ||
}, 'This value is required when A is equal to 10'); | ||
``` | ||
[readm-cn]: https://github.com/rsuite/schema-typed/blob/master/README_zh.md | ||
[npm-badge]: https://img.shields.io/npm/v/schema-typed.svg | ||
@@ -408,0 +455,0 @@ [npm]: https://www.npmjs.com/package/schema-typed |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56071
441
20