Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rsuite-schema

Package Overview
Dependencies
Maintainers
2
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rsuite-schema - npm Package Compare versions

Comparing version 0.0.6 to 0.0.7

.coveralls.yml

138

lib/ArrayType.js
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});

@@ -22,91 +22,91 @@

var ArrayType = function (_Type) {
_inherits(ArrayType, _Type);
_inherits(ArrayType, _Type);
ArrayType.from = function from(n) {
return n;
};
ArrayType.from = function from(n) {
return n;
};
function ArrayType() {
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'Please enter a valid array' : arguments[0];
function ArrayType() {
var errorMessage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Please enter a valid array';
_classCallCheck(this, ArrayType);
_classCallCheck(this, ArrayType);
var _this = _possibleConstructorReturn(this, _Type.call(this, 'array'));
var _this = _possibleConstructorReturn(this, _Type.call(this, 'array'));
_Type.prototype.addRule.call(_this, function (v) {
return Array.isArray(v);
}, errorMessage);
return _this;
}
_Type.prototype.addRule.call(_this, function (v) {
return Array.isArray(v);
}, errorMessage);
return _this;
}
ArrayType.prototype.rangeLength = function rangeLength(minLength, maxLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length >= minLength && value.length <= maxLength;
}, errorMessage);
return this;
};
ArrayType.prototype.rangeLength = function rangeLength(minLength, maxLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length >= minLength && value.length <= maxLength;
}, errorMessage);
return this;
};
ArrayType.prototype.minLength = function minLength(_minLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length >= _minLength;
}, errorMessage);
return this;
};
ArrayType.prototype.minLength = function minLength(_minLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length >= _minLength;
}, errorMessage);
return this;
};
ArrayType.prototype.maxLength = function maxLength(_maxLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length <= _maxLength;
}, errorMessage);
return this;
};
ArrayType.prototype.maxLength = function maxLength(_maxLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length <= _maxLength;
}, errorMessage);
return this;
};
ArrayType.prototype.unrepeatable = function unrepeatable(errorMessage) {
_Type.prototype.addRule.call(this, function (items) {
var hash = {};
for (var i in items) {
if (hash[items[i]]) {
return false;
}
hash[items[i]] = true;
}
return true;
}, errorMessage);
return this;
};
ArrayType.prototype.unrepeatable = function unrepeatable(errorMessage) {
_Type.prototype.addRule.call(this, function (items) {
var hash = {};
for (var i in items) {
if (hash[items[i]]) {
return false;
}
hash[items[i]] = true;
}
return true;
}, errorMessage);
return this;
};
/**
* @example
* ArrayType('这是一个数组').shape(
* StringType().isOneOf(['数码','体育','游戏','旅途','其他'],
* '只能是选择中的值'
* )
*/
/**
* @example
* ArrayType('这是一个数组').of(
* StringType().isOneOf(['数码','体育','游戏','旅途','其他'],
* '只能是选择中的值'
* )
*/
ArrayType.prototype.shape = function shape(type, errorMessage) {
ArrayType.prototype.of = function of(type, errorMessage) {
_Type.prototype.addRule.call(this, function (items) {
var valids = items.map(function (value) {
return type.check(value);
});
_Type.prototype.addRule.call(this, function (items) {
var valids = items.map(function (value) {
return type.check(value);
});
var errors = valids.filter(function (item) {
return item.hasError;
}) || [];
var errors = valids.filter(function (item) {
return item.hasError;
}) || [];
if (errors.length) {
return errors[0];
}
if (errors.length) {
return errors[0];
}
return errors.length === 0;
}, null);
return errors.length === 0;
}, null);
return this;
};
return this;
};
return ArrayType;
return ArrayType;
}(_Type3.default);
exports.default = function (errorMessage) {
return new ArrayType(errorMessage);
return new ArrayType(errorMessage);
};
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});

@@ -22,26 +22,26 @@

