1 | import { Component, Vue } from 'vue-property-decorator';
|
2 | import dayjs from 'dayjs-ext';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var extendStatics = function(d, b) {
|
21 | extendStatics = Object.setPrototypeOf ||
|
22 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
23 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
24 | return extendStatics(d, b);
|
25 | };
|
26 |
|
27 | function __extends(d, b) {
|
28 | extendStatics(d, b);
|
29 | function __() { this.constructor = d; }
|
30 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
31 | }
|
32 |
|
33 | var __assign = function() {
|
34 | __assign = Object.assign || function __assign(t) {
|
35 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
36 | s = arguments[i];
|
37 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
38 | }
|
39 | return t;
|
40 | };
|
41 | return __assign.apply(this, arguments);
|
42 | };
|
43 |
|
44 | function __decorate(decorators, target, key, desc) {
|
45 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
46 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
47 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
48 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
49 | }
|
50 |
|
51 | var FieldBag = (function () {
|
52 | function FieldBag(items) {
|
53 | this._items = items || [];
|
54 | }
|
55 | Object.defineProperty(FieldBag.prototype, "items", {
|
56 | set: function (items) {
|
57 | this.push(items);
|
58 | },
|
59 | enumerable: true,
|
60 | configurable: true
|
61 | });
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | FieldBag.prototype.get = function (field, scope) {
|
70 | return this._items.find(function (item) { return scope
|
71 | ? item.name === field && item.scope === scope
|
72 | : item.name === field; });
|
73 | };
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | FieldBag.prototype.all = function (scope) {
|
81 | return scope
|
82 | ? this._items.filter(function (f) { return f.scope === scope; })
|
83 | : this._items;
|
84 | };
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | FieldBag.prototype.has = function (field, scope) {
|
94 | return !!this._items.find(function (item) { return scope
|
95 | ? item.name === field && item.scope === scope
|
96 | : item.name === field; });
|
97 | };
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | FieldBag.prototype.push = function (item) {
|
107 |
|
108 | this._items.push.apply(this._items, Array.isArray(item) ? item : [item]);
|
109 | };
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | FieldBag.prototype.remove = function (field, scope) {
|
118 | this._items = this._items.filter(function (item) { return scope && item.scope === scope
|
119 | ? item.name !== field
|
120 | : item.name !== field; });
|
121 | };
|
122 | return FieldBag;
|
123 | }());
|
124 |
|
125 | var RULES = {};
|
126 | var RuleContainer = (function () {
|
127 | function RuleContainer() {
|
128 | }
|
129 | Object.defineProperty(RuleContainer, "rules", {
|
130 | get: function () {
|
131 | return RULES;
|
132 | },
|
133 | enumerable: true,
|
134 | configurable: true
|
135 | });
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | RuleContainer.getRule = function (ruleName) {
|
143 | return RULES[ruleName];
|
144 | };
|
145 | |
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | RuleContainer.add = function (ruleName, rule) {
|
153 | RULES[ruleName] = rule;
|
154 | };
|
155 | |
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | RuleContainer.remove = function (ruleName) {
|
162 | delete RULES[ruleName];
|
163 | };
|
164 | |
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | RuleContainer.has = function (ruleName) {
|
171 | return !!RULES[ruleName];
|
172 | };
|
173 | return RuleContainer;
|
174 | }());
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | var is = function (value, constructor) {
|
185 | return Object.prototype.toString.call(value) === "[object " + constructor + "]";
|
186 | };
|
187 |
|
188 | /**
|
189 | * Test to see if an entity of the form `validations` object is an scope
|
190 | * and not a FormValidation object.
|
191 | *
|
192 | * @param {Any} formEntity - The entity to be tested.
|
193 | *
|
194 | * @author Erik Isidore
|
195 | * @version 0.1
|
196 | */
|
197 | var isFormScope = function (formEntity) {
|
198 | if (!is(formEntity, 'Object'))
|
199 | return false;
|
200 | return Object.keys(formEntity)
|
201 | .every(function (name) { return !RuleContainer.has(name); });
|
202 | };
|
203 |
|
204 | var Field = (function () {
|
205 | function Field(options) {
|
206 | this._flags = {
|
207 | pristine: false,
|
208 | dirty: false,
|
209 | changed: false,
|
210 | touched: false,
|
211 | valid: false,
|
212 | errors: []
|
213 | };
|
214 | this.el = options.el;
|
215 | this._vm = options.vm;
|
216 | this.name = options.name;
|
217 | this.value = options.value;
|
218 | this.scope = options.scope;
|
219 | this.rules = this.mapRules(options.rules);
|
220 | this.initialValue = options.value;
|
221 | this.init(options);
|
222 | }
|
223 | Object.defineProperty(Field.prototype, "validate", {
|
224 | get: function () {
|
225 | if (!this._vm || !this._vm.$validator)
|
226 | return function () { return []; };
|
227 | var validate = this._vm.$validator.validate;
|
228 | return validate.bind(this._vm.$validator, this.name, this.scope);
|
229 | },
|
230 | enumerable: true,
|
231 | configurable: true
|
232 | });
|
233 | Object.defineProperty(Field.prototype, "watch", {
|
234 | get: function () {
|
235 | if (!this._vm || !this._vm.$validator)
|
236 | return;
|
237 | return this._vm.$watch.bind(this._vm);
|
238 | },
|
239 | enumerable: true,
|
240 | configurable: true
|
241 | });
|
242 | Object.defineProperty(Field.prototype, "options", {
|
243 | get: function () {
|
244 | if (!this._vm || !this._vm.$validator)
|
245 | return;
|
246 | return this._vm.$validator.options;
|
247 | },
|
248 | enumerable: true,
|
249 | configurable: true
|
250 | });
|
251 | Object.defineProperty(Field.prototype, "flags", {
|
252 | get: function () { return this._flags; },
|
253 | enumerable: true,
|
254 | configurable: true
|
255 | });
|
256 | Object.defineProperty(Field.prototype, "errors", {
|
257 | get: function () { return this._flags.errors; },
|
258 | enumerable: true,
|
259 | configurable: true
|
260 | });
|
261 | Object.defineProperty(Field.prototype, "error", {
|
262 | get: function () { return this._flags.errors[0] || ''; },
|
263 | enumerable: true,
|
264 | configurable: true
|
265 | });
|
266 | |
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | Field.prototype.setFlag = function (flag, value) {
|
275 | if (!Object.keys(this._flags).includes(flag))
|
276 | return;
|
277 | this._flags[flag] = value;
|
278 | };
|
279 | |
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | Field.prototype.init = function (options) {
|
287 | if (process.env.NODE_ENV !== 'production' && !this.name)
|
288 | console.warn('CeeValidate: A field declaration is missing a "name" attribute');
|
289 | this.initFlags();
|
290 | this.addValueListeners();
|
291 | };
|
292 | |
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 | Field.prototype.initFlags = function () {
|
299 | var _this = this;
|
300 | var flagNames = Object.keys(this._flags);
|
301 | var defaultFlags = {
|
302 | pristine: !this.value,
|
303 | dirty: !!this.value,
|
304 | touched: false,
|
305 | changed: false,
|
306 | valid: false,
|
307 | errors: []
|
308 | };
|
309 | flagNames.forEach(function (flag) {
|
310 | _this._flags[flag] = defaultFlags[flag];
|
311 | });
|
312 | };
|
313 | |
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | Field.prototype.addValueListeners = function () {
|
322 | var _this = this;
|
323 | if (!this.watch || !this.el)
|
324 | return;
|
325 | var onBlur = function () {
|
326 | if (!_this._flags.touched)
|
327 | _this._flags.touched = true;
|
328 | if (!_this.options.noListeners)
|
329 | _this.validate();
|
330 | };
|
331 | var onInput = function (value) {
|
332 | _this.value = value;
|
333 | _this._flags.changed = _this.value !== _this.initialValue;
|
334 | if (!_this.options.noListeners)
|
335 | _this.validate();
|
336 | if (!_this._flags.dirty) {
|
337 | _this._flags.dirty = true;
|
338 | _this._flags.pristine = false;
|
339 | }
|
340 | };
|
341 | this.el.addEventListener('focusout', onBlur.bind(this));
|
342 | this.watch(this.scope ? this.scope + "." + this.name : this.name, onInput.bind(this));
|
343 | };
|
344 | |
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | Field.prototype.mapRules = function (rules) {
|
362 | var stringToRules = function (ruleDef) { return ({
|
363 | ruleName: ruleDef.split(':')[0],
|
364 | args: ruleDef.split(':')[1] && ruleDef.split(':')[1].split(',')
|
365 | }); };
|
366 | var objToRules = function (rulesObj) {
|
367 | return Object.keys(rulesObj).map(function (ruleName) { return ({
|
368 | ruleName: ruleName,
|
369 | args: !Array.isArray(rulesObj[ruleName])
|
370 | ? [rulesObj[ruleName]]
|
371 | : rulesObj[ruleName]
|
372 | }); });
|
373 | };
|
374 | return typeof rules === 'string' && rules.length
|
375 | ? rules.split('|').map(stringToRules)
|
376 | : Array.isArray(rules) ? rules.map(stringToRules)
|
377 | : rules && is(rules, 'Object') ? objToRules(rules)
|
378 | : [];
|
379 | };
|
380 | |
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 | Field.prototype.reset = function () {
|
387 | this.value = this.initialValue;
|
388 | this.initFlags();
|
389 | };
|
390 | |
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | Field.prototype.setRule = function (rule) {
|
399 | this.rules = this.mapRules(rule);
|
400 | };
|
401 | return Field;
|
402 | }());
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 | var rule = {
|
415 | validate: function (value) {
|
416 | if (!value)
|
417 | return true;
|
418 | return !!value.trim() && /^([0-9a-zA-Z\s]*)?$/.test(value.trim());
|
419 | },
|
420 | message: 'Deve conter apenas letras e números'
|
421 | };
|
422 | RuleContainer.add('alphanumeric', rule);
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 | var rule$1 = {
|
437 | validate: function (value) {
|
438 | var callbacks = [];
|
439 | for (var _i = 1; _i < arguments.length; _i++) {
|
440 | callbacks[_i - 1] = arguments[_i];
|
441 | }
|
442 | return callbacks.every(function (f) { return f(value); });
|
443 | },
|
444 | message: 'Campo inválido'
|
445 | };
|
446 | RuleContainer.add('custom', rule$1);
|
447 |
|
448 | var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
449 |
|
450 | function createCommonjsModule(fn, module) {
|
451 | return module = { exports: {} }, fn(module, module.exports), module.exports;
|
452 | }
|
453 |
|
454 | var customParseFormat = createCommonjsModule(function (module, exports) {
|
455 | !function(n,t){module.exports=t();}(commonjsGlobal,function(){var i=/(\[[^[]*\])|([-:/.()\s]+)|(A|a|YYYY|YY?|MM?|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g,n=/\d\d/,t=/\d\d?/,r=/[+-]\d\d:?\d\d/,a={},f={},y={},m=[31,0,31,30,31,30,31,31,30,31,30,31];function w(n){var c=n.match(i);if(!c)throw new Error('Invalid format: "'+n+'".');for(var d=c.length,t=0;t<d;t+=1){var r=c[t],e=a[r],o=f[r];c[t]=o?{regex:e,parser:o}:r.replace(/^\[|\]$/g,"");}return function(n){for(var t={},r=0,e=0;r<d;r+=1){var o=c[r];if("string"==typeof o){if(n.indexOf(o,e)!==e){var i=n.substr(e,o.length);throw new Error('Expected "'+o+'" at character '+e+', found "'+i+'".')}e+=o.length;}else{var a=o.regex,f=o.parser,s=n.substr(e),u=a.exec(s);if(!u||0!==u.index)throw new Error('Matching "'+a+'" at character '+e+' failed with "'+s+'".');var h=u[0];f.call(t,h),e+=h.length;}}return function(n){var t,r,e=n.day,o=n.month;if(t=2===o?(r=n.year)%4==0&&r%100!=0||r%400==0?29:28:m[o-1],!(1<=e&&e<=t))throw new Error('Invalid day: "'+e+'".')}(t),function(n){var t=n.afternoon;if(void 0!==t){var r=n.hours;t?r<12&&(n.hours+=12):12===r&&(n.hours=0),delete n.afternoon;}}(t),t}}function e(n,t){a[n]=t;}function o(n,r,e){var t;"string"==typeof n&&(n=[n]),t="string"==typeof r?e?function(n){var t=+n;if(!e(t))throw new Error("Invalid "+r+': "'+n+'".');this[r]=t;}:function(n){this[r]=+n;}:r;for(var o=0,i=n.length;o<i;o+=1)f[n[o]]=t;}e("A",/[AP]M/),o(["A"],function(n){this.afternoon="PM"===n;}),e("a",/[ap]m/),o(["a"],function(n){this.afternoon="pm"===n;}),e("S",/\d/),e("SS",n),e("SSS",/\d{3}/);for(var s=function(n,t){o(n,function(n){this.milliseconds=+n*t;});},u="S",h=100;1<=h;u+="S",h/=10)s(u,h);e("s",t),e("ss",n),o(["s","ss"],"seconds",function(n){return n<=59}),e("m",t),e("mm",n),o(["m","mm"],"minutes",function(n){return n<=59}),e("H",t),e("h",t),e("HH",n),e("hh",n),o(["H","HH"],"hours",function(n){return n<=23}),o(["h","hh"],"hours",function(n){return 1<=n&&n<=12}),e("D",t),e("DD",n),o(["D","DD"],"day"),e("M",t),e("MM",n),o(["M","MM"],"month",function(n){return 1<=n&&n<=12}),e("Y",/[+-]?\d+/),e("YY",n),e("YYYY",/\d{4}/),o(["Y","YYYY"],"year"),o("YY",function(n){n=+n,this.year=n+(68<n?1900:2e3);}),e("z",/[A-Z]{3,4}/),o("z",function(n){(this.zone||(this.zone={})).abbreviation=n;}),e("Z",r),e("ZZ",r),o(["Z","ZZ"],function(n){(this.zone||(this.zone={})).offset=function(n){var t=n.match(/([+-]|\d\d)/g),r=60*t[1]+ +t[2],e=0===r?0:"+"===t[0]?-r:r;if(!(e%15==0&&Math.abs(e)<=765))throw new Error('Invalid time zone offset: "'+n+'".');return e}(n);});return function(n,t){var r=t.prototype,p=r.parse;r.parse=function(n){var t,r,e,o=n.date,i=n.format;if(i){try{var a,f=(t=o,(e=y[r=i])||(e=w(r),y[r]=e),e(t)),s=f.year,u=f.month,h=f.day,c=f.hours,d=f.minutes,m=f.seconds,v=f.milliseconds,l=f.zone;if(l){var Y=Date.UTC(s,u-1,h,c||0,d||0,m||0,v||0)+60*l.offset*1e3;a=new Date(Y);}else a=new Date(s,u-1,h,c||0,d||0,m||0,v||0);this.$d=a;}catch(n){this.$d=new Date(Number.NaN);}this.init(n);}else p.call(this,n);};}});
|
456 |
|
457 | });
|
458 |
|
459 | dayjs.extend(customParseFormat);
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | var rule$2 = {
|
477 | validate: function (value, format) {
|
478 | if (format === void 0) { format = 'DD/MM/YYYY'; }
|
479 | if (!value)
|
480 | return true;
|
481 | var options = is(value, 'String') ? { format: format } : {};
|
482 | var date = dayjs(value, options);
|
483 | return date.isValid();
|
484 | },
|
485 | message: 'Data inválida.'
|
486 | };
|
487 | RuleContainer.add('dateFormat', rule$2);
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 | var rule$3 = {
|
499 | validate: function (value) {
|
500 | if (!value)
|
501 | return true;
|
502 | return !!value.trim() && /^(\d+(\.\d+)?$)/.test(value.trim());
|
503 | },
|
504 | message: 'Deve conter apenas números.'
|
505 | };
|
506 | RuleContainer.add('numeric', rule$3);
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 | var rule$4 = {
|
520 | validate: function (value, regex) {
|
521 | if (!value)
|
522 | return true;
|
523 | if (!is(value, 'String') || !is(regex, 'RegExp'))
|
524 | return false;
|
525 | return !!value && regex.test(value);
|
526 | },
|
527 | message: 'Formato inválido.'
|
528 | };
|
529 | RuleContainer.add('regex', rule$4);
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 | var rule$5 = {
|
541 | validate: function (value) {
|
542 | if (Array.isArray(value))
|
543 | return !!value.length;
|
544 | if (typeof value === 'object')
|
545 | return !!Object.keys(value).length;
|
546 | if (typeof value === 'string')
|
547 | return !!value.trim().length;
|
548 | if (typeof value === 'number')
|
549 | return !!value;
|
550 | if (typeof value === 'boolean')
|
551 | return true;
|
552 | return !!value;
|
553 | },
|
554 | message: 'Campo obrigatório.'
|
555 | };
|
556 | RuleContainer.add('required', rule$5);
|
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 | var rule$6 = {
|
571 | validate: function (value, maxLength) {
|
572 | if (!value)
|
573 | return true;
|
574 | if (Array.isArray(value) || typeof value === 'string')
|
575 | return (value || []).length <= maxLength;
|
576 | return maxLength >= value;
|
577 | },
|
578 | message: 'Valor acima do limite.'
|
579 | };
|
580 | RuleContainer.add('maxLength', rule$6);
|
581 |
|
582 |
|
583 |
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 | var rule$7 = {
|
595 | validate: function (value, minLength) {
|
596 | if (!value)
|
597 | return true;
|
598 | if (Array.isArray(value) || typeof value === 'string')
|
599 | return (value || []).length >= minLength;
|
600 | return value >= minLength;
|
601 | },
|
602 | message: 'Valor abaixo do limite.'
|
603 | };
|
604 | RuleContainer.add('minLength', rule$7);
|
605 |
|
606 | var rule$8 = {
|
607 | validate: function (value) {
|
608 | if (!value)
|
609 | return true;
|
610 | var FTAP = '3298765432';
|
611 | var numPis = (value + '').replace(/[^\d]+/g, '');
|
612 | var total = FTAP
|
613 | .split('')
|
614 | .reduce(function (total, digit, index) { return total + (+numPis[index] * +digit); }, 0);
|
615 | var rest = (total % 11);
|
616 | if (rest !== 0)
|
617 | rest = 11 - rest;
|
618 | if (rest === 10 || rest === 11)
|
619 | rest = (rest + '').slice(1, 2);
|
620 | if (+rest !== +(numPis.slice(10, 11)))
|
621 | return false;
|
622 | return true;
|
623 | },
|
624 | message: 'Número de PIS inválido'
|
625 | };
|
626 | RuleContainer.add('pis', rule$8);
|
627 |
|
628 | var ScopedValidator = (function () {
|
629 | function ScopedValidator(vm) {
|
630 | var _this = this;
|
631 | this.scopes = [];
|
632 | this._options = {};
|
633 | this.validations = {};
|
634 | this._vm = vm;
|
635 | this.fields = new FieldBag();
|
636 | vm.$nextTick(function () {
|
637 | if (_this._vm.$options.validatorOptions)
|
638 | _this.options = _this._vm.$options.validatorOptions;
|
639 | if (_this._vm.$options.validations)
|
640 | _this.init(_this._vm.$options.validations);
|
641 | });
|
642 | }
|
643 | Object.defineProperty(ScopedValidator.prototype, "options", {
|
644 | get: function () {
|
645 | return this._options;
|
646 | },
|
647 | set: function (options) {
|
648 | this._options = is(options, 'Function')
|
649 | ? options(this._vm)
|
650 | : __assign({}, options);
|
651 | },
|
652 | enumerable: true,
|
653 | configurable: true
|
654 | });
|
655 | |
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 | ScopedValidator.prototype.init = function (template) {
|
668 | this._vm.$nextTick(this.__init.bind(this, template));
|
669 | };
|
670 | ScopedValidator.prototype.__init = function (template) {
|
671 | this.scopes = Object.keys(template).filter(function (key) { return isFormScope(template[key]); });
|
672 | this.fields.items = this.initFields(template);
|
673 | this.validations = this.mapValidations();
|
674 | };
|
675 | |
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 | ScopedValidator.prototype.initFields = function (template) {
|
685 | var _this = this;
|
686 | var _a;
|
687 | var mapField = function (name, rules, scope) {
|
688 | var fieldOptions = {
|
689 | name: name,
|
690 | rules: rules,
|
691 | scope: scope,
|
692 | vm: _this._vm,
|
693 | el: _this.getFieldEl(name, scope),
|
694 | value: scope ? _this._vm[scope][name] : _this._vm[name],
|
695 | };
|
696 | return new Field(fieldOptions);
|
697 | };
|
698 |
|
699 |
|
700 |
|
701 | var scopes = this.scopes.map(function (scope) {
|
702 | var formScope = template[scope];
|
703 | return Object.keys(formScope)
|
704 | .map(function (fieldName) { return mapField(fieldName, formScope[fieldName], scope); });
|
705 | });
|
706 | var fields = Object.keys(template)
|
707 | .filter(function (key) { return !isFormScope(template[key]); })
|
708 | .map(function (key) { return mapField(key, template[key]); });
|
709 | return (_a = Array.prototype).concat.apply(_a, [fields].concat(scopes));
|
710 | };
|
711 | |
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 | ScopedValidator.prototype.mapValidations = function () {
|
719 | var _this = this;
|
720 | var mapFlags = function (scope) { return _this.fields.all(scope)
|
721 | .reduce(function (acc, field) {
|
722 | var _a;
|
723 | return (__assign({}, acc, (_a = {}, _a[field.name] = field.flags, _a)));
|
724 | }, {}); };
|
725 | var mapFormScopes = function (acc, scope) {
|
726 | var _a;
|
727 | return (__assign({}, acc, (_a = {}, _a[scope] = mapFlags(scope), _a)));
|
728 | };
|
729 | return this.scopes.length > 1
|
730 | ? this.scopes.reduce(mapFormScopes, {})
|
731 | : mapFlags();
|
732 | };
|
733 | |
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 | ScopedValidator.prototype.getFieldEl = function (fieldName, scope) {
|
744 | var fieldQuery = scope
|
745 | ? "form[name=\"" + scope + "\"] [name=\"" + fieldName + "\"]"
|
746 | : "[name=\"" + fieldName + "\"]";
|
747 | var fields = this._vm.$el.querySelectorAll(fieldQuery);
|
748 | if (process.env.NODE_ENV !== 'production' && !fields.length)
|
749 | console.warn("CeeValidate: Field \"" + fieldName + "\" could not be found in the DOM");
|
750 | return fields[0];
|
751 | };
|
752 | |
753 |
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 | ScopedValidator.prototype.validate = function (fieldName, scope) {
|
760 | var field = this.fields.get(fieldName, scope);
|
761 |
|
762 | if (!field)
|
763 | return false;
|
764 | var mapErrors = function (_a) {
|
765 | var ruleName = _a.ruleName, args = _a.args;
|
766 | var rule = RuleContainer.getRule(ruleName);
|
767 | var hasError = !rule.validate.apply(null, [field.value].concat((args || [])));
|
768 | var errorMessage = rule.message;
|
769 | return hasError ? errorMessage : '';
|
770 | };
|
771 | var fieldErrors = field.rules
|
772 | .map(mapErrors)
|
773 | .filter(function (message) { return !!message; });
|
774 | field.setFlag('errors', fieldErrors);
|
775 | field.setFlag('valid', !fieldErrors.length);
|
776 | return !fieldErrors.length;
|
777 | };
|
778 | |
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 | ScopedValidator.prototype.validateAll = function (scope) {
|
787 | var fieldFlags = this.fields.all(scope)
|
788 | .map(function (field) { return field.validate(); });
|
789 | var isValid = fieldFlags.every(function (isValid) { return !!isValid; });
|
790 | this.options = __assign({}, (this.options || {}), { noListeners: false });
|
791 | return isValid;
|
792 | };
|
793 | |
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 | ScopedValidator.prototype.reset = function (scope) {
|
802 | this.fields.all(scope).forEach(function (field) { return field.reset(); });
|
803 | };
|
804 | |
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 | ScopedValidator.prototype.attach = function (field) {
|
813 | var newField = new Field({
|
814 | vm: this._vm,
|
815 | name: field.name,
|
816 | rules: field.rules,
|
817 | scope: field.scope,
|
818 | el: this.getFieldEl(field.name, field.scope),
|
819 | value: field.scope ? this._vm[field.scope][field.name] : this._vm[field.name]
|
820 | });
|
821 | this.fields.push(newField);
|
822 | this.validations = this.mapValidations();
|
823 | };
|
824 | |
825 |
|
826 |
|
827 |
|
828 |
|
829 |
|
830 |
|
831 |
|
832 |
|
833 | ScopedValidator.prototype.detach = function (field, scope) {
|
834 | this.fields.remove(field, scope);
|
835 | this.validations = this.mapValidations();
|
836 | };
|
837 | |
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 | ScopedValidator.prototype.setFieldRule = function (field, rules) {
|
846 | var fieldInstance = this.fields.get(field.name, field.scope);
|
847 | if (!fieldInstance)
|
848 | return;
|
849 | fieldInstance.setRule(rules);
|
850 | if (fieldInstance.flags.changed)
|
851 | fieldInstance.validate();
|
852 | };
|
853 | return ScopedValidator;
|
854 | }());
|
855 |
|
856 |
|
857 | var FormValidator = (function (_super) {
|
858 | __extends(FormValidator, _super);
|
859 | function FormValidator() {
|
860 | return _super !== null && _super.apply(this, arguments) || this;
|
861 | }
|
862 |
|
863 |
|
864 | FormValidator.prototype.beforeCreate = function () {
|
865 | var _this = this;
|
866 |
|
867 | var Vue$$1 = this.$options._base;
|
868 | this.$validator = new ScopedValidator(this);
|
869 |
|
870 | if (!this.$options.computed)
|
871 | this.$options.computed = {};
|
872 | Vue$$1.util.defineReactive(this.$validator, 'validations', this.$validator.validations);
|
873 | this.$options.computed['$validations'] = function () { return _this.$validator.validations; };
|
874 | };
|
875 | FormValidator = __decorate([
|
876 | Component
|
877 | ], FormValidator);
|
878 | return FormValidator;
|
879 | }(Vue));
|
880 |
|
881 | export default FormValidator;
|
882 |
|