2 lines
14 KiB
JavaScript
2 lines
14 KiB
JavaScript
function _extends(){return(_extends=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t,n=arguments[r];for(t in n)Object.prototype.hasOwnProperty.call(n,t)&&(e[t]=n[t])}return e}).apply(this,arguments)}var formatRegExp=/%[sdj%]/g,warning=function(){};function convertFieldsError(e){if(!e||!e.length)return null;var t={};return e.forEach(function(e){var r=e.field;t[r]=t[r]||[],t[r].push(e)}),t}function format(){for(var e=arguments.length,r=new Array(e),t=0;t<e;t++)r[t]=arguments[t];var n=1,a=r[0],i=r.length;if("function"==typeof a)return a.apply(null,r.slice(1));if("string"!=typeof a)return a;for(var s=String(a).replace(formatRegExp,function(e){if("%%"===e)return"%";if(i<=n)return e;switch(e){case"%s":return String(r[n++]);case"%d":return Number(r[n++]);case"%j":try{return JSON.stringify(r[n++])}catch(e){return"[Circular]"}break;default:return e}}),u=r[n];n<i;u=r[++n])s+=" "+u;return s}function isNativeStringType(e){return"string"===e||"url"===e||"hex"===e||"email"===e||"pattern"===e}function isEmptyValue(e,r){return null==e||(!("array"!==r||!Array.isArray(e)||e.length)||!(!isNativeStringType(r)||"string"!=typeof e||e))}function asyncParallelArray(e,r,t){var n=[],a=0,i=e.length;function s(e){n.push.apply(n,e),++a===i&&t(n)}e.forEach(function(e){r(e,s)})}function asyncSerialArray(t,n,a){var i=0,s=t.length;!function e(r){r&&r.length?a(r):(r=i,i+=1,r<s?n(t[r],e):a([]))}([])}function flattenObjArr(r){var t=[];return Object.keys(r).forEach(function(e){t.push.apply(t,r[e])}),t}function asyncMap(a,e,i,s){if(e.first){var r=new Promise(function(r,t){asyncSerialArray(flattenObjArr(a),i,function(e){return s(e),e.length?t({errors:e,fields:convertFieldsError(e)}):r()})});return r.catch(function(e){return e}),r}var u=e.firstFields||[];!0===u&&(u=Object.keys(a));var o=Object.keys(a),l=o.length,f=0,p=[],e=new Promise(function(r,t){function n(e){if(p.push.apply(p,e),++f===l)return s(p),p.length?t({errors:p,fields:convertFieldsError(p)}):r()}o.length||(s(p),r()),o.forEach(function(e){var r=a[e];(-1!==u.indexOf(e)?asyncSerialArray:asyncParallelArray)(r,i,n)})});return e.catch(function(e){return e}),e}function complementError(r){return function(e){return e&&e.message?(e.field=e.field||r.fullField,e):{message:"function"==typeof e?e():e,field:e.field||r.fullField}}}function deepMerge(e,r){if(r)for(var t in r){var n;r.hasOwnProperty(t)&&("object"==typeof(n=r[t])&&"object"==typeof e[t]?e[t]=_extends({},e[t],{},n):e[t]=n)}return e}function required(e,r,t,n,a,i){!e.required||t.hasOwnProperty(e.field)&&!isEmptyValue(r,i||e.type)||n.push(format(a.messages.required,e.fullField))}function whitespace(e,r,t,n,a){!/^\s+$/.test(r)&&""!==r||n.push(format(a.messages.whitespace,e.fullField))}"undefined"!=typeof process&&process.env&&"production"!==process.env.NODE_ENV&&"undefined"!=typeof window&&"undefined"!=typeof document&&(warning=function(e,r){"undefined"!=typeof console&&console.warn&&r.every(function(e){return"string"==typeof e})&&console.warn(e,r)});var pattern={email:/^(([^<>()\[\]\\.,;:\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,}))$/,url: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"),hex:/^#?([a-f0-9]{6}|[a-f0-9]{3})$/i},types={integer:function(e){return types.number(e)&&parseInt(e,10)===e},float:function(e){return types.number(e)&&!types.integer(e)},array:function(e){return Array.isArray(e)},regexp:function(e){if(e instanceof RegExp)return!0;try{return!!new RegExp(e)}catch(e){return!1}},date:function(e){return"function"==typeof e.getTime&&"function"==typeof e.getMonth&&"function"==typeof e.getYear},number:function(e){return!isNaN(e)&&"number"==typeof+e},object:function(e){return"object"==typeof e&&!types.array(e)},method:function(e){return"function"==typeof e},email:function(e){return"string"==typeof e&&!!e.match(pattern.email)&&e.length<255},url:function(e){return"string"==typeof e&&!!e.match(pattern.url)},hex:function(e){return"string"==typeof e&&!!e.match(pattern.hex)}};function type(e,r,t,n,a){e.required&&void 0===r?required(e,r,t,n,a):(t=e.type,-1<["integer","float","array","regexp","object","method","email","number","date","url","hex"].indexOf(t)?types[t](r)||n.push(format(a.messages.types[t],e.fullField,e.type)):t&&typeof r!==e.type&&n.push(format(a.messages.types[t],e.fullField,e.type)))}function range(e,r,t,n,a){var i="number"==typeof e.len,s="number"==typeof e.min,u="number"==typeof e.max,o=r,l=null,f="number"==typeof r,p="string"==typeof r,d=Array.isArray(r);if(f?l="number":p?l="string":d&&(l="array"),!l)return!1;d&&(o=r.length),p&&(o=r.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,"_").length),i?o!==e.len&&n.push(format(a.messages[l].len,e.fullField,e.len)):s&&!u&&o<e.min?n.push(format(a.messages[l].min,e.fullField,e.min)):u&&!s&&o>e.max?n.push(format(a.messages[l].max,e.fullField,e.max)):s&&u&&(o<e.min||o>e.max)&&n.push(format(a.messages[l].range,e.fullField,e.min,e.max))}var ENUM="enum";function enumerable(e,r,t,n,a){e[ENUM]=Array.isArray(e[ENUM])?e[ENUM]:[],-1===e[ENUM].indexOf(r)&&n.push(format(a.messages[ENUM],e.fullField,e[ENUM].join(", ")))}function pattern$1(e,r,t,n,a){e.pattern&&(e.pattern instanceof RegExp?(e.pattern.lastIndex=0,e.pattern.test(r)||n.push(format(a.messages.pattern.mismatch,e.fullField,r,e.pattern))):"string"==typeof e.pattern&&(new RegExp(e.pattern).test(r)||n.push(format(a.messages.pattern.mismatch,e.fullField,r,e.pattern))))}var rules={required:required,whitespace:whitespace,type:type,range:range,enum:enumerable,pattern:pattern$1};function string(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r,"string")&&!e.required)return t();rules.required(e,r,n,i,a,"string"),isEmptyValue(r,"string")||(rules.type(e,r,n,i,a),rules.range(e,r,n,i,a),rules.pattern(e,r,n,i,a),!0===e.whitespace&&rules.whitespace(e,r,n,i,a))}t(i)}function method(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r)&&!e.required)return t();rules.required(e,r,n,i,a),void 0!==r&&rules.type(e,r,n,i,a)}t(i)}function number(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(""===r&&(r=void 0),isEmptyValue(r)&&!e.required)return t();rules.required(e,r,n,i,a),void 0!==r&&(rules.type(e,r,n,i,a),rules.range(e,r,n,i,a))}t(i)}function _boolean(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r)&&!e.required)return t();rules.required(e,r,n,i,a),void 0!==r&&rules.type(e,r,n,i,a)}t(i)}function regexp(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r)&&!e.required)return t();rules.required(e,r,n,i,a),isEmptyValue(r)||rules.type(e,r,n,i,a)}t(i)}function integer(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r)&&!e.required)return t();rules.required(e,r,n,i,a),void 0!==r&&(rules.type(e,r,n,i,a),rules.range(e,r,n,i,a))}t(i)}function floatFn(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r)&&!e.required)return t();rules.required(e,r,n,i,a),void 0!==r&&(rules.type(e,r,n,i,a),rules.range(e,r,n,i,a))}t(i)}function array(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r,"array")&&!e.required)return t();rules.required(e,r,n,i,a,"array"),isEmptyValue(r,"array")||(rules.type(e,r,n,i,a),rules.range(e,r,n,i,a))}t(i)}function object(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r)&&!e.required)return t();rules.required(e,r,n,i,a),void 0!==r&&rules.type(e,r,n,i,a)}t(i)}var ENUM$1="enum";function enumerable$1(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r)&&!e.required)return t();rules.required(e,r,n,i,a),void 0!==r&&rules[ENUM$1](e,r,n,i,a)}t(i)}function pattern$2(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r,"string")&&!e.required)return t();rules.required(e,r,n,i,a),isEmptyValue(r,"string")||rules.pattern(e,r,n,i,a)}t(i)}function date(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r)&&!e.required)return t();rules.required(e,r,n,i,a),isEmptyValue(r)||(r="number"==typeof r?new Date(r):r,rules.type(e,r,n,i,a),r&&rules.range(e,r.getTime(),n,i,a))}t(i)}function required$1(e,r,t,n,a){var i=[],s=Array.isArray(r)?"array":typeof r;rules.required(e,r,n,i,a,s),t(i)}function type$1(e,r,t,n,a){var i=e.type,s=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r,i)&&!e.required)return t();rules.required(e,r,n,s,a,i),isEmptyValue(r,i)||rules.type(e,r,n,s,a)}t(s)}function any(e,r,t,n,a){var i=[];if(e.required||!e.required&&n.hasOwnProperty(e.field)){if(isEmptyValue(r)&&!e.required)return t();rules.required(e,r,n,i,a)}t(i)}var validators={string:string,method:method,number:number,boolean:_boolean,regexp:regexp,integer:integer,float:floatFn,array:array,object:object,enum:enumerable$1,pattern:pattern$2,date:date,url:type$1,hex:type$1,email:type$1,required:required$1,any:any};function newMessages(){return{default:"Validation error on field %s",required:"%s is required",enum:"%s must be one of %s",whitespace:"%s cannot be empty",date:{format:"%s date %s is invalid for format %s",parse:"%s date could not be parsed, %s is invalid ",invalid:"%s date %s is invalid"},types:{string:"%s is not a %s",method:"%s is not a %s (function)",array:"%s is not an %s",object:"%s is not an %s",number:"%s is not a %s",date:"%s is not a %s",boolean:"%s is not a %s",integer:"%s is not an %s",float:"%s is not a %s",regexp:"%s is not a valid %s",email:"%s is not a valid %s",url:"%s is not a valid %s",hex:"%s is not a valid %s"},string:{len:"%s must be exactly %s characters",min:"%s must be at least %s characters",max:"%s cannot be longer than %s characters",range:"%s must be between %s and %s characters"},number:{len:"%s must equal %s",min:"%s cannot be less than %s",max:"%s cannot be greater than %s",range:"%s must be between %s and %s"},array:{len:"%s must be exactly %s in length",min:"%s cannot be less than %s in length",max:"%s cannot be greater than %s in length",range:"%s must be between %s and %s in length"},pattern:{mismatch:"%s value %s does not match pattern %s"},clone:function(){var e=JSON.parse(JSON.stringify(this));return e.clone=this.clone,e}}}var messages=newMessages();function Schema(e){this.rules=null,this._messages=messages,this.define(e)}Schema.prototype={messages:function(e){return e&&(this._messages=deepMerge(newMessages(),e)),this._messages},define:function(e){if(!e)throw new Error("Cannot configure a schema with no rules");if("object"!=typeof e||Array.isArray(e))throw new Error("Rules must be an object");var r,t;for(r in this.rules={},e)e.hasOwnProperty(r)&&(t=e[r],this.rules[r]=Array.isArray(t)?t:[t])},validate:function(t,e,r){var n=this;void 0===e&&(e={}),void 0===r&&(r=function(){});var a,i,s=t,p=e,u=r;if("function"==typeof p&&(u=p,p={}),!this.rules||0===Object.keys(this.rules).length)return u&&u(),Promise.resolve();p.messages?((r=this.messages())===messages&&(r=newMessages()),deepMerge(r,p.messages),p.messages=r):p.messages=this.messages();var o={};(p.keys||Object.keys(this.rules)).forEach(function(r){a=n.rules[r],i=s[r],a.forEach(function(e){"function"==typeof e.transform&&(s===t&&(s=_extends({},s)),i=s[r]=e.transform(i)),(e="function"==typeof e?{validator:e}:_extends({},e)).validator=n.getValidationMethod(e),e.field=r,e.fullField=e.fullField||r,e.type=n.getType(e),e.validator&&(o[r]=o[r]||[],o[r].push({rule:e,value:i,source:s,field:r}))})});var d={};return asyncMap(o,p,function(s,u){var e,o=s.rule,l=!("object"!==o.type&&"array"!==o.type||"object"!=typeof o.fields&&"object"!=typeof o.defaultField);function f(e,r){return _extends({},r,{fullField:o.fullField+"."+e})}function r(e){void 0===e&&(e=[]);var t=e;if(Array.isArray(t)||(t=[t]),!p.suppressWarning&&t.length&&Schema.warning("async-validator:",t),t.length&&o.message&&(t=[].concat(o.message)),t=t.map(complementError(o)),p.first&&t.length)return d[o.field]=1,u(t);if(l){if(o.required&&!s.value)return t=o.message?[].concat(o.message).map(complementError(o)):p.error?[p.error(o,format(p.messages.required,o.field))]:[],u(t);var r,n,a={};if(o.defaultField)for(var i in s.value)s.value.hasOwnProperty(i)&&(a[i]=o.defaultField);for(r in a=_extends({},a,{},s.rule.fields))a.hasOwnProperty(r)&&(n=Array.isArray(a[r])?a[r]:[a[r]],a[r]=n.map(f.bind(null,r)));e=new Schema(a);e.messages(p.messages),s.rule.options&&(s.rule.options.messages=p.messages,s.rule.options.error=p.error),e.validate(s.value,s.rule.options||p,function(e){var r=[];t&&t.length&&r.push.apply(r,t),e&&e.length&&r.push.apply(r,e),u(r.length?r:null)})}else u(t)}l=l&&(o.required||!o.required&&s.value),o.field=s.field,o.asyncValidator?e=o.asyncValidator(o,s.value,r,s.source,p):o.validator&&(!0===(e=o.validator(o,s.value,r,s.source,p))?r():!1===e?r(o.message||o.field+" fails"):e instanceof Array?r(e):e instanceof Error&&r(e.message)),e&&e.then&&e.then(function(){return r()},r)},function(e){!function(e){var r,t,n=[],a={};for(r=0;r<e.length;r++)t=e[r],Array.isArray(t)?n=n.concat.apply(n,t):n.push(t);a=n.length?convertFieldsError(n):n=null,u(n,a)}(e)})},getType:function(e){if(void 0===e.type&&e.pattern instanceof RegExp&&(e.type="pattern"),"function"!=typeof e.validator&&e.type&&!validators.hasOwnProperty(e.type))throw new Error(format("Unknown rule type %s",e.type));return e.type||"string"},getValidationMethod:function(e){if("function"==typeof e.validator)return e.validator;var r=Object.keys(e),t=r.indexOf("message");return-1!==t&&r.splice(t,1),1===r.length&&"required"===r[0]?validators.required:validators[this.getType(e)]||!1}},Schema.register=function(e,r){if("function"!=typeof r)throw new Error("Cannot register a validator by type, validator is not a function");validators[e]=r},Schema.warning=warning,Schema.messages=messages;export default Schema;
|