var BooleanType = function (_Type) {
_inherits(BooleanType, _Type);
_inherits(BooleanType, _Type);
BooleanType.from = function from(n) {
return n;
};
BooleanType.from = function from(n) {
return n;
};
function BooleanType() {
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'Please enter a valid `boolean`' : arguments[0];
function BooleanType() {
var errorMessage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Please enter a valid `boolean`';
_classCallCheck(this, BooleanType);
_classCallCheck(this, BooleanType);
var _this = _possibleConstructorReturn(this, _Type.call(this, 'boolean'));
var _this = _possibleConstructorReturn(this, _Type.call(this, 'boolean'));
_Type.prototype.addRule.call(_this, function (v) {
return typeof v === 'boolean';
}, errorMessage);
return _this;
}
_Type.prototype.addRule.call(_this, function (v) {
return typeof v === 'boolean';
}, errorMessage);
return _this;
}
return BooleanType;
return BooleanType;
}(_Type3.default);
exports.default = function (errorMessage) {
return new BooleanType(errorMessage);
return new BooleanType(errorMessage);
};
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});

@@ -22,47 +22,47 @@

var DateType = function (_Type) {
_inherits(DateType, _Type);
_inherits(DateType, _Type);
DateType.from = function from(n) {
return n;
};
DateType.from = function from(n) {
return n;
};
function DateType() {
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'Please enter a valid date' : arguments[0];
function DateType() {
var errorMessage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Please enter a valid date';
_classCallCheck(this, DateType);
_classCallCheck(this, DateType);
var _this = _possibleConstructorReturn(this, _Type.call(this, 'date'));
var _this = _possibleConstructorReturn(this, _Type.call(this, 'date'));
_Type.prototype.addRule.call(_this, function (value) {
return !/Invalid|NaN/.test(new Date(value));
}, errorMessage);
return _this;
}
_Type.prototype.addRule.call(_this, function (value) {
return !/Invalid|NaN/.test(new Date(value));
}, errorMessage);
return _this;
}
DateType.prototype.range = function range(min, max, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return new Date(value) >= new Date(min) && new Date(value) <= new Date(max);
}, errorMessage);
return this;
};
DateType.prototype.range = function range(min, max, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return new Date(value) >= new Date(min) && new Date(value) <= new Date(max);
}, errorMessage);
return this;
};
DateType.prototype.min = function min(_min, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return new Date(value) >= new Date(_min);
}, errorMessage);
return this;
};
DateType.prototype.min = function min(_min, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return new Date(value) >= new Date(_min);
}, errorMessage);
return this;
};
DateType.prototype.max = function max(_max, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return new Date(value) <= new Date(_max);
}, errorMessage);
return this;
};
DateType.prototype.max = function max(_max, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return new Date(value) <= new Date(_max);
}, errorMessage);
return this;
};
return DateType;
return DateType;
}(_Type3.default);
exports.default = function (errorMessage) {
return new DateType(errorMessage);
return new DateType(errorMessage);
};
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});

@@ -22,73 +22,73 @@

function _f(value) {
return +value;
return +value;
}
var NumberType = function (_Type) {
_inherits(NumberType, _Type);
_inherits(NumberType, _Type);
NumberType.from = function from(n) {
return n;
};
NumberType.from = function from(n) {
return n;
};
function NumberType() {
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'Please enter a valid number' : arguments[0];
function NumberType() {
var errorMessage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Please enter a valid number';
_classCallCheck(this, NumberType);
_classCallCheck(this, NumberType);
var _this = _possibleConstructorReturn(this, _Type.call(this, 'number'));
var _this = _possibleConstructorReturn(this, _Type.call(this, 'number'));
_Type.prototype.addRule.call(_this, function (value) {
return (/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value)
);
}, errorMessage);
return _this;
}
_Type.prototype.addRule.call(_this, function (value) {
return (/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value)
);
}, errorMessage);
return _this;
}
NumberType.prototype.isInteger = function isInteger() {
_Type.prototype.addRule.call(this, function (value) {
return (/^-?\d+$/.test(value)
);
}, errorMessage);
};
NumberType.prototype.isInteger = function isInteger() {
_Type.prototype.addRule.call(this, function (value) {
return (/^-?\d+$/.test(value)
);
}, errorMessage);
};
NumberType.prototype.pattern = function pattern(regexp, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return regexp.test(value);
}, errorMessage);
return this;
};
NumberType.prototype.pattern = function pattern(regexp, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return regexp.test(value);
}, errorMessage);
return this;
};
NumberType.prototype.isOneOf = function isOneOf(numLst, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return _f(value) in numLst;
}, errorMessage);
return this;
};
NumberType.prototype.isOneOf = function isOneOf(numLst, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return _f(value) in numLst;
}, errorMessage);
return this;
};
NumberType.prototype.range = function range(min, max, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return _f(value) >= min && _f(value) <= max;
}, errorMessage);
return this;
};
NumberType.prototype.range = function range(min, max, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return _f(value) >= min && _f(value) <= max;
}, errorMessage);
return this;
};
NumberType.prototype.min = function min(_min, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return _f(value) >= _min;
}, errorMessage);
return this;
};
NumberType.prototype.min = function min(_min, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return _f(value) >= _min;
}, errorMessage);
return this;
};
NumberType.prototype.max = function max(_max, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return _f(value) <= _max;
}, errorMessage);
return this;
};
NumberType.prototype.max = function max(_max, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return _f(value) <= _max;
}, errorMessage);
return this;
};
return NumberType;
return NumberType;
}(_Type3.default);
exports.default = function (errorMessage) {
return new NumberType(errorMessage);
return new NumberType(errorMessage);
};
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
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; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

