-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema-validate.min.js
11 lines (11 loc) · 9.39 KB
/
schema-validate.min.js
1
2
3
4
5
6
7
8
9
10
11
/*! @luobotang/schema-validate v0.1.6 */
var SchemaValidate=function(t){"use strict";function u(t){return null==t||""===t}function r(t,r){this.name=t,this._desc=r||"",this._required=!1,this._requiredMessage="",this._trim=!1,this._rules=[],this._priorityRules=[]}r.prototype.check=function(t,r){if(this._required&&(e=t,this.trim&&"string"==typeof e&&(e=e.replace(/(^\s*)|(\s*$)/g,"")),!(Array.isArray(e)?e.length:!u(e))))return{hasError:!0,errorMessage:this._desc+this._requiredMessage};var e,c,s,n=(c=r,s=this._desc,function(t,r){for(var e=0;e<r.length;e+=1){var n=r[e],o=n.onValid,u=n.errorMessage,i=o(t,c);if(!1===i)return{hasError:!0,errorMessage:s+u};if("object"==typeof i&&i.hasError)return i}return null}),o=n(t,this._priorityRules);return o||((this._required||!u(t))&&n(t,this._rules)||{hasError:!1})},r.prototype.rule=function(t,r,e){return e?this._priorityRules.push({onValid:t,errorMessage:r}):this._rules.push({onValid:t,errorMessage:r}),this},r.prototype.required=function(t,r){return void 0===t&&(t="不能为空"),void 0===r&&(r=!0),
this._required=!0,this._trim=r,this._requiredMessage=t,this},r.prototype.desc=function(t){return this._desc=t||"",this},r.prototype.clone=function(){var t=this,r={};return Object.setPrototypeOf(r,Object.getPrototypeOf(t)),r.name=t.name,r._desc=t._desc,r._required=t._required,r._requiredMessage=t._requiredMessage,r._trim=t._trim,r._rules=t._rules.concat(),r._priorityRules=t._priorityRules.concat(),r};function e(t){return new f(t)}function n(t){this.schema=t}var o=r,i=/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,c=/^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$/,s=new RegExp(
"^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-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,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$","i"),f=function(r){function t(t){r.call(this,"string",t),this.rule(function(t){return"string"==typeof t},"应为字符串")}return r&&(t.__proto__=r),((t.prototype=Object.create(r&&r.prototype)).constructor=t).prototype.hasLetter=function(t){return this.rule(function(t){return/[a-zA-Z]/.test(t)},t||"应包含字母")},t.prototype.hasUppercaseLetter=function(t){return this.rule(function(t){return/[A-Z]/.test(t)},t||"应包含大写字母")},t.prototype.hasLowercaseLetter=function(t){return this.rule(function(t){return/[a-z]/.test(t)},t||"应包含小写字母")},t.prototype.letterOnly=function(t){return this.rule(function(t){
return/^[a-zA-Z]+$/.test(t)},t||"只能是字母")},t.prototype.hasNumber=function(t){return this.rule(function(t){return/[0-9]/.test(t)},t||"应包含数字")},t.prototype.numberOnly=function(t){return this.rule(function(t){return/^\d+$/.test(t)},t||"只能是数字")},t.prototype.isInteger=function(t){return this.rule(function(t){return/^-?\d+$/.test(t)},t||"应为整数")},t.prototype.isNumber=function(t){return this.rule(function(t){return/^-?\d+(\.\d+)?$/.test(t)},t||"应为数值")},t.prototype.isFloat=function(t){return this.rule(function(t){return/^-?\d+\.\d+$/.test(t)},t||"应为 Float")},t.prototype.isDouble=function(t){return this.isFloat(t||"应为 Double")},t.prototype.oneOf=function(r,t){return this.rule(function(t){return-1!==r.indexOf(t)},t||"非指定值")},t.prototype.email=function(t){return this.rule(function(t){return i.test(t)},t||"应为 Email")},t.prototype.ip=function(t){return this.rule(function(t){return c.test(t)},t||"应为 IP 地址")},t.prototype.url=function(t){return this.rule(function(t){return s.test(t)},t||"应为 URL")},
t.prototype.hex=function(t){return this.rule(function(t){return/^#?([a-f0-9]{6}|[a-f0-9]{3})$/i.test(t)},t||"应为十六进制颜色")},t.prototype.pattern=function(r,t){return this.rule(function(t){return r.test(t)},t||"格式错误")},t.prototype.range=function(r,e,t){return this.rule(function(t){return t.length>=r&&t.length<=e},t||"长度应在 "+r+" 到 "+e+" 字符之间")},t.prototype.minlen=function(r,t){return this.rule(function(t){return t.length>=r},t||"长度不小于 "+r+" 字符")},t.prototype.maxlen=function(r,t){return this.rule(function(t){return t.length<=r},t||"长度不大于 "+r+" 字符")},t.prototype.length=function(r,t){return this.rule(function(t){return t.length===r},t||"长度应为 "+r+" 字符")},t.prototype.same=function(e,t){return this.rule(function(t,r){return!!r&&r[e]===t},t||"值应与字段 "+e+" 一致")},t}(o);n.prototype.getFieldType=function(t){return this.schema[t]||new e},n.prototype.getKeys=function(){return Object.keys(this.schema)},n.prototype.checkForField=function(t,r,e){var n=this.schema[t];return n?n.check(r,e):{hasError:!1}},
n.prototype.check=function(r){var e=this,n={};return Object.keys(this.schema).forEach(function(t){n[t]=e.checkForField(t,r[t],r)}),n},n.prototype.validate=function(t){for(var r=0,e=Object.keys(this.schema);r<e.length;r+=1){var n=e[r],o=this.checkForField(n,t[n],t);if(o.hasError)return o.field=n,o}return{hasError:!1}};function p(t){return new n(t)}var a=n;p.combine=function(){for(var t=[],r=arguments.length;r--;)t[r]=arguments[r];return new n(t.map(function(t){return t.schema}).reduce(function(t,r){return Object.assign(t,r)},{}))};var h={Schema:a,SchemaModel:p},l=function(r){function t(t){r.call(this,"number",t),this.rule(function(t){return"number"==typeof t},"应为数值")}return r&&(t.__proto__=r),((t.prototype=Object.create(r&&r.prototype)).constructor=t).prototype.integer=function(t){return this.rule(function(t){return-1===t.toString().indexOf(".")},t||"应为整数")},t.prototype.range=function(r,e,t){return this.rule(function(t){return r<=t&&t<=e},t||"应在 "+r+" 到 "+e+" 之间")},
t.prototype.min=function(r,t){return this.rule(function(t){return r<=t},t||"应不小于 "+r)},t.prototype.max=function(r,t){return this.rule(function(t){return t<=r},t||"应不大于 "+r)},t.prototype.oneOf=function(r,t){return this.rule(function(t){return-1!==r.indexOf(t)},t||"非指定值")},t}(o),y=function(r){function t(t){r.call(this,"boolean",t),this.rule(function(t){return"boolean"==typeof t},"应为布尔值")}return r&&(t.__proto__=r),(t.prototype=Object.create(r&&r.prototype)).constructor=t}(o),d=function(r){function t(t){r.call(this,"date",t),this.rule(function(t){return!/Invalid|NaN/.test(new Date(t))},"应为日期")}return r&&(t.__proto__=r),((t.prototype=Object.create(r&&r.prototype)).constructor=t).prototype.range=function(r,e,t){return this.rule(function(t){return new Date(t)>=new Date(r)&&new Date(t)<=new Date(e)},t||"应在 "+r.toString()+" 到 "+e.toString()+" 之间")},t.prototype.min=function(r,t){return this.rule(function(t){return new Date(t)>=new Date(r)},t||"应不小于 "+r.toString())},t.prototype.max=function(r,t){
return this.rule(function(t){return new Date(t)<=new Date(r)},t||"应不大于 "+r.toString())},t.prototype.strict=function(t){return this.rule(function(t){return"[object Date]"===Object.prototype.toString.call(t)},t||"应为日期类型")},t}(o),_=function(r){function t(t){r.call(this,"object",t),this.rule(function(t){return"object"==typeof t},"应为对象")}return r&&(t.__proto__=r),((t.prototype=Object.create(r&&r.prototype)).constructor=t).prototype.shape=function(r){return this.rule(function(e){var t=Object.entries(r).map(function(t){var r=t[0];return t[1].check(e[r],e)}).filter(function(t){return t.hasError})||[];return t.length?t[0]:0===t.length},null)},t}(o),g=function(r){function t(t){r.call(this,"array",t),this.rule(function(t){return Array.isArray(t)},"应为数组")}return r&&(t.__proto__=r),((t.prototype=Object.create(r&&r.prototype)).constructor=t).prototype.range=function(r,e,t){return this.rule(function(t){return t.length>=r&&t.length<=e},t||"长度应在 "+r+" 到 "+e+" 之间")},t.prototype.minlen=function(r,t){
return this.rule(function(t){return t.length>=r},t||"长度不小于 "+r)},t.prototype.maxlen=function(r,t){return this.rule(function(t){return t.length<=r},t||"长度不大于 "+r)},t.prototype.length=function(r,t){return this.rule(function(t){return t.length===r},t||"长度应为 "+r)},t.prototype.unique=function(t){return this.rule(function(t){for(var r={},e=0,n=t;e<n.length;e+=1){var o=n[e];if(r[o])return!1;r[o]=!0}return!0},t||"不能重复")},t.prototype.uniqueKey=function(u,t){return this.rule(function(t){for(var r={},e=0,n=t;e<n.length;e+=1){var o=n[e][u];if(r[o])return!1;r[o]=!0}return!0},t||"不能重复")},t.prototype.of=function(i){return this.rule(function(t,r){for(var e=0,n=t;e<n.length;e+=1){var o=n[e],u=i.check(o,r);if(u.hasError)return u}return!0})},t}(o),m=function(r){function t(){for(var u=[],t=arguments.length;t--;)u[t]=arguments[t];r.call(this,"any"),this.rule(function(t,r){for(var e,n=0,o=u;n<o.length;n+=1){if(!(e=o[n].check(t,r)).hasError)return e}return e},null)}return r&&(t.__proto__=r),(
t.prototype=Object.create(r&&r.prototype)).constructor=t}(o),b=h.Schema,v=h.SchemaModel,O={string:e,number:function(t){return new l(t)},boolean:function(t){return new y(t)},date:function(t){return new d(t)},object:function(t){return new _(t)},array:function(t){return new g(t)},any:function(){for(var t=[],r=arguments.length;r--;)t[r]=arguments[r];return new(Function.prototype.bind.apply(m,[null].concat(t)))}},w={Schema:b,SchemaModel:v,T:O};return t.Schema=b,t.SchemaModel=v,t.T=O,t.default=w,t}({});