easy-object-validator
Advanced tools
Comparing version
# CHANGELOG | ||
## 1.0.1 | ||
## 1.0.2 | ||
@@ -5,0 +5,0 @@ > 2018.08.27 |
/** | ||
* easy-object-valodator version 1.0.0 | ||
* easy-object-valodator version 1.0.2 | ||
*/ | ||
@@ -19,3 +19,3 @@ (function (global, factory) { | ||
if (!this instanceof Validate) { | ||
if (!(this instanceof Validate)) { | ||
throw new Error('Validate should be called with the `new` keyword'); | ||
@@ -28,5 +28,10 @@ } | ||
// 最后执行所有校验规则 | ||
this.$doValidate = function (value) { | ||
this.doValidate = function (value) { | ||
_this.value = value; | ||
var ret = _this.$validRules.every(function (rule) { | ||
if (Validate.type(rule) === 'array') { | ||
return rule.some(function (validate) { | ||
return validate.doValidate(value); | ||
}); | ||
} | ||
return rule(); | ||
@@ -118,2 +123,43 @@ }); | ||
return this; | ||
}, | ||
// 校验数组的元素 | ||
arrayOf: function arrayOf(validate) { | ||
var _this6 = this; | ||
if (!(validate instanceof Validate)) { | ||
throw new TypeError('The parameter must be a instance of Validate'); | ||
} | ||
this.$validRules.push(function () { | ||
return type(_this6.value) === 'array' && _this6.value.every(function (item) { | ||
return validate.doValidate(item); | ||
}); | ||
}); | ||
return this; | ||
}, | ||
// 多个校验规则,能匹配到一个就算匹配成功 | ||
oneOf: function oneOf() { | ||
for (var _len = arguments.length, validators = Array(_len), _key = 0; _key < _len; _key++) { | ||
validators[_key] = arguments[_key]; | ||
} | ||
var valid = validators.every(function (validate) { | ||
return validate instanceof Validate; | ||
}); | ||
if (!valid) { | ||
throw new TypeError('The parameter must be a instance of Validate'); | ||
} | ||
this.$validRules.push(validators); | ||
return this; | ||
}, | ||
// 重置校验规则 | ||
reset: function reset() { | ||
this.$validRules = []; | ||
this.$flag = false; | ||
return this; | ||
} | ||
@@ -137,3 +183,3 @@ }; | ||
// 执行校验规则 | ||
return validate.$doValidate(value); | ||
return validate.doValidate(value); | ||
} else if (Validate_1.type(validate) === 'object') { | ||
@@ -155,3 +201,3 @@ // 如果是对象,递归校验 | ||
// 继承保留字段 | ||
var extendReservedKeyword = ['extend', 'arguments', 'caller', 'length', 'prototype', 'apply', 'bind', 'call', 'toString', 'toLocaleString', 'name', 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'valueOf', '$flag', 'validRules', '$doValidate', 'value']; | ||
var extendReservedKeyword = ['extend', 'arguments', 'caller', 'length', 'prototype', 'apply', 'bind', 'call', 'toString', 'toLocaleString', 'name', 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'valueOf', '$flag', '$validRules', 'doValidate', 'value']; | ||
// 继承,用于自定义校验方法 | ||
@@ -166,3 +212,3 @@ validator.extend = function (options) { | ||
if (Validate_1.type(validateFn) !== 'function') { | ||
throw new TypeError('validateFn must be a function'); | ||
throw new TypeError('The validate func must be a function'); | ||
} | ||
@@ -219,3 +265,14 @@ | ||
}; | ||
validator.arrayOf = function (validate) { | ||
return new Validate_1().arrayOf(validate); | ||
}; | ||
validator.oneOf = function () { | ||
var _ref; | ||
return (_ref = new Validate_1()).oneOf.apply(_ref, arguments); | ||
}; | ||
validator.reset = function () { | ||
return new Validate_1().reset(); | ||
}; | ||
var src = validator; | ||
@@ -222,0 +279,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.validator=e()}(this,function(){"use strict";function n(t){return Object.prototype.toString.call(t).slice(0,-1).slice(8).toLowerCase()}function t(){var n=this;if(!this instanceof t)throw new Error("Validate should be called with the `new` keyword");this.$validRules=[],this.$flag=!1,this.$doValidate=function(t){n.value=t;var e=n.$validRules.every(function(t){return t()});return n.$flag?!e:e}}t.type=n,t.prototype={string:function(){return this.is("string")},number:function(){return this.is("number")},object:function(){return this.is("object")},array:function(){return this.is("array")},boolean:function(){return this.is("boolean")},isRequire:function(){var t=this;return this.$validRules.push(function(){return null!=t.value&&""!==t.value}),this},length:function(t){var e=this;return this.$validRules.push(function(){return e.value&&e.value.length===t}),this},test:function(t){var e=this;return this.$validRules.push(function(){return t.test(e.value)}),this},is:function(t){var e=this;return this.$validRules.push(function(){return n(e.value)===t}),this},not:function(){return this.$flag=!this.$flag,this}};var u=t;function o(r,i){var t=!1;return"object"===u.type(r)&&(t=Object.keys(i).every(function(t){var e=r[t],n=i[t];if(n instanceof u)return n.$doValidate(e);if("object"===u.type(n))return o(e,n);throw new TypeError("The validator of "+t+" is not a right validator")})),t}var r=["extend","arguments","caller","length","prototype","apply","bind","call","toString","toLocaleString","name","constructor","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","valueOf","$flag","validRules","$doValidate","value"];return o.extend=function(n){Object.keys(n).forEach(function(t){if(r.includes(t))throw new Error("The arrt `"+t+"` is reserved keyword");var e=n[t];if("function"!==u.type(e))throw new TypeError("validateFn must be a function");Object.defineProperty(u.prototype,t,{value:function(){var t=this;return this.$validRules.push(function(){return e(t.value)}),this},configurable:!0,enumerable:!1}),o[t]=function(){return(new u)[t]()}})},o.string=function(){return(new u).string()},o.number=function(){return(new u).number()},o.object=function(){return(new u).object()},o.array=function(){return(new u).array()},o.boolean=function(){return(new u).boolean()},o.isRequire=function(){return(new u).isRequire()},o.test=function(t){return(new u).test(t)},o.is=function(t){return(new u).is(t)},o.not=function(){return(new u).not()},o}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.validator=e()}(this,function(){"use strict";function n(t){return Object.prototype.toString.call(t).slice(0,-1).slice(8).toLowerCase()}function r(){var n=this;if(!(this instanceof r))throw new Error("Validate should be called with the `new` keyword");this.$validRules=[],this.$flag=!1,this.doValidate=function(e){n.value=e;var t=n.$validRules.every(function(t){return"array"===r.type(t)?t.some(function(t){return t.doValidate(e)}):t()});return n.$flag?!t:t}}r.type=n,r.prototype={string:function(){return this.is("string")},number:function(){return this.is("number")},object:function(){return this.is("object")},array:function(){return this.is("array")},boolean:function(){return this.is("boolean")},isRequire:function(){var t=this;return this.$validRules.push(function(){return null!=t.value&&""!==t.value}),this},length:function(t){var e=this;return this.$validRules.push(function(){return e.value&&e.value.length===t}),this},test:function(t){var e=this;return this.$validRules.push(function(){return t.test(e.value)}),this},is:function(t){var e=this;return this.$validRules.push(function(){return n(e.value)===t}),this},not:function(){return this.$flag=!this.$flag,this},arrayOf:function(e){var t=this;if(!(e instanceof r))throw new TypeError("The parameter must be a instance of Validate");return this.$validRules.push(function(){return"array"===n(t.value)&&t.value.every(function(t){return e.doValidate(t)})}),this},oneOf:function(){for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];if(!e.every(function(t){return t instanceof r}))throw new TypeError("The parameter must be a instance of Validate");return this.$validRules.push(e),this},reset:function(){return this.$validRules=[],this.$flag=!1,this}};var u=r;function o(r,i){var t=!1;return"object"===u.type(r)&&(t=Object.keys(i).every(function(t){var e=r[t],n=i[t];if(n instanceof u)return n.doValidate(e);if("object"===u.type(n))return o(e,n);throw new TypeError("The validator of "+t+" is not a right validator")})),t}var i=["extend","arguments","caller","length","prototype","apply","bind","call","toString","toLocaleString","name","constructor","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","valueOf","$flag","$validRules","doValidate","value"];return o.extend=function(n){Object.keys(n).forEach(function(t){if(i.includes(t))throw new Error("The arrt `"+t+"` is reserved keyword");var e=n[t];if("function"!==u.type(e))throw new TypeError("The validate func must be a function");Object.defineProperty(u.prototype,t,{value:function(){var t=this;return this.$validRules.push(function(){return e(t.value)}),this},configurable:!0,enumerable:!1}),o[t]=function(){return(new u)[t]()}})},o.string=function(){return(new u).string()},o.number=function(){return(new u).number()},o.object=function(){return(new u).object()},o.array=function(){return(new u).array()},o.boolean=function(){return(new u).boolean()},o.isRequire=function(){return(new u).isRequire()},o.test=function(t){return(new u).test(t)},o.is=function(t){return(new u).is(t)},o.not=function(){return(new u).not()},o.arrayOf=function(t){return(new u).arrayOf(t)},o.oneOf=function(){var t;return(t=new u).oneOf.apply(t,arguments)},o.reset=function(){return(new u).reset()},o}); |
{ | ||
"name": "easy-object-validator", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "深度校验一个对象的属性值是否合法", | ||
@@ -5,0 +5,0 @@ "main": "dist/validator.js", |
33174
7.68%685
8.04%