@@ -24,26 +24,59 @@ var _Type2 = require('./Type');

var ObjectType = function (_Type) {
_inherits(ObjectType, _Type);
_inherits(ObjectType, _Type);
ObjectType.from = function from(n) {
return n;
};
ObjectType.from = function from(n) {
return n;
};
function ObjectType() {
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'Please enter a valid `object`' : arguments[0];
function ObjectType() {
var errorMessage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Please enter a valid `object`';
_classCallCheck(this, ObjectType);
_classCallCheck(this, ObjectType);
var _this = _possibleConstructorReturn(this, _Type.call(this, 'object'));
var _this = _possibleConstructorReturn(this, _Type.call(this, 'object'));
_Type.prototype.addRule.call(_this, function (v) {
return (typeof v === 'undefined' ? 'undefined' : _typeof(v)) === 'object';
}, errorMessage);
return _this;
}
_Type.prototype.addRule.call(_this, function (v) {
return (typeof v === 'undefined' ? 'undefined' : _typeof(v)) === 'object';
}, errorMessage);
return _this;
}
return ObjectType;
/**
* @example
* ObjectType('这是一个对象').shape({
* name: StringType(),
* age: NumberType()
* })
*/
ObjectType.prototype.shape = function shape(types) {
_Type.prototype.addRule.call(this, function (values) {
var valids = Object.entries(types).map(function (item) {
var key = item[0];
var type = item[1];
return type.check(values[key]);
});
var errors = valids.filter(function (item) {
return item.hasError;
}) || [];
if (errors.length) {
return errors[0];
}
return errors.length === 0;
}, null);
return this;
};
return ObjectType;
}(_Type3.default);
exports.default = function (errorMessage) {
return new ObjectType(errorMessage);
return new ObjectType(errorMessage);
};
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});

@@ -17,34 +17,34 @@ exports.SchemaModel = exports.Schema = undefined;

var Schema = exports.Schema = function () {
function Schema(schema) {
_classCallCheck(this, Schema);
function Schema(schema) {
_classCallCheck(this, Schema);
this.schema = schema;
}
this.schema = schema;
}
Schema.prototype.getFieldType = function getFieldType(fieldName) {
return this.schema[fieldName] || new _StringType2.default();
};
Schema.prototype.getFieldType = function getFieldType(fieldName) {
return this.schema[fieldName] || new _StringType2.default();
};
Schema.prototype.checkForField = function checkForField(fieldName, fieldValue) {
var fieldChecker = this.schema[fieldName];
if (!fieldChecker) {
return { hasError: false }; // fieldValue can be anything if no schema defined
}
return fieldChecker.check(fieldValue);
};
Schema.prototype.checkForField = function checkForField(fieldName, fieldValue) {
var fieldChecker = this.schema[fieldName];
if (!fieldChecker) {
return { hasError: false }; // fieldValue can be anything if no schema defined
}
return fieldChecker.check(fieldValue);
};
Schema.prototype.check = function check(value, cb) {
var checkResult = {};
for (var fieldName in this.schema) {
var fieldValue = value[fieldName];
checkResult[fieldName] = this.checkForField(fieldName, fieldValue);
}
return checkResult;
};
Schema.prototype.check = function check(value, cb) {
var checkResult = {};
for (var fieldName in this.schema) {
var fieldValue = value[fieldName];
checkResult[fieldName] = this.checkForField(fieldName, fieldValue);
}
return checkResult;
};
return Schema;
return Schema;
}();
var SchemaModel = exports.SchemaModel = function SchemaModel(o) {
return new Schema(o);
return new Schema(o);
};
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});

