rsuite-schema
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,1 +0,3 @@ | ||
{} | ||
{ | ||
"vsicons.presets.angular": false | ||
} |
@@ -29,3 +29,3 @@ 'use strict'; | ||
function ArrayType() { | ||
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'no error message' : arguments[0]; | ||
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'Please enter a valid array' : arguments[0]; | ||
@@ -36,3 +36,3 @@ _classCallCheck(this, ArrayType); | ||
_Type.prototype.addValidator.call(_this, function (v) { | ||
_Type.prototype.addRule.call(_this, function (v) { | ||
return Array.isArray(v); | ||
@@ -44,3 +44,3 @@ }, errorMessage); | ||
ArrayType.prototype.rangeLength = function rangeLength(minLength, maxLength, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return value.length >= minLength && value.length <= maxLength; | ||
@@ -52,3 +52,3 @@ }, errorMessage); | ||
ArrayType.prototype.minLength = function minLength(_minLength, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return value.length >= _minLength; | ||
@@ -60,3 +60,3 @@ }, errorMessage); | ||
ArrayType.prototype.maxLength = function maxLength(_maxLength, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return value.length <= _maxLength; | ||
@@ -68,3 +68,3 @@ }, errorMessage); | ||
ArrayType.prototype.unrepeatable = function unrepeatable(errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (items) { | ||
_Type.prototype.addRule.call(this, function (items) { | ||
var hash = {}; | ||
@@ -93,11 +93,18 @@ for (var i in items) { | ||
_Type.prototype.addValidator.call(this, function (items) { | ||
_Type.prototype.addRule.call(this, function (items) { | ||
var valids = items.map(function (value) { | ||
return type.check(value); | ||
}); | ||
return valids.reduce(function (previousValue, currentValue) { | ||
return !previousValue.hasError && !currentValue.hasError; | ||
}); | ||
}, errorMessage); | ||
var errors = valids.filter(function (item) { | ||
return item.hasError; | ||
}) || []; | ||
if (errors.length) { | ||
return errors[0]; | ||
} | ||
return errors.length === 0; | ||
}, null); | ||
return this; | ||
@@ -104,0 +111,0 @@ }; |
@@ -29,3 +29,3 @@ 'use strict'; | ||
function DateType() { | ||
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'no error message' : arguments[0]; | ||
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'Please enter a valid date' : arguments[0]; | ||
@@ -36,3 +36,3 @@ _classCallCheck(this, DateType); | ||
_Type.prototype.addValidator.call(_this, function (value) { | ||
_Type.prototype.addRule.call(_this, function (value) { | ||
return !/Invalid|NaN/.test(new Date(value)); | ||
@@ -44,3 +44,3 @@ }, errorMessage); | ||
DateType.prototype.range = function range(min, max, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return new Date(value) >= new Date(min) && new Date(value) <= new Date(max); | ||
@@ -52,3 +52,3 @@ }, errorMessage); | ||
DateType.prototype.min = function min(_min, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return new Date(value) >= new Date(_min); | ||
@@ -60,3 +60,3 @@ }, errorMessage); | ||
DateType.prototype.max = function max(_max, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return new Date(value) <= new Date(_max); | ||
@@ -63,0 +63,0 @@ }, errorMessage); |
@@ -33,3 +33,3 @@ 'use strict'; | ||
function NumberType() { | ||
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'no error message' : arguments[0]; | ||
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'Please enter a valid number' : arguments[0]; | ||
@@ -40,3 +40,3 @@ _classCallCheck(this, NumberType); | ||
_Type.prototype.addValidator.call(_this, function (value) { | ||
_Type.prototype.addRule.call(_this, function (value) { | ||
return (/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value) | ||
@@ -49,3 +49,3 @@ ); | ||
NumberType.prototype.isInteger = function isInteger() { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return (/^-?\d+$/.test(value) | ||
@@ -57,3 +57,3 @@ ); | ||
NumberType.prototype.pattern = function pattern(regexp, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return regexp.test(value); | ||
@@ -65,3 +65,3 @@ }, errorMessage); | ||
NumberType.prototype.isOneOf = function isOneOf(numLst, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return _f(value) in numLst; | ||
@@ -73,3 +73,3 @@ }, errorMessage); | ||
NumberType.prototype.range = function range(min, max, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return _f(value) >= min && _f(value) <= max; | ||
@@ -81,3 +81,3 @@ }, errorMessage); | ||
NumberType.prototype.min = function min(_min, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return _f(value) >= _min; | ||
@@ -89,3 +89,3 @@ }, errorMessage); | ||
NumberType.prototype.max = function max(_max, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return _f(value) <= _max; | ||
@@ -92,0 +92,0 @@ }, errorMessage); |
@@ -29,3 +29,3 @@ 'use strict'; | ||
function StringType() { | ||
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'no error message' : arguments[0]; | ||
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'Please enter a valid string' : arguments[0]; | ||
@@ -36,3 +36,3 @@ _classCallCheck(this, StringType); | ||
_Type.prototype.addValidator.call(_this, function (v) { | ||
_Type.prototype.addRule.call(_this, function (v) { | ||
return typeof v === 'string'; | ||
@@ -43,11 +43,4 @@ }, errorMessage); | ||
StringType.prototype.isLongerThan = function isLongerThan(n, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (v) { | ||
return v.length > n; | ||
}, errorMessage); | ||
return this; | ||
}; | ||
StringType.prototype.containsLetter = function containsLetter(errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (v) { | ||
_Type.prototype.addRule.call(this, function (v) { | ||
return (/[a-zA-Z]/.test(v) | ||
@@ -60,3 +53,3 @@ ); | ||
StringType.prototype.containsUppercaseLetter = function containsUppercaseLetter(errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (v) { | ||
_Type.prototype.addRule.call(this, function (v) { | ||
return (/[A-Z]/.test(v) | ||
@@ -69,3 +62,3 @@ ); | ||
StringType.prototype.containsLowercaseLetter = function containsLowercaseLetter(errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (v) { | ||
_Type.prototype.addRule.call(this, function (v) { | ||
return (/[a-z]/.test(v) | ||
@@ -78,3 +71,3 @@ ); | ||
StringType.prototype.containsLetterOnly = function containsLetterOnly(errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (v) { | ||
_Type.prototype.addRule.call(this, function (v) { | ||
return (/^[a-zA-Z]+$/.test(v) | ||
@@ -87,3 +80,3 @@ ); | ||
StringType.prototype.containsNumber = function containsNumber(errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (v) { | ||
_Type.prototype.addRule.call(this, function (v) { | ||
return (/[0-9]/.test(v) | ||
@@ -96,3 +89,3 @@ ); | ||
StringType.prototype.isOneOf = function isOneOf(strArr, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (v) { | ||
_Type.prototype.addRule.call(this, function (v) { | ||
return ~strArr.indexOf(v); | ||
@@ -105,3 +98,3 @@ }, errorMessage); | ||
var re = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; | ||
_Type.prototype.addValidator.call(this, function (v) { | ||
_Type.prototype.addRule.call(this, function (v) { | ||
return re.test(v); | ||
@@ -112,5 +105,5 @@ }, errorMessage); | ||
StringType.prototype.isURL = function isURL() { | ||
StringType.prototype.isURL = function isURL(errorMessage) { | ||
var re = /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i; | ||
_Type.prototype.addValidator.call(this, function (v) { | ||
_Type.prototype.addRule.call(this, function (v) { | ||
return re.test(v); | ||
@@ -122,3 +115,3 @@ }, errorMessage); | ||
StringType.prototype.pattern = function pattern(regexp, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return regexp.test(value); | ||
@@ -130,3 +123,3 @@ }, errorMessage); | ||
StringType.prototype.rangeLength = function rangeLength(minLength, maxLength, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return value.length >= minLength && value.length <= maxLength; | ||
@@ -138,3 +131,3 @@ }, errorMessage); | ||
StringType.prototype.minLength = function minLength(_minLength, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return value.length >= _minLength; | ||
@@ -146,3 +139,3 @@ }, errorMessage); | ||
StringType.prototype.maxLength = function maxLength(_maxLength, errorMessage) { | ||
_Type.prototype.addValidator.call(this, function (value) { | ||
_Type.prototype.addRule.call(this, function (value) { | ||
return value.length <= _maxLength; | ||
@@ -149,0 +142,0 @@ }, errorMessage); |
@@ -7,4 +7,24 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function isEmpty(value) { | ||
return typeof value === 'undefined' || value === null || value === ''; | ||
} | ||
function checkRequired(value) { | ||
//String trim | ||
if (typeof value === 'string') { | ||
value = value.replace(/(^\s*)|(\s*$)/g, ''); | ||
} | ||
//String/Array length > 0 | ||
if (value && value.length && value.length > 0) { | ||
return true; | ||
} | ||
return !isEmpty(value); | ||
} | ||
var Type = function () { | ||
@@ -16,26 +36,40 @@ function Type(name) { | ||
this.required = false; | ||
this.validators = []; | ||
this.requiredMessage = ''; | ||
this.rules = []; | ||
} | ||
Type.prototype.check = function check(value) { | ||
for (var i = this.validators.length; i > 0; i--) { | ||
var _validators = this.validators[i - 1]; | ||
var onValid = _validators.onValid; | ||
var errorMessage = _validators.errorMessage; | ||
if (this.required && !checkRequired(value)) { | ||
return { hasError: true, errorMessage: this.requiredMessage }; | ||
} | ||
if (!this.required && (typeof value === 'undefined' || value === null || value === '')) { | ||
for (var i = 0; i < this.rules.length; i++) { | ||
var _rules$i = this.rules[i]; | ||
var onValid = _rules$i.onValid; | ||
var errorMessage = _rules$i.errorMessage; | ||
if (!this.required && isEmpty(value)) { | ||
return { hasError: false }; | ||
} | ||
if (!onValid(value)) { | ||
var checkStatus = onValid(value); | ||
if (typeof checkStatus === 'boolean' && !checkStatus) { | ||
return { hasError: true, errorMessage: errorMessage }; | ||
} else if ((typeof checkStatus === 'undefined' ? 'undefined' : _typeof(checkStatus)) === 'object') { | ||
return checkStatus; | ||
} | ||
} | ||
return { hasError: false }; | ||
}; | ||
Type.prototype.addValidator = function addValidator(onValid, errorMessage) { | ||
errorMessage = errorMessage || this.validators[0].errorMessage; | ||
this.validators.push({ onValid: onValid, errorMessage: errorMessage }); | ||
Type.prototype.addRule = function addRule(onValid, errorMessage) { | ||
errorMessage = errorMessage || this.rules[0].errorMessage; | ||
this.rules.push({ | ||
onValid: onValid, | ||
errorMessage: errorMessage | ||
}); | ||
}; | ||
@@ -45,16 +79,3 @@ | ||
this.required = true; | ||
this.addValidator(function (value) { | ||
//String trim | ||
if (typeof value === 'string') { | ||
value = value.replace(/(^\s*)|(\s*$)/g, ''); | ||
} | ||
//String/Array length > 0 | ||
if (value && value.length && value.length > 0) { | ||
return true; | ||
} | ||
return typeof value !== 'undefined' && value !== null && value !== ''; | ||
}, errorMessage); | ||
this.requiredMessage = errorMessage; | ||
return this; | ||
@@ -61,0 +82,0 @@ }; |
{ | ||
"name": "rsuite-schema", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Schema for data modeling & validation", | ||
@@ -10,3 +10,3 @@ "main": "lib/index.js", | ||
"test": "npm run build && mocha", | ||
"tdd": "mocha" | ||
"tdd": "mocha --compilers js:babel-register test/*Spec.js" | ||
}, | ||
@@ -33,2 +33,3 @@ "repository": { | ||
"babel-preset-stage-0": "^6.5.0", | ||
"babel-register": "^6.24.0", | ||
"chai": "^3.5.0", | ||
@@ -35,0 +36,0 @@ "mocha": "^2.5.3" |
# rsuite-schema | ||
Schema for data modeling & validation | ||
> Currently under development. API may change in future. | ||
数据建模及数据验证 | ||
## 安装 | ||
``` | ||
npm install rsuite-schema --save | ||
``` | ||
## 示例 | ||
```js | ||
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` 返回结构是: | ||
```js | ||
{ | ||
username: { hasError: false }, | ||
email: { hasError: false }, | ||
age: { hasError: true, errorMessage: '年应该在 18 到 30 岁' } | ||
} | ||
``` | ||
## 自定义验证 | ||
```js | ||
const myModel = SchemaModel({ | ||
field1: StringType().addRule((value) => { | ||
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}$/, '请输入合法字符') | ||
}); | ||
``` | ||
## API | ||
### StringType | ||
- isEmail(String errorMessage) | ||
- isURL(String errorMessage) | ||
- isOneOf(String errorMessage) | ||
- containsLetter(String errorMessage) | ||
- containsUppercaseLetter(String errorMessage) | ||
- containsLowercaseLetter(String errorMessage) | ||
- containsLetterOnly(String errorMessage) | ||
- containsNumber(String errorMessage) | ||
- pattern(Object regexp, String errorMessage) | ||
- rangeLength(Number minLength, Number maxLength, String errorMessage) | ||
- minLength(Number minLength, String errorMessage) | ||
- maxLength(Number maxLength, String errorMessage) | ||
### NumbserType | ||
- isInteger(String errorMessage) | ||
- isOneOf(String errorMessage) | ||
- pattern(Object regexp, String errorMessage) | ||
- range(Number minLength, Number maxLength, String errorMessage) | ||
- min(Number min, String errorMessage) | ||
- max(Number min, String errorMessage) | ||
### ArrayType | ||
- rangeLength(Number minLength, Number maxLength, String errorMessage) | ||
- minLength(Number minLength, String errorMessage) | ||
- maxLength(Number maxLength, String errorMessage) | ||
- unrepeatable(String errorMessage) | ||
- shape(Object type, String errorMessage) | ||
### DateType | ||
- range(Date min, Date max, String errorMessage) | ||
- min(Date min, String errorMessage) | ||
- max(Date max, String errorMessage) | ||
38859
18
706
93
8