async-validator.min.js 16.9 KB
Newer Older
1
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e="undefined"!=typeof globalThis?globalThis:e||self).AsyncValidator=r()}(this,(function(){"use strict";var e=function(r,n){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,r){e.__proto__=r}||function(e,r){for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])},e(r,n)};var r=function(){return r=Object.assign||function(e){for(var r,n=1,t=arguments.length;n<t;n++)for(var i in r=arguments[n])Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i]);return e},r.apply(this,arguments)};function n(e,r){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var t,i,a=n.call(e),o=[];try{for(;(void 0===r||r-- >0)&&!(t=a.next()).done;)o.push(t.value)}catch(e){i={error:e}}finally{try{t&&!t.done&&(n=a.return)&&n.call(a)}finally{if(i)throw i.error}}return o}function t(e,r,n){if(n||2===arguments.length)for(var t,i=0,a=r.length;i<a;i++)!t&&i in r||(t||(t=Array.prototype.slice.call(r,0,i)),t[i]=r[i]);return e.concat(t||Array.prototype.slice.call(r))}var i=/%[sdj%]/g,a=function(){};function o(e){if(!e||!e.length)return null;var r={};return e.forEach((function(e){var n=e.field;r[n]=r[n]||[],r[n].push(e)})),r}function u(e){for(var r=[],n=1;n<arguments.length;n++)r[n-1]=arguments[n];var t=0,a=r.length;if("function"==typeof e)return e.apply(null,r);if("string"==typeof e){var o=e.replace(i,(function(e){if("%%"===e)return"%";if(t>=a)return e;switch(e){case"%s":return String(r[t++]);case"%d":return Number(r[t++]);case"%j":try{return JSON.stringify(r[t++])}catch(e){return"[Circular]"}break;default:return e}}));return o}return e}function s(e,r){return null==e||(!("array"!==r||!Array.isArray(e)||e.length)||!(!function(e){return"string"===e||"url"===e||"hex"===e||"email"===e||"date"===e||"pattern"===e}(r)||"string"!=typeof e||e))}function c(e,r,n){var t=0,i=e.length;!function a(o){if(o&&o.length)n(o);else{var u=t;t+=1,u<i?r(e[u],a):n([])}}([])}"undefined"!=typeof process&&process.env&&"production"!==process.env.NODE_ENV&&"undefined"!=typeof window&&"undefined"!=typeof document&&(a=function(e,r){"undefined"!=typeof console&&console.warn&&"undefined"==typeof ASYNC_VALIDATOR_NO_WARNING&&r.every((function(e){return"string"==typeof e}))&&console.warn(e,r)});var f=function(r){function n(e,n){var t=r.call(this,"Async Validation Error")||this;return t.errors=e,t.fields=n,t}return function(r,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function t(){this.constructor=r}e(r,n),r.prototype=null===n?Object.create(n):(t.prototype=n.prototype,new t)}(n,r),n}(Error);function l(e,r,i,a,u){if(r.first){var s=new Promise((function(r,s){var l=function(e){var r=[];return Object.keys(e).forEach((function(i){r.push.apply(r,t([],n(e[i]||[]),!1))})),r}(e);c(l,i,(function(e){return a(e),e.length?s(new f(e,o(e))):r(u)}))}));return s.catch((function(e){return e})),s}var l=!0===r.firstFields?Object.keys(e):r.firstFields||[],d=Object.keys(e),p=d.length,y=0,g=[],h=new Promise((function(r,s){var h=function(e){if(g.push.apply(g,e),++y===p)return a(g),g.length?s(new f(g,o(g))):r(u)};d.length||(a(g),r(u)),d.forEach((function(r){var a=e[r];-1!==l.indexOf(r)?c(a,i,h):function(e,r,i){var a=[],o=0,u=e.length;function s(e){a.push.apply(a,t([],n(e||[]),!1)),++o===u&&i(a)}e.forEach((function(e){r(e,s)}))}(a,i,h)}))}));return h.catch((function(e){return e})),h}function d(e,r){return function(n){var t,i;return t=e.fullFields?function(e,r){for(var n=e,t=0;t<r.length;t++){if(null==n)return n;n=n[r[t]]}return n}(r,e.fullFields):r[n.field||e.fullField],(i=n)&&void 0!==i.message?(n.field=n.field||e.fullField,n.fieldValue=t,n):{message:"function"==typeof n?n():n,fieldValue:t,field:n.field||e.fullField}}}function p(e,n){if(n)for(var t in n)if(n.hasOwnProperty(t)){var i=n[t];"object"==typeof i&&"object"==typeof e[t]?e[t]=r(r({},e[t]),i):e[t]=i}return e}var y,g=function(e,r,n,t,i,a){!e.required||n.hasOwnProperty(e.field)&&!s(r,a||e.type)||t.push(u(i.messages.required,e.fullField))},h=/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+\.)+[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}))$/,m=/^#?([a-f0-9]{6}|[a-f0-9]{3})$/i,v={integer:function(e){return v.number(e)&&parseInt(e,10)===e},float:function(e){return v.number(e)&&!v.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&&!isNaN(e.getTime())},number:function(e){return!isNaN(e)&&"number"==typeof e},object:function(e){return"object"==typeof e&&!v.array(e)},method:function(e){return"function"==typeof e},email:function(e){return"string"==typeof e&&e.length<=320&&!!e.match(h)},url:function(e){return"string"==typeof e&&e.length<=2048&&!!e.match(function(){if(y)return y;var e="[a-fA-F\\d:]",r=function(r){return r&&r.includeBoundaries?"(?:(?<=\\s|^)(?=".concat(e,")|(?<=").concat(e,")(?=\\s|$))"):""},n="(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}",t="[a-fA-F\\d]{1,4}",i="\n(?:\n(?:".concat(t,":){7}(?:").concat(t,"|:)|                                    // 1:2:3:4:5:6:7::  1:2:3:4:5:6:7:8\n(?:").concat(t,":){6}(?:").concat(n,"|:").concat(t,"|:)|                             // 1:2:3:4:5:6::    1:2:3:4:5:6::8   1:2:3:4:5:6::8  1:2:3:4:5:6::1.2.3.4\n(?:").concat(t,":){5}(?::").concat(n,"|(?::").concat(t,"){1,2}|:)|                   // 1:2:3:4:5::      1:2:3:4:5::7:8   1:2:3:4:5::8    1:2:3:4:5::7:1.2.3.4\n(?:").concat(t,":){4}(?:(?::").concat(t,"){0,1}:").concat(n,"|(?::").concat(t,"){1,3}|:)| // 1:2:3:4::        1:2:3:4::6:7:8   1:2:3:4::8      1:2:3:4::6:7:1.2.3.4\n(?:").concat(t,":){3}(?:(?::").concat(t,"){0,2}:").concat(n,"|(?::").concat(t,"){1,4}|:)| // 1:2:3::          1:2:3::5:6:7:8   1:2:3::8        1:2:3::5:6:7:1.2.3.4\n(?:").concat(t,":){2}(?:(?::").concat(t,"){0,3}:").concat(n,"|(?::").concat(t,"){1,5}|:)| // 1:2::            1:2::4:5:6:7:8   1:2::8          1:2::4:5:6:7:1.2.3.4\n(?:").concat(t,":){1}(?:(?::").concat(t,"){0,4}:").concat(n,"|(?::").concat(t,"){1,6}|:)| // 1::              1::3:4:5:6:7:8   1::8            1::3:4:5:6:7:1.2.3.4\n(?::(?:(?::").concat(t,"){0,5}:").concat(n,"|(?::").concat(t,"){1,7}|:))             // ::2:3:4:5:6:7:8  ::2:3:4:5:6:7:8  ::8             ::1.2.3.4\n)(?:%[0-9a-zA-Z]{1,})?                                             // %eth0            %1\n").replace(/\s*\/\/.*$/gm,"").replace(/\n/g,"").trim(),a=new RegExp("(?:^".concat(n,"$)|(?:^").concat(i,"$)")),o=new RegExp("^".concat(n,"$")),u=new RegExp("^".concat(i,"$")),s=function(e){return e&&e.exact?a:new RegExp("(?:".concat(r(e)).concat(n).concat(r(e),")|(?:").concat(r(e)).concat(i).concat(r(e),")"),"g")};s.v4=function(e){return e&&e.exact?o:new RegExp("".concat(r(e)).concat(n).concat(r(e)),"g")},s.v6=function(e){return e&&e.exact?u:new RegExp("".concat(r(e)).concat(i).concat(r(e)),"g")};var c=s.v4().source,f=s.v6().source,l="(?:".concat("(?:(?:[a-z]+:)?//)","|www\\.)").concat("(?:\\S+(?::\\S*)?@)?","(?:localhost|").concat(c,"|").concat(f,"|").concat("(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)").concat("(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*").concat("(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))",")").concat("(?::\\d{2,5})?").concat('(?:[/?#][^\\s"]*)?');return y=new RegExp("(?:^".concat(l,"$)"),"i")}())},hex:function(e){return"string"==typeof e&&!!e.match(m)}},q={required:g,whitespace:function(e,r,n,t,i){(/^\s+$/.test(r)||""===r)&&t.push(u(i.messages.whitespace,e.fullField))},type:function(e,r,n,t,i){if(e.required&&void 0===r)g(e,r,n,t,i);else{var a=e.type;["integer","float","array","regexp","object","method","email","number","date","url","hex"].indexOf(a)>-1?v[a](r)||t.push(u(i.messages.types[a],e.fullField,e.type)):a&&typeof r!==e.type&&t.push(u(i.messages.types[a],e.fullField,e.type))}},range:function(e,r,n,t,i){var a="number"==typeof e.len,o="number"==typeof e.min,s="number"==typeof e.max,c=r,f=null,l="number"==typeof r,d="string"==typeof r,p=Array.isArray(r);if(l?f="number":d?f="string":p&&(f="array"),!f)return!1;p&&(c=r.length),d&&(c=r.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,"_").length),a?c!==e.len&&t.push(u(i.messages[f].len,e.fullField,e.len)):o&&!s&&c<e.min?t.push(u(i.messages[f].min,e.fullField,e.min)):s&&!o&&c>e.max?t.push(u(i.messages[f].max,e.fullField,e.max)):o&&s&&(c<e.min||c>e.max)&&t.push(u(i.messages[f].range,e.fullField,e.min,e.max))},enum:function(e,r,n,t,i){e.enum=Array.isArray(e.enum)?e.enum:[],-1===e.enum.indexOf(r)&&t.push(u(i.messages.enum,e.fullField,e.enum.join(", ")))},pattern:function(e,r,n,t,i){if(e.pattern)if(e.pattern instanceof RegExp)e.pattern.lastIndex=0,e.pattern.test(r)||t.push(u(i.messages.pattern.mismatch,e.fullField,r,e.pattern));else if("string"==typeof e.pattern){new RegExp(e.pattern).test(r)||t.push(u(i.messages.pattern.mismatch,e.fullField,r,e.pattern))}}},b=function(e,r,n,t,i){var a=e.type,o=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r,a)&&!e.required)return n();q.required(e,r,t,o,i,a),s(r,a)||q.type(e,r,t,o,i)}n(o)},w={string:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r,"string")&&!e.required)return n();q.required(e,r,t,a,i,"string"),s(r,"string")||(q.type(e,r,t,a,i),q.range(e,r,t,a,i),q.pattern(e,r,t,a,i),!0===e.whitespace&&q.whitespace(e,r,t,a,i))}n(a)},method:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r)&&!e.required)return n();q.required(e,r,t,a,i),void 0!==r&&q.type(e,r,t,a,i)}n(a)},number:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(""===r&&(r=void 0),s(r)&&!e.required)return n();q.required(e,r,t,a,i),void 0!==r&&(q.type(e,r,t,a,i),q.range(e,r,t,a,i))}n(a)},boolean:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r)&&!e.required)return n();q.required(e,r,t,a,i),void 0!==r&&q.type(e,r,t,a,i)}n(a)},regexp:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r)&&!e.required)return n();q.required(e,r,t,a,i),s(r)||q.type(e,r,t,a,i)}n(a)},integer:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r)&&!e.required)return n();q.required(e,r,t,a,i),void 0!==r&&(q.type(e,r,t,a,i),q.range(e,r,t,a,i))}n(a)},float:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r)&&!e.required)return n();q.required(e,r,t,a,i),void 0!==r&&(q.type(e,r,t,a,i),q.range(e,r,t,a,i))}n(a)},array:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(null==r&&!e.required)return n();q.required(e,r,t,a,i,"array"),null!=r&&(q.type(e,r,t,a,i),q.range(e,r,t,a,i))}n(a)},object:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r)&&!e.required)return n();q.required(e,r,t,a,i),void 0!==r&&q.type(e,r,t,a,i)}n(a)},enum:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r)&&!e.required)return n();q.required(e,r,t,a,i),void 0!==r&&q.enum(e,r,t,a,i)}n(a)},pattern:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r,"string")&&!e.required)return n();q.required(e,r,t,a,i),s(r,"string")||q.pattern(e,r,t,a,i)}n(a)},date:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r,"date")&&!e.required)return n();if(q.required(e,r,t,a,i),!s(r,"date")){var o=void 0;o=r instanceof Date?r:new Date(r),q.type(e,o,t,a,i),o&&q.range(e,o.getTime(),t,a,i)}}n(a)},url:b,hex:b,email:b,required:function(e,r,n,t,i){var a=[],o=Array.isArray(r)?"array":typeof r;q.required(e,r,t,a,i,o),n(a)},any:function(e,r,n,t,i){var a=[];if(e.required||!e.required&&t.hasOwnProperty(e.field)){if(s(r)&&!e.required)return n();q.required(e,r,t,a,i)}n(a)}};function F(){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 x=F(),O=function(){function e(e){this.rules=null,this._messages=x,this.define(e)}return e.prototype.define=function(e){var r=this;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");this.rules={},Object.keys(e).forEach((function(n){var t=e[n];r.rules[n]=Array.isArray(t)?t:[t]}))},e.prototype.messages=function(e){return e&&(this._messages=p(F(),e)),this._messages},e.prototype.validate=function(i,a,s){var c=this;void 0===a&&(a={}),void 0===s&&(s=function(){});var f=i,y=a,g=s;if("function"==typeof y&&(g=y,y={}),!this.rules||0===Object.keys(this.rules).length)return g&&g(null,f),Promise.resolve(f);if(y.messages){var h=this.messages();h===x&&(h=F()),p(h,y.messages),y.messages=h}else y.messages=this.messages();var m={};(y.keys||Object.keys(this.rules)).forEach((function(e){var n=c.rules[e],t=f[e];n.forEach((function(n){var a=n;"function"==typeof a.transform&&(f===i&&(f=r({},f)),t=f[e]=a.transform(t)),(a="function"==typeof a?{validator:a}:r({},a)).validator=c.getValidationMethod(a),a.validator&&(a.field=e,a.fullField=a.fullField||e,a.type=c.getType(a),m[e]=m[e]||[],m[e].push({rule:a,value:t,source:f,field:e}))}))}));var v={};return l(m,y,(function(i,a){var o,s,c=i.rule,l=!("object"!==c.type&&"array"!==c.type||"object"!=typeof c.fields&&"object"!=typeof c.defaultField);function p(e,i){return r(r({},i),{fullField:"".concat(c.fullField,".").concat(e),fullFields:c.fullFields?t(t([],n(c.fullFields),!1),[e],!1):[e]})}function g(o){void 0===o&&(o=[]);var s=Array.isArray(o)?o:[o];!y.suppressWarning&&s.length&&e.warning("async-validator:",s),s.length&&void 0!==c.message&&(s=[].concat(c.message));var g=s.map(d(c,f));if(y.first&&g.length)return v[c.field]=1,a(g);if(l){if(c.required&&!i.value)return void 0!==c.message?g=[].concat(c.message).map(d(c,f)):y.error&&(g=[y.error(c,u(y.messages.required,c.field))]),a(g);var h={};c.defaultField&&Object.keys(i.value).map((function(e){h[e]=c.defaultField})),h=r(r({},h),i.rule.fields);var m={};Object.keys(h).forEach((function(e){var r=h[e],n=Array.isArray(r)?r:[r];m[e]=n.map(p.bind(null,e))}));var q=new e(m);q.messages(y.messages),i.rule.options&&(i.rule.options.messages=y.messages,i.rule.options.error=y.error),q.validate(i.value,i.rule.options||y,(function(e){var r=[];g&&g.length&&r.push.apply(r,t([],n(g),!1)),e&&e.length&&r.push.apply(r,t([],n(e),!1)),a(r.length?r:null)}))}else a(g)}if(l=l&&(c.required||!c.required&&i.value),c.field=i.field,c.asyncValidator)s=c.asyncValidator(c,i.value,g,i.source,y);else if(c.validator){try{s=c.validator(c,i.value,g,i.source,y)}catch(e){null===(o=console.error)||void 0===o||o.call(console,e),y.suppressValidatorError||setTimeout((function(){throw e}),0),g(e.message)}!0===s?g():!1===s?g("function"==typeof c.message?c.message(c.fullField||c.field):c.message||"".concat(c.fullField||c.field," fails")):s instanceof Array?g(s):s instanceof Error&&g(s.message)}s&&s.then&&s.then((function(){return g()}),(function(e){return g(e)}))}),(function(e){!function(e){for(var r,i=[],a={},u=0;u<e.length;u++)r=e[u],Array.isArray(r)?i=i.concat.apply(i,t([],n(r),!1)):i.push(r);i.length?(a=o(i),g(i,a)):g(null,f)}(e)}),f)},e.prototype.getType=function(e){if(void 0===e.type&&e.pattern instanceof RegExp&&(e.type="pattern"),"function"!=typeof e.validator&&e.type&&!w.hasOwnProperty(e.type))throw new Error(u("Unknown rule type %s",e.type));return e.type||"string"},e.prototype.getValidationMethod=function(e){if("function"==typeof e.validator)return e.validator;var r=Object.keys(e),n=r.indexOf("message");return-1!==n&&r.splice(n,1),1===r.length&&"required"===r[0]?w.required:w[this.getType(e)]||void 0},e.register=function(e,r){if("function"!=typeof r)throw new Error("Cannot register a validator by type, validator is not a function");w[e]=r},e.warning=a,e.messages=x,e.validators=w,e}();return O}));