@@ -22,117 +22,117 @@

var StringType = function (_Type) {
_inherits(StringType, _Type);
_inherits(StringType, _Type);
StringType.from = function from(s) {
return s + '';
};
StringType.from = function from(s) {
return s + '';
};
function StringType() {
var errorMessage = arguments.length <= 0 || arguments[0] === undefined ? 'Please enter a valid string' : arguments[0];
function StringType() {
var errorMessage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Please enter a valid string';
_classCallCheck(this, StringType);
_classCallCheck(this, StringType);
var _this = _possibleConstructorReturn(this, _Type.call(this, 'string'));
var _this = _possibleConstructorReturn(this, _Type.call(this, 'string'));
_Type.prototype.addRule.call(_this, function (v) {
return typeof v === 'string';
}, errorMessage);
return _this;
}
_Type.prototype.addRule.call(_this, function (v) {
return typeof v === 'string';
}, errorMessage);
return _this;
}
StringType.prototype.containsLetter = function containsLetter(errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return (/[a-zA-Z]/.test(v)
);
}, errorMessage);
return this;
};
StringType.prototype.containsLetter = function containsLetter(errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return (/[a-zA-Z]/.test(v)
);
}, errorMessage);
return this;
};
StringType.prototype.containsUppercaseLetter = function containsUppercaseLetter(errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return (/[A-Z]/.test(v)
);
}, errorMessage);
return this;
};
StringType.prototype.containsUppercaseLetter = function containsUppercaseLetter(errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return (/[A-Z]/.test(v)
);
}, errorMessage);
return this;
};
StringType.prototype.containsLowercaseLetter = function containsLowercaseLetter(errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return (/[a-z]/.test(v)
);
}, errorMessage);
return this;
};
StringType.prototype.containsLowercaseLetter = function containsLowercaseLetter(errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return (/[a-z]/.test(v)
);
}, errorMessage);
return this;
};
StringType.prototype.containsLetterOnly = function containsLetterOnly(errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return (/^[a-zA-Z]+$/.test(v)
);
}, errorMessage);
return this;
};
StringType.prototype.containsLetterOnly = function containsLetterOnly(errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return (/^[a-zA-Z]+$/.test(v)
);
}, errorMessage);
return this;
};
StringType.prototype.containsNumber = function containsNumber(errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return (/[0-9]/.test(v)
);
}, errorMessage);
return this;
};
StringType.prototype.containsNumber = function containsNumber(errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return (/[0-9]/.test(v)
);
}, errorMessage);
return this;
};
StringType.prototype.isOneOf = function isOneOf(strArr, errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return ~strArr.indexOf(v);
}, errorMessage);
return this;
};
StringType.prototype.isOneOf = function isOneOf(strArr, errorMessage) {
_Type.prototype.addRule.call(this, function (v) {
return ~strArr.indexOf(v);
}, errorMessage);
return this;
};
StringType.prototype.isEmail = function isEmail(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.addRule.call(this, function (v) {
return re.test(v);
}, errorMessage);
return this;
};
StringType.prototype.isEmail = function isEmail(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.addRule.call(this, function (v) {
return re.test(v);
}, errorMessage);
return this;
};
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.addRule.call(this, function (v) {
return re.test(v);
}, errorMessage);
return this;
};
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.addRule.call(this, function (v) {
return re.test(v);
}, errorMessage);
return this;
};
StringType.prototype.pattern = function pattern(regexp, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return regexp.test(value);
}, errorMessage);
return this;
};
StringType.prototype.pattern = function pattern(regexp, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return regexp.test(value);
}, errorMessage);
return this;
};
StringType.prototype.rangeLength = function rangeLength(minLength, maxLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length >= minLength && value.length <= maxLength;
}, errorMessage);
return this;
};
StringType.prototype.rangeLength = function rangeLength(minLength, maxLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length >= minLength && value.length <= maxLength;
}, errorMessage);
return this;
};
StringType.prototype.minLength = function minLength(_minLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length >= _minLength;
}, errorMessage);
return this;
};
StringType.prototype.minLength = function minLength(_minLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length >= _minLength;
}, errorMessage);
return this;
};
StringType.prototype.maxLength = function maxLength(_maxLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length <= _maxLength;
}, errorMessage);
return this;
};
StringType.prototype.maxLength = function maxLength(_maxLength, errorMessage) {
_Type.prototype.addRule.call(this, function (value) {
return value.length <= _maxLength;
}, errorMessage);
return this;
};
return StringType;
return StringType;
}(_Type3.default);
exports.default = function (errorMessage) {
return new StringType(errorMessage);
return new StringType(errorMessage);
};
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
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; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

