UNPKG

ll-package

Version:

2 lines (1 loc) 2.86 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("../../../../@babel/runtime/helpers/esm/extends.js"),F=require("../../../../async-validator/dist-web/index.js"),O=require("vue"),q=require("../../vc-util/warning.js"),V=require("./valueUtil.js"),$=require("./messages.js"),j=require("../../_util/props-util/index.js");var b=function(o,u,r,s){function d(n){return n instanceof r?n:new r(function(c){c(n)})}return new(r||(r=Promise))(function(n,c){function m(t){try{e(s.next(t))}catch(i){c(i)}}function f(t){try{e(s.throw(t))}catch(i){c(i)}}function e(t){t.done?n(t.value):d(t.value).then(m,f)}e((s=s.apply(o,u||[])).next())})};const M=F.default;function A(o,u){return o.replace(/\$\{\w+\}/g,r=>{const s=r.slice(2,-1);return u[s]})}function P(o,u,r,s,d){return b(this,void 0,void 0,function*(){const n=p.default({},r);delete n.ruleIndex,delete n.trigger;let c=null;n&&n.type==="array"&&n.defaultField&&(c=n.defaultField,delete n.defaultField);const m=new M({[o]:[n]}),f=V.setValues({},$.defaultValidateMessages,s.validateMessages);m.messages(f);let e=[];try{yield Promise.resolve(m.validate({[o]:u},p.default({},s)))}catch(l){l.errors?e=l.errors.map((a,h)=>{let{message:g}=a;return j.isValidElement(g)?O.cloneVNode(g,{key:`error_${h}`}):g}):(console.error(l),e=[f.default()])}if(!e.length&&c)return(yield Promise.all(u.map((a,h)=>P(`${o}.${h}`,a,c,s,d)))).reduce((a,h)=>[...a,...h],[]);const t=p.default(p.default(p.default({},r),{name:o,enum:(r.enum||[]).join(", ")}),d);return e.map(l=>typeof l=="string"?A(l,t):l)})}function I(o,u,r,s,d,n){const c=o.join("."),m=r.map((e,t)=>{const i=e.validator,l=p.default(p.default({},e),{ruleIndex:t});return i&&(l.validator=(a,h,g)=>{let v=!1;const w=i(a,h,function(){for(var y=arguments.length,x=new Array(y),R=0;R<y;R++)x[R]=arguments[R];Promise.resolve().then(()=>{q.warning(!v,"Your validator function has already return a promise. `callback` will be ignored."),v||g(...x)})});v=w&&typeof w.then=="function"&&typeof w.catch=="function",q.warning(v,"`callback` is deprecated. Please return a promise instead."),v&&w.then(()=>{g()}).catch(y=>{g(y||" ")})}),l}).sort((e,t)=>{let{warningOnly:i,ruleIndex:l}=e,{warningOnly:a,ruleIndex:h}=t;return!!i==!!a?l-h:i?1:-1});let f;if(d===!0)f=new Promise((e,t)=>b(this,void 0,void 0,function*(){for(let i=0;i<m.length;i+=1){const l=m[i],a=yield P(c,u,l,s,n);if(a.length){t([{errors:a,rule:l}]);return}}e([])}));else{const e=m.map(t=>P(c,u,t,s,n).then(i=>({errors:i,rule:t})));f=(d?C(e):k(e)).then(t=>Promise.reject(t))}return f.catch(e=>e),f}function k(o){return b(this,void 0,void 0,function*(){return Promise.all(o).then(u=>[].concat(...u))})}function C(o){return b(this,void 0,void 0,function*(){let u=0;return new Promise(r=>{o.forEach(s=>{s.then(d=>{d.errors.length&&r([d]),u+=1,u===o.length&&r([])})})})})}exports.validateRules=I;