@@ -12,75 +12,75 @@ 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 === '';
return typeof value === 'undefined' || value === null || value === '';
}
function checkRequired(value) {
//String trim
if (typeof value === 'string') {
value = value.replace(/(^\s*)|(\s*$)/g, '');
}
//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;
}
//String/Array length > 0
if (value && value.length && value.length > 0) {
return true;
}
return !isEmpty(value);
return !isEmpty(value);
}
var Type = function () {
function Type(name) {
_classCallCheck(this, Type);
function Type(name) {
_classCallCheck(this, Type);
this.name = name;
this.required = false;
this.requiredMessage = '';
this.rules = [];
}
this.name = name;
this.required = false;
this.requiredMessage = '';
this.rules = [];
}
Type.prototype.check = function check(value) {
Type.prototype.check = function check(value) {
if (this.required && !checkRequired(value)) {
return { hasError: true, errorMessage: this.requiredMessage };
}
if (this.required && !checkRequired(value)) {
return { hasError: true, errorMessage: this.requiredMessage };
}
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;
for (var i = 0; i < this.rules.length; i++) {
var _rules$i = this.rules[i],
onValid = _rules$i.onValid,
errorMessage = _rules$i.errorMessage;
if (!this.required && isEmpty(value)) {
return { hasError: false };
}
if (!this.required && isEmpty(value)) {
return { hasError: false };
}
var checkStatus = 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;
}
}
if (typeof checkStatus === 'boolean' && !checkStatus) {
return { hasError: true, errorMessage: errorMessage };
} else if ((typeof checkStatus === 'undefined' ? 'undefined' : _typeof(checkStatus)) === 'object') {
return checkStatus;
}
}
return { hasError: false };
};
return { hasError: false };
};
Type.prototype.addRule = function addRule(onValid, errorMessage) {
errorMessage = errorMessage || this.rules[0].errorMessage;
this.rules.push({
onValid: onValid,
errorMessage: errorMessage
});
return this;
};
Type.prototype.addRule = function addRule(onValid, errorMessage) {
errorMessage = errorMessage || this.rules[0].errorMessage;
this.rules.push({
onValid: onValid,
errorMessage: errorMessage
});
return this;
};
Type.prototype.isRequired = function isRequired(errorMessage) {
this.required = true;
this.requiredMessage = errorMessage;
return this;
};
Type.prototype.isRequired = function isRequired(errorMessage) {
this.required = true;
this.requiredMessage = errorMessage;
return this;
};
return Type;
return Type;
}();
exports.default = Type;
{
"name": "rsuite-schema",
"version": "0.0.6",
"version": "0.0.7",
"description": "Schema for data modeling & validation",
"main": "lib/index.js",
"scripts": {
"lint": "eslint src *.js",
"build": "./node_modules/.bin/babel -d lib/ src/",
"prepublish": "npm run build",
"test": "npm run build && mocha",
"test": "npm run lint && npm run build && mocha",
"tdd": "mocha --compilers js:babel-register test/*Spec.js"

@@ -20,3 +21,6 @@ },

],
"author": "A2ZH",
"contributors": [
"A2ZH",
"Simon Guo <simonguo.2009@gmail.com>"
],
"license": "MIT",

@@ -29,2 +33,3 @@ "bugs": {

"babel-cli": "^6.11.4",
"babel-eslint": "^7.2.3",
"babel-plugin-transform-es2015-classes": "^6.14.0",

@@ -36,4 +41,10 @@ "babel-plugin-transform-proto-to-assign": "^6.9.0",

"chai": "^3.5.0",
"coveralls": "^2.13.1",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^15.0.1",
"eslint-plugin-babel": "^3.2.0",
"eslint-plugin-jsx-a11y": "^6.0.2",
"istanbul": "^0.4.5",
"mocha": "^2.5.3"
}
}

@@ -5,2 +5,8 @@ # rsuite-schema

版本与状态
[![npm][npm-badge]][npm]
[![Travis][build-badge]][build]
## 安装

@@ -60,48 +66,65 @@

- isRequired()
- 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)
- addRule(onValid, errorMessage)
- 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)
- addRule(Function: onValid, String: errorMessage)
### NumbserType
- isRequired()
- 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)
- addRule(onValid, errorMessage)
- 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)
- addRule(Function: onValid, String: errorMessage)
### ArrayType
- isRequired()
- 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)
- addRule(onValid, errorMessage)
- rangeLength(Number: minLength, Number: maxLength, String: errorMessage)
- minLength(Number: minLength, String: errorMessage)
- maxLength(Number: maxLength, String: errorMessage)
- unrepeatable(String: errorMessage)
- of(Object: type, String: errorMessage)
- addRule(Function: onValid, String: errorMessage)
### DateType
- isRequired()
- range(Date min, Date max, String errorMessage)
- min(Date min, String errorMessage)
- max(Date max, String errorMessage)
- addRule(onValid, errorMessage)
- range(Date: min, Date: max, String: errorMessage)
- min(Date: min, String: errorMessage)
- max(Date: max, String: errorMessage)
- addRule(Function: onValid, String: errorMessage)
### ObjectType
- isRequired()
- addRule(onValid, errorMessage)
- addRule(Function: onValid, String: errorMessage)
- shape(Object: types)
### BooleanType
- isRequired()
- addRule(onValid, errorMessage)
- addRule(Function: onValid, String: errorMessage)
[npm-badge]: https://img.shields.io/npm/v/rsuite-schema.svg
[npm]: https://www.npmjs.com/package/rsuite-schema
[npm-beta-badge]: https://img.shields.io/npm/v/rsuite-schema/beta.svg
[npm-beta]: https://www.npmjs.com/package/rsuite-schema
[build-badge]: https://travis-ci.org/rsuite/rsuite-schema.svg
[build]: https://travis-ci.org/rsuite/rsuite-schema
[coverage-badge]: https://coveralls.io/repos/github/rsuite/rsuite-schema/badge.svg?branch=next
[coverage]: https://coveralls.io/github/rsuite/rsuite-schema

@@ -7,23 +7,24 @@ const should = require('chai').should();

let schemaData1 = { data: ArrayType().shape(StringType().isEmail('应该是一个 email'), '格式错误') };
let schemaData2 = { data: ArrayType().shape(StringType().isEmail(), '格式错误') };
let schemaData1 = { data: ArrayType().of(StringType().isEmail('应该是一个 email'), '格式错误') };
let schemaData2 = { data: ArrayType().of(StringType().isEmail(), '格式错误') };
it('Should be a valid array', () => {
it('Should be a valid array', () => {
let schema = new Schema(schemaData1);
let schema2 = new Schema(schemaData2);
schema.checkForField('data', ['simon.guo@hypers.com', 'ddddd@d.com', 'ddd@bbb.com']).hasError.should.equal(false);
let checkStatus = schema.checkForField('data', ['simon.guo@hypers.com', 'ddddd', 'ddd@bbb.com']);
let schema = new Schema(schemaData1);
let schema2 = new Schema(schemaData2);
checkStatus.hasError.should.equal(true);
checkStatus.errorMessage.should.equal('应该是一个 email');
schema.checkForField('data', ['simon.guo@hypers.com', 'ddddd@d.com', 'ddd@bbb.com']).hasError.should.equal(false);
let checkStatus = schema.checkForField('data', ['simon.guo@hypers.com', 'ddddd', 'ddd@bbb.com']);
let checkStatus2 = schema2.checkForField('data', ['simon.guo@hypers.com', 'ddddd', 'ddd@bbb.com']);
checkStatus.hasError.should.equal(true);
checkStatus.errorMessage.should.equal('应该是一个 email');
checkStatus2.hasError.should.equal(true);
checkStatus2.errorMessage.should.equal('Please enter a valid string');
let checkStatus2 = schema2.checkForField('data', ['simon.guo@hypers.com', 'ddddd', 'ddd@bbb.com']);
});
checkStatus2.hasError.should.equal(true);
checkStatus2.errorMessage.should.equal('Please enter a valid string');
});
});

@@ -7,43 +7,43 @@ const should = require('chai').should();

let schemaData = { data: NumberType() };
let schema = new Schema(schemaData);
let schemaData = { data: NumberType() };
let schema = new Schema(schemaData);
it('Should be a valid number', () => {
schema.checkForField('data', "2.22").hasError.should.equal(false);
schema.checkForField('data', 2.22).hasError.should.equal(false);
schema.checkForField('data', 2.).hasError.should.equal(false);
schema.checkForField('data', -222).hasError.should.equal(false);
});
it('Should be a valid number', () => {
schema.checkForField('data', "2.22").hasError.should.equal(false);
schema.checkForField('data', 2.22).hasError.should.equal(false);
schema.checkForField('data', 2.).hasError.should.equal(false);
schema.checkForField('data', -222).hasError.should.equal(false);
});
it('Should not be checked', () => {
schema.checkForField('data', null).hasError.should.equal(false);
schema.checkForField('data', undefined).hasError.should.equal(false);
schema.checkForField('data', '').hasError.should.equal(false);
});
it('Should not be checked', () => {
schema.checkForField('data', null).hasError.should.equal(false);
schema.checkForField('data', undefined).hasError.should.equal(false);
schema.checkForField('data', '').hasError.should.equal(false);
});
it('Should be a invalid number', () => {
schema.checkForField('data', "abc").hasError.should.equal(true);
schema.checkForField('data', '1abc').hasError.should.equal(true);
schema.checkForField('data', {}).hasError.should.equal(true);
schema.checkForField('data', []).hasError.should.equal(true);
});
it('Should be a invalid number', () => {
schema.checkForField('data', "abc").hasError.should.equal(true);
schema.checkForField('data', '1abc').hasError.should.equal(true);
schema.checkForField('data', {}).hasError.should.equal(true);
schema.checkForField('data', []).hasError.should.equal(true);
});
it('True should be a invalid number', () => {
schema.checkForField('data', true).hasError.should.equal(true);
});
it('True should be a invalid number', () => {
schema.checkForField('data', true).hasError.should.equal(true);
});
it('Function should be a invalid number', () => {
schema.checkForField('data', function () { }).hasError.should.equal(true);
});
it('Function should be a invalid number', () => {
schema.checkForField('data', function () { }).hasError.should.equal(true);
});
it('Null and Undefined should be a invalid number', () => {
let schemaData = { data: NumberType().isRequired() };
let schema = new Schema(schemaData);
schema.checkForField('data', null).hasError.should.equal(true);
schema.checkForField('data', undefined).hasError.should.equal(true);
});
it('Null and Undefined should be a invalid number', () => {
let schemaData = { data: NumberType().isRequired() };
let schema = new Schema(schemaData);
schema.checkForField('data', null).hasError.should.equal(true);
schema.checkForField('data', undefined).hasError.should.equal(true);
});
});

@@ -8,45 +8,42 @@ const should = require('chai').should();

it('save Schema as proporty', () => {
let schemaData = { data: StringType() };
let schema = new Schema(schemaData);
schema.schema.should.equal(schemaData);
});
it('save Schema as proporty', () => {
let schemaData = { data: StringType() };
let schema = new Schema(schemaData);
schema.schema.should.equal(schemaData);
});
it('get field value type of given field name', () => {
let schemaData = { data: NumberType() };
let schema = new Schema(schemaData);
schema.getFieldType('data').should.equal(schemaData.data);
});
it('get field value type of given field name', () => {
let schemaData = { data: NumberType() };
let schema = new Schema(schemaData);
schema.getFieldType('data').should.equal(schemaData.data);
});
it('should return error information', () => {
let schemaData = { data: NumberType() };
let schema = new Schema(schemaData);
let checkResult = schema.checkForField('data', "2.22");
it('should return error information', () => {
let schemaData = { data: NumberType() };
let schema = new Schema(schemaData);
let checkResult = schema.checkForField('data', "2.22");
checkResult.should.have.property('hasError').be.a('boolean');
});
checkResult.should.have.property('hasError').be.a('boolean');
});
it('should return error information', () => {
it('should return error information', () => {
const userModel = SchemaModel({
username: StringType().isRequired('用户名不能为空'),
email: StringType().isEmail('请输入正确的邮箱'),
age: NumberType('年龄应该是一个数字').range(18, 30, '年应该在 18 到 30 岁')
});
const userModel = SchemaModel({
username: StringType().isRequired('用户名不能为空'),
email: StringType().isEmail('请输入正确的邮箱'),
age: NumberType('年龄应该是一个数字').range(18, 30, '年应该在 18 到 30 岁')
});
const checkStatus = userModel.check({
username: 'foobar',
email: 'foo@bar.com',
age: 40
});
const checkStatus = userModel.check({
username: 'foobar',
email: 'foo@bar.com',
age: 40
});
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc