@impelsys/validatorjs 3.22.4 → 3.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/validator.js CHANGED
@@ -1,2183 +1,2 @@
1
- /*! @impelsys/validatorjs - 2023-06-27 */
2
- (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Validator = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
3
- function AsyncResolvers(onFailedOne, onResolvedAll) {
4
- this.onResolvedAll = onResolvedAll;
5
- this.onFailedOne = onFailedOne;
6
- this.resolvers = {};
7
- this.resolversCount = 0;
8
- this.passed = [];
9
- this.failed = [];
10
- this.firing = false;
11
- }
12
-
13
- AsyncResolvers.prototype = {
14
-
15
- /**
16
- * Add resolver
17
- *
18
- * @param {Rule} rule
19
- * @return {integer}
20
- */
21
- add: function(rule) {
22
- var index = this.resolversCount;
23
- this.resolvers[index] = rule;
24
- this.resolversCount++;
25
- return index;
26
- },
27
-
28
- /**
29
- * Resolve given index
30
- *
31
- * @param {integer} index
32
- * @return {void}
33
- */
34
- resolve: function(index) {
35
- var rule = this.resolvers[index];
36
- if (rule.passes === true) {
37
- this.passed.push(rule);
38
- } else if (rule.passes === false) {
39
- this.failed.push(rule);
40
- this.onFailedOne(rule);
41
- }
42
-
43
- this.fire();
44
- },
45
-
46
- /**
47
- * Determine if all have been resolved
48
- *
49
- * @return {boolean}
50
- */
51
- isAllResolved: function() {
52
- return (this.passed.length + this.failed.length) === this.resolversCount;
53
- },
54
-
55
- /**
56
- * Attempt to fire final all resolved callback if completed
57
- *
58
- * @return {void}
59
- */
60
- fire: function() {
61
-
62
- if (!this.firing) {
63
- return;
64
- }
65
-
66
- if (this.isAllResolved()) {
67
- this.onResolvedAll(this.failed.length === 0);
68
- }
69
-
70
- },
71
-
72
- /**
73
- * Enable firing
74
- *
75
- * @return {void}
76
- */
77
- enableFiring: function() {
78
- this.firing = true;
79
- }
80
-
81
- };
82
-
83
- module.exports = AsyncResolvers;
84
-
85
- },{}],2:[function(require,module,exports){
86
- var replacements = {
87
-
88
- /**
89
- * Between replacement (replaces :min and :max)
90
- *
91
- * @param {string} template
92
- * @param {Rule} rule
93
- * @return {string}
94
- */
95
- between: function(template, rule) {
96
- var parameters = rule.getParameters();
97
- return this._replacePlaceholders(rule, template, {
98
- min: parameters[0],
99
- max: parameters[1]
100
- });
101
- },
102
-
103
- /**
104
- * Digits-Between replacement (replaces :min and :max)
105
- *
106
- * @param {string} template
107
- * @param {Rule} rule
108
- * @return {string}
109
- */
110
- digits_between: function(template, rule) {
111
- var parameters = rule.getParameters();
112
- return this._replacePlaceholders(rule, template, {
113
- min: parameters[0],
114
- max: parameters[1]
115
- });
116
- },
117
-
118
- /**
119
- * Required_if replacement.
120
- *
121
- * @param {string} template
122
- * @param {Rule} rule
123
- * @return {string}
124
- */
125
- required_if: function(template, rule) {
126
- var parameters = rule.getParameters();
127
- return this._replacePlaceholders(rule, template, {
128
- other: this._getAttributeName(parameters[0]),
129
- value: parameters[1]
130
- });
131
- },
132
-
133
- /**
134
- * Required_unless replacement.
135
- *
136
- * @param {string} template
137
- * @param {Rule} rule
138
- * @return {string}
139
- */
140
- required_unless: function(template, rule) {
141
- var parameters = rule.getParameters();
142
- return this._replacePlaceholders(rule, template, {
143
- other: this._getAttributeName(parameters[0]),
144
- value: parameters[1]
145
- });
146
- },
147
-
148
- /**
149
- * Required_with replacement.
150
- *
151
- * @param {string} template
152
- * @param {Rule} rule
153
- * @return {string}
154
- */
155
- required_with: function(template, rule) {
156
- var parameters = rule.getParameters();
157
- return this._replacePlaceholders(rule, template, {
158
- field: this._getAttributeName(parameters[0])
159
- });
160
- },
161
-
162
- /**
163
- * Required_with_all replacement.
164
- *
165
- * @param {string} template
166
- * @param {Rule} rule
167
- * @return {string}
168
- */
169
- required_with_all: function(template, rule) {
170
- var parameters = rule.getParameters();
171
- var getAttributeName = this._getAttributeName.bind(this);
172
- return this._replacePlaceholders(rule, template, {
173
- fields: parameters.map(getAttributeName).join(', ')
174
- });
175
- },
176
-
177
- /**
178
- * Required_without replacement.
179
- *
180
- * @param {string} template
181
- * @param {Rule} rule
182
- * @return {string}
183
- */
184
- required_without: function(template, rule) {
185
- var parameters = rule.getParameters();
186
- return this._replacePlaceholders(rule, template, {
187
- field: this._getAttributeName(parameters[0])
188
- });
189
- },
190
-
191
- /**
192
- * Required_without_all replacement.
193
- *
194
- * @param {string} template
195
- * @param {Rule} rule
196
- * @return {string}
197
- */
198
- required_without_all: function(template, rule) {
199
- var parameters = rule.getParameters();
200
- var getAttributeName = this._getAttributeName.bind(this);
201
- return this._replacePlaceholders(rule, template, {
202
- fields: parameters.map(getAttributeName).join(', ')
203
- });
204
- },
205
-
206
- /**
207
- * After replacement.
208
- *
209
- * @param {string} template
210
- * @param {Rule} rule
211
- * @return {string}
212
- */
213
- after: function(template, rule) {
214
- var parameters = rule.getParameters();
215
- return this._replacePlaceholders(rule, template, {
216
- after: this._getAttributeName(parameters[0])
217
- });
218
- },
219
-
220
- /**
221
- * Before replacement.
222
- *
223
- * @param {string} template
224
- * @param {Rule} rule
225
- * @return {string}
226
- */
227
- before: function(template, rule) {
228
- var parameters = rule.getParameters();
229
- return this._replacePlaceholders(rule, template, {
230
- before: this._getAttributeName(parameters[0])
231
- });
232
- },
233
-
234
- /**
235
- * After_or_equal replacement.
236
- *
237
- * @param {string} template
238
- * @param {Rule} rule
239
- * @return {string}
240
- */
241
- after_or_equal: function(template, rule) {
242
- var parameters = rule.getParameters();
243
- return this._replacePlaceholders(rule, template, {
244
- after_or_equal: this._getAttributeName(parameters[0])
245
- });
246
- },
247
-
248
- /**
249
- * Before_or_equal replacement.
250
- *
251
- * @param {string} template
252
- * @param {Rule} rule
253
- * @return {string}
254
- */
255
- before_or_equal: function(template, rule) {
256
- var parameters = rule.getParameters();
257
- return this._replacePlaceholders(rule, template, {
258
- before_or_equal: this._getAttributeName(parameters[0])
259
- });
260
- },
261
-
262
- /**
263
- * Same replacement.
264
- *
265
- * @param {string} template
266
- * @param {Rule} rule
267
- * @return {string}
268
- */
269
- same: function(template, rule) {
270
- var parameters = rule.getParameters();
271
- return this._replacePlaceholders(rule, template, {
272
- same: this._getAttributeName(parameters[0])
273
- });
274
- },
275
- };
276
-
277
- function formatter(attribute) {
278
- return attribute.replace(/[_\[]/g, ' ').replace(/]/g, '');
279
- }
280
-
281
- module.exports = {
282
- replacements: replacements,
283
- formatter: formatter
284
- };
285
-
286
- },{}],3:[function(require,module,exports){
287
- var Errors = function() {
288
- this.errors = {};
289
- };
290
-
291
- Errors.prototype = {
292
- constructor: Errors,
293
-
294
- /**
295
- * Add new error message for given attribute
296
- *
297
- * @param {string} attribute
298
- * @param {string} message
299
- * @return {void}
300
- */
301
- add: function(attribute, message) {
302
- if (!this.has(attribute)) {
303
- this.errors[attribute] = [];
304
- }
305
-
306
- if (this.errors[attribute].indexOf(message) === -1) {
307
- this.errors[attribute].push(message);
308
- }
309
- },
310
-
311
- /**
312
- * Returns an array of error messages for an attribute, or an empty array
313
- *
314
- * @param {string} attribute A key in the data object being validated
315
- * @return {array} An array of error messages
316
- */
317
- get: function(attribute) {
318
- if (this.has(attribute)) {
319
- return this.errors[attribute];
320
- }
321
-
322
- return [];
323
- },
324
-
325
- /**
326
- * Returns the first error message for an attribute, false otherwise
327
- *
328
- * @param {string} attribute A key in the data object being validated
329
- * @return {string|false} First error message or false
330
- */
331
- first: function(attribute) {
332
- if (this.has(attribute)) {
333
- return this.errors[attribute][0];
334
- }
335
-
336
- return false;
337
- },
338
-
339
- /**
340
- * Get all error messages from all failing attributes
341
- *
342
- * @return {Object} Failed attribute names for keys and an array of messages for values
343
- */
344
- all: function() {
345
- return this.errors;
346
- },
347
-
348
- /**
349
- * Determine if there are any error messages for an attribute
350
- *
351
- * @param {string} attribute A key in the data object being validated
352
- * @return {boolean}
353
- */
354
- has: function(attribute) {
355
- if (this.errors.hasOwnProperty(attribute)) {
356
- return true;
357
- }
358
-
359
- return false;
360
- }
361
- };
362
-
363
- module.exports = Errors;
364
-
365
- },{}],4:[function(require,module,exports){
366
- var Messages = require('./messages');
367
-
368
- require('./lang/en');
369
-
370
- var require_method = require;
371
-
372
- var container = {
373
-
374
- messages: {},
375
-
376
- /**
377
- * Set messages for language
378
- *
379
- * @param {string} lang
380
- * @param {object} rawMessages
381
- * @return {void}
382
- */
383
- _set: function(lang, rawMessages) {
384
- this.messages[lang] = rawMessages;
385
- },
386
-
387
- /**
388
- * Set message for given language's rule.
389
- *
390
- * @param {string} lang
391
- * @param {string} attribute
392
- * @param {string|object} message
393
- * @return {void}
394
- */
395
- _setRuleMessage: function(lang, attribute, message) {
396
- this._load(lang);
397
- if (message === undefined) {
398
- message = this.messages[lang].def;
399
- }
400
-
401
- this.messages[lang][attribute] = message;
402
- },
403
-
404
- /**
405
- * Load messages (if not already loaded)
406
- *
407
- * @param {string} lang
408
- * @return {void}
409
- */
410
- _load: function(lang) {
411
- if (!this.messages[lang]) {
412
- try {
413
- var rawMessages = require_method('./lang/' + lang);
414
- this._set(lang, rawMessages);
415
- } catch (e) {}
416
- }
417
- },
418
-
419
- /**
420
- * Get raw messages for language
421
- *
422
- * @param {string} lang
423
- * @return {object}
424
- */
425
- _get: function(lang) {
426
- this._load(lang);
427
- return this.messages[lang];
428
- },
429
-
430
- /**
431
- * Make messages for given language
432
- *
433
- * @param {string} lang
434
- * @return {Messages}
435
- */
436
- _make: function(lang) {
437
- this._load(lang);
438
- return new Messages(lang, this.messages[lang]);
439
- }
440
-
441
- };
442
-
443
- module.exports = container;
444
-
445
- },{"./lang/en":5,"./messages":6}],5:[function(require,module,exports){
446
- module.exports = {
447
- accepted: 'The :attribute must be accepted.',
448
- after: 'The :attribute must be after :after.',
449
- after_or_equal: 'The :attribute must be equal or after :after_or_equal.',
450
- alpha: 'The :attribute field must contain only alphabetic characters.',
451
- alpha_dash: 'The :attribute field may only contain alpha-numeric characters, as well as dashes and underscores.',
452
- alpha_num: 'The :attribute field must be alphanumeric.',
453
- before: 'The :attribute must be before :before.',
454
- before_or_equal: 'The :attribute must be equal or before :before_or_equal.',
455
- between: {
456
- numeric: 'The :attribute field must be between :min and :max.',
457
- string: 'The :attribute field must be between :min and :max characters.',
458
- },
459
- confirmed: 'The :attribute confirmation does not match.',
460
- email: 'The :attribute format is invalid.',
461
- date: 'The :attribute is not a valid date format.',
462
- def: 'The :attribute attribute has errors.',
463
- digits: 'The :attribute must be :digits digits.',
464
- digits_between: 'The :attribute field must be between :min and :max digits.',
465
- different: 'The :attribute and :different must be different.',
466
- in: 'The selected :attribute is invalid.',
467
- integer: 'The :attribute must be an integer.',
468
- hex: 'The :attribute field should have hexadecimal format',
469
- min: {
470
- numeric: 'The :attribute must be at least :min.',
471
- string: 'The :attribute must be at least :min characters.'
472
- },
473
- max: {
474
- numeric: 'The :attribute may not be greater than :max.',
475
- string: 'The :attribute may not be greater than :max characters.'
476
- },
477
- not_in: 'The selected :attribute is invalid.',
478
- numeric: 'The :attribute must be a number.',
479
- present: 'The :attribute field must be present (but can be empty).',
480
- required: 'The :attribute field is required.',
481
- required_if: 'The :attribute field is required when :other is :value.',
482
- required_unless: 'The :attribute field is required when :other is not :value.',
483
- required_with: 'The :attribute field is required when :field is not empty.',
484
- required_with_all: 'The :attribute field is required when :fields are not empty.',
485
- required_without: 'The :attribute field is required when :field is empty.',
486
- required_without_all: 'The :attribute field is required when :fields are empty.',
487
- same: 'The :attribute and :same fields must match.',
488
- size: {
489
- numeric: 'The :attribute must be :size.',
490
- string: 'The :attribute must be :size characters.'
491
- },
492
- string: 'The :attribute must be a string.',
493
- url: 'The :attribute format is invalid.',
494
- regex: 'The :attribute format is invalid.',
495
- attributes: {}
496
- };
497
-
498
- },{}],6:[function(require,module,exports){
499
- var Attributes = require('./attributes');
500
-
501
- var Messages = function(lang, messages) {
502
- this.lang = lang;
503
- this.messages = messages;
504
- this.customMessages = {};
505
- this.attributeNames = {};
506
- };
507
-
508
- Messages.prototype = {
509
- constructor: Messages,
510
-
511
- /**
512
- * Set custom messages
513
- *
514
- * @param {object} customMessages
515
- * @return {void}
516
- */
517
- _setCustom: function(customMessages) {
518
- this.customMessages = customMessages || {};
519
- },
520
-
521
- /**
522
- * Set custom attribute names.
523
- *
524
- * @param {object} attributes
525
- */
526
- _setAttributeNames: function(attributes) {
527
- this.attributeNames = attributes;
528
- },
529
-
530
- /**
531
- * Set the attribute formatter.
532
- *
533
- * @param {fuction} func
534
- * @return {void}
535
- */
536
- _setAttributeFormatter: function(func) {
537
- this.attributeFormatter = func;
538
- },
539
-
540
- /**
541
- * Get attribute name to display.
542
- *
543
- * @param {string} attribute
544
- * @return {string}
545
- */
546
- _getAttributeName: function(attribute) {
547
- var name = attribute;
548
- if (this.attributeNames.hasOwnProperty(attribute)) {
549
- return this.attributeNames[attribute];
550
- } else if (this.messages.attributes.hasOwnProperty(attribute)) {
551
- name = this.messages.attributes[attribute];
552
- }
553
-
554
- if (this.attributeFormatter) {
555
- name = this.attributeFormatter(name);
556
- }
557
-
558
- return name;
559
- },
560
-
561
- /**
562
- * Get all messages
563
- *
564
- * @return {object}
565
- */
566
- all: function() {
567
- return this.messages;
568
- },
569
-
570
- /**
571
- * Render message
572
- *
573
- * @param {Rule} rule
574
- * @return {string}
575
- */
576
- render: function(rule) {
577
- if (rule.customMessage) {
578
- return rule.customMessage;
579
- }
580
- var template = this._getTemplate(rule);
581
-
582
- var message;
583
- if (Attributes.replacements[rule.name]) {
584
- message = Attributes.replacements[rule.name].apply(this, [template, rule]);
585
- } else {
586
- message = this._replacePlaceholders(rule, template, {});
587
- }
588
-
589
- return message;
590
- },
591
-
592
- /**
593
- * Get the template to use for given rule
594
- *
595
- * @param {Rule} rule
596
- * @return {string}
597
- */
598
- _getTemplate: function(rule) {
599
-
600
- var messages = this.messages;
601
- var template = messages.def;
602
- var customMessages = this.customMessages;
603
- var formats = [rule.name + '.' + rule.attribute, rule.name];
604
-
605
- for (var i = 0, format; i < formats.length; i++) {
606
- format = formats[i];
607
- if (customMessages.hasOwnProperty(format)) {
608
- template = customMessages[format];
609
- break;
610
- } else if (messages.hasOwnProperty(format)) {
611
- template = messages[format];
612
- break;
613
- }
614
- }
615
-
616
- if (typeof template === 'object') {
617
- template = template[rule._getValueType()];
618
- }
619
-
620
- return template;
621
- },
622
-
623
- /**
624
- * Replace placeholders in the template using the data object
625
- *
626
- * @param {Rule} rule
627
- * @param {string} template
628
- * @param {object} data
629
- * @return {string}
630
- */
631
- _replacePlaceholders: function(rule, template, data) {
632
- var message, attribute;
633
-
634
- data.attribute = this._getAttributeName(rule.attribute);
635
- data[rule.name] = data[rule.name] || rule.getParameters().join(',');
636
-
637
- if (typeof template === 'string' && typeof data === 'object') {
638
- message = template;
639
-
640
- for (attribute in data) {
641
- message = message.replace(new RegExp(':' + attribute, 'g'), data[attribute]);
642
- }
643
- }
644
-
645
- return message;
646
- }
647
-
648
- };
649
-
650
- module.exports = Messages;
651
-
652
- },{"./attributes":2}],7:[function(require,module,exports){
653
-
654
- // https://docs.microsoft.com/en-us/office/troubleshoot/excel/determine-a-leap-year
655
- function leapYear(year) {
656
- return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
657
- }
658
-
659
- function checkFalsePositiveDates(dateString = '') {
660
-
661
- if (dateString.length === 10) {
662
-
663
- // massage input to use yyyy-mm-dd format
664
- // we support yyyy/mm/dd or yyyy.mm.dd
665
- let normalizedDate = dateString.replace('.', '-').replace('/', '-');
666
- let parts = normalizedDate.split('-');
667
- if (parts.length === 3) {
668
- if (parts[0].length === 4) {
669
- // yyyy-mm-dd format
670
- let y = parseInt(parts[0]);
671
- let m = parseInt(parts[1]);
672
- let d = parseInt(parts[2]);
673
- if (m === 2) {
674
- // return leapYear(y) ? d <= 29 : d <= 28;
675
- if (leapYear(y)) {
676
- if (d > 29) {
677
- return false;
678
- }
679
- } else {
680
- if (d > 28) {
681
- return false;
682
- }
683
- }
684
- }
685
- if (m === 4 || m === 6 || m === 9 || m === 11) {
686
- if (d > 30) {
687
- return false;
688
- }
689
- }
690
- }
691
- }
692
- return true; // we are not in feburary, proceed
693
- }
694
- return true; // we are not testing formatted date, proceed to rest of validation
695
- }
696
-
697
- function isValidDate(dateString) {
698
- let testDate;
699
- if (typeof dateString === 'number') {
700
- testDate = new Date(dateString);
701
- if (typeof testDate === 'object') {
702
- return true;
703
- }
704
- }
705
- // first convert incoming string to date object and see if it correct date and format
706
- testDate = new Date(dateString);
707
- if (typeof testDate === 'object') {
708
- if (testDate.toString() === 'Invalid Date') {
709
- return false;
710
- }
711
-
712
- /**
713
- * Check for false positive dates
714
- * perform special check on february as JS `new Date` incorrectly returns valid date
715
- * Eg. let newDate = new Date('2020-02-29') // returns as March 02 2020
716
- * Eg. let newDate = new Date('2019-02-29') // returns as March 01 2020
717
- * Eg. let newDate = new Date('2019-04-31') // returns as April 30 2020
718
- */
719
- if (!checkFalsePositiveDates(dateString)) {
720
- return false;
721
- }
722
-
723
- // valid date object and not a february date
724
- return true;
725
- }
726
-
727
- // First check for the pattern
728
- var regex_date = /^\d{4}\-\d{1,2}\-\d{1,2}$/;
729
-
730
- if (!regex_date.test(dateString)) {
731
- return false;
732
- }
733
-
734
- // Parse the date parts to integers
735
- var parts = dateString.split("-");
736
- var day = parseInt(parts[2], 10);
737
- var month = parseInt(parts[1], 10);
738
- var year = parseInt(parts[0], 10);
739
-
740
- // Check the ranges of month and year
741
- if (year < 1000 || year > 3000 || month == 0 || month > 12) {
742
- return false;
743
- }
744
-
745
- var monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
746
-
747
- // Adjust for leap years
748
- if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
749
- monthLength[1] = 29;
750
- }
751
-
752
- // Check the range of the day
753
- return day > 0 && day <= monthLength[month - 1];
754
- }
755
-
756
- var rules = {
757
- required: function (val) {
758
- var str;
759
-
760
- if (val === undefined || val === null) {
761
- return false;
762
- }
763
-
764
- str = String(val).replace(/\s/g, "");
765
- return str.length > 0 ? true : false;
766
- },
767
-
768
- required_if: function (val, req, attribute) {
769
- req = this.getParameters();
770
- if (this.validator._objectPath(this.validator.input, req[0]) === req[1]) {
771
- return this.validator.getRule("required").validate(val);
772
- }
773
-
774
- return true;
775
- },
776
-
777
- required_unless: function (val, req, attribute) {
778
- req = this.getParameters();
779
- if (this.validator._objectPath(this.validator.input, req[0]) !== req[1]) {
780
- return this.validator.getRule("required").validate(val);
781
- }
782
-
783
- return true;
784
- },
785
-
786
- required_with: function (val, req, attribute) {
787
- if (this.validator._objectPath(this.validator.input, req)) {
788
- return this.validator.getRule("required").validate(val);
789
- }
790
-
791
- return true;
792
- },
793
-
794
- required_with_all: function (val, req, attribute) {
795
- req = this.getParameters();
796
-
797
- for (var i = 0; i < req.length; i++) {
798
- if (!this.validator._objectPath(this.validator.input, req[i])) {
799
- return true;
800
- }
801
- }
802
-
803
- return this.validator.getRule("required").validate(val);
804
- },
805
-
806
- required_without: function (val, req, attribute) {
807
- if (this.validator._objectPath(this.validator.input, req)) {
808
- return true;
809
- }
810
-
811
- return this.validator.getRule("required").validate(val);
812
- },
813
-
814
- required_without_all: function (val, req, attribute) {
815
- req = this.getParameters();
816
-
817
- for (var i = 0; i < req.length; i++) {
818
- if (this.validator._objectPath(this.validator.input, req[i])) {
819
- return true;
820
- }
821
- }
822
-
823
- return this.validator.getRule("required").validate(val);
824
- },
825
-
826
- boolean: function (val) {
827
- return (
828
- val === true ||
829
- val === false ||
830
- val === 0 ||
831
- val === 1 ||
832
- val === "0" ||
833
- val === "1" ||
834
- val === "true" ||
835
- val === "false"
836
- );
837
- },
838
-
839
- // compares the size of strings
840
- // with numbers, compares the value
841
- size: function (val, req, attribute) {
842
- if (val) {
843
- req = parseFloat(req);
844
-
845
- var size = this.getSize();
846
-
847
- return size === req;
848
- }
849
-
850
- return true;
851
- },
852
-
853
- string: function (val, req, attribute) {
854
- return typeof val === "string";
855
- },
856
-
857
- sometimes: function (val) {
858
- return true;
859
- },
860
-
861
- /**
862
- * Compares the size of strings or the value of numbers if there is a truthy value
863
- */
864
- min: function (val, req, attribute) {
865
- var size = this.getSize();
866
- return size >= req;
867
- },
868
-
869
- /**
870
- * Compares the size of strings or the value of numbers if there is a truthy value
871
- */
872
- max: function (val, req, attribute) {
873
- var size = this.getSize();
874
- return size <= req;
875
- },
876
-
877
- between: function (val, req, attribute) {
878
- req = this.getParameters();
879
- var size = this.getSize();
880
- var min = parseFloat(req[0], 10);
881
- var max = parseFloat(req[1], 10);
882
- return size >= min && size <= max;
883
- },
884
-
885
- email: function (val) {
886
- // Added umlaut support https://github.com/skaterdav85/validatorjs/issues/308
887
- var re = /^(([^<>()[\]\\.,;:\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,}))$/;
888
- if (!re.test(val)) {
889
- // added support domain 3-n level https://github.com/skaterdav85/validatorjs/issues/384
890
- re = /^((?:[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]|[^\u0000-\u007F])+@(?:[a-zA-Z0-9]|[^\u0000-\u007F])(?:(?:[a-zA-Z0-9-]|[^\u0000-\u007F]){0,61}(?:[a-zA-Z0-9]|[^\u0000-\u007F]))?(?:\.(?:[a-zA-Z0-9]|[^\u0000-\u007F])(?:(?:[a-zA-Z0-9-]|[^\u0000-\u007F]){0,61}(?:[a-zA-Z0-9]|[^\u0000-\u007F]))?)+)*$/;
891
- }
892
- return re.test(val);
893
- },
894
-
895
- numeric: function (val) {
896
- var num;
897
-
898
- num = Number(val); // tries to convert value to a number. useful if value is coming from form element
899
-
900
- if (typeof num === "number" && !isNaN(num) && typeof val !== "boolean") {
901
- return true;
902
- } else {
903
- return false;
904
- }
905
- },
906
-
907
- array: function (val) {
908
- return val instanceof Array;
909
- },
910
-
911
- url: function (url) {
912
- return /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,63}\b([-a-zA-Z0-9@:%_\+.~#?&/=]*)/i.test(url);
913
- },
914
-
915
- alpha: function (val) {
916
- return /^[a-zA-Z]+$/.test(val);
917
- },
918
-
919
- alpha_dash: function (val) {
920
- return /^[a-zA-Z0-9_\-]+$/.test(val);
921
- },
922
-
923
- alpha_num: function (val) {
924
- return /^[a-zA-Z0-9]+$/.test(val);
925
- },
926
-
927
- same: function (val, req) {
928
- var val1 = this.validator._flattenObject(this.validator.input)[req];
929
- var val2 = val;
930
-
931
- if (val1 === val2) {
932
- return true;
933
- }
934
-
935
- return false;
936
- },
937
-
938
- different: function (val, req) {
939
- var val1 = this.validator._flattenObject(this.validator.input)[req];
940
- var val2 = val;
941
-
942
- if (val1 !== val2) {
943
- return true;
944
- }
945
-
946
- return false;
947
- },
948
-
949
- in: function (val, req) {
950
- var list, i;
951
-
952
- if (val) {
953
- list = this.getParameters();
954
- }
955
-
956
- if (val && !(val instanceof Array)) {
957
- var localValue = val;
958
-
959
- for (i = 0; i < list.length; i++) {
960
- if (typeof list[i] === "string") {
961
- localValue = String(val);
962
- }
963
-
964
- if (localValue === list[i]) {
965
- return true;
966
- }
967
- }
968
-
969
- return false;
970
- }
971
-
972
- if (val && val instanceof Array) {
973
- for (i = 0; i < val.length; i++) {
974
- if (list.indexOf(val[i]) < 0) {
975
- return false;
976
- }
977
- }
978
- }
979
-
980
- return true;
981
- },
982
-
983
- not_in: function (val, req) {
984
- var list = this.getParameters();
985
- var len = list.length;
986
- var returnVal = true;
987
-
988
- for (var i = 0; i < len; i++) {
989
- var localValue = val;
990
-
991
- if (typeof list[i] === "string") {
992
- localValue = String(val);
993
- }
994
-
995
- if (localValue === list[i]) {
996
- returnVal = false;
997
- break;
998
- }
999
- }
1000
-
1001
- return returnVal;
1002
- },
1003
-
1004
- accepted: function (val) {
1005
- if (val === "on" || val === "yes" || val === 1 || val === "1" || val === true) {
1006
- return true;
1007
- }
1008
-
1009
- return false;
1010
- },
1011
-
1012
- confirmed: function (val, req, key) {
1013
- var confirmedKey = key + "_confirmation";
1014
-
1015
- if (this.validator.input[confirmedKey] === val) {
1016
- return true;
1017
- }
1018
-
1019
- return false;
1020
- },
1021
-
1022
- integer: function (val) {
1023
- return String(parseInt(val, 10)) === String(val);
1024
- },
1025
-
1026
- digits: function (val, req) {
1027
- var numericRule = this.validator.getRule('numeric');
1028
- if (numericRule.validate(val) && String(val.trim()).length === parseInt(req)) {
1029
- return true;
1030
- }
1031
-
1032
- return false;
1033
- },
1034
-
1035
- digits_between: function (val) {
1036
- var numericRule = this.validator.getRule("numeric");
1037
- var req = this.getParameters();
1038
- var valueDigitsCount = String(val).length;
1039
- var min = parseFloat(req[0], 10);
1040
- var max = parseFloat(req[1], 10);
1041
-
1042
- if (numericRule.validate(val) && valueDigitsCount >= min && valueDigitsCount <= max) {
1043
- return true;
1044
- }
1045
-
1046
- return false;
1047
- },
1048
-
1049
- regex: function (val, req) {
1050
- let reqPattern = req;
1051
- var mod = /[g|i|m]{1,3}$/;
1052
- var flag = req.match(mod);
1053
- flag = flag ? flag[0] : "";
1054
-
1055
- req = req.replace(mod, "").slice(1, -1);
1056
- req = new RegExp(req, flag);
1057
- return !!req.test(val);
1058
- },
1059
-
1060
- date: function (val, format) {
1061
- return isValidDate(val);
1062
- },
1063
-
1064
- present: function (val) {
1065
- return typeof val !== "undefined";
1066
- },
1067
-
1068
- after: function (val, req) {
1069
- var val1 = this.validator.input[req];
1070
- var val2 = val;
1071
-
1072
- if (!isValidDate(val1)) {
1073
- return false;
1074
- }
1075
- if (!isValidDate(val2)) {
1076
- return false;
1077
- }
1078
-
1079
- if (new Date(val1).getTime() < new Date(val2).getTime()) {
1080
- return true;
1081
- }
1082
-
1083
- return false;
1084
- },
1085
-
1086
- after_or_equal: function (val, req) {
1087
- var val1 = this.validator.input[req];
1088
- var val2 = val;
1089
-
1090
- if (!isValidDate(val1)) {
1091
- return false;
1092
- }
1093
- if (!isValidDate(val2)) {
1094
- return false;
1095
- }
1096
-
1097
- if (new Date(val1).getTime() <= new Date(val2).getTime()) {
1098
- return true;
1099
- }
1100
-
1101
- return false;
1102
- },
1103
-
1104
- before: function (val, req) {
1105
- var val1 = this.validator.input[req];
1106
- var val2 = val;
1107
-
1108
- if (!isValidDate(val1)) {
1109
- return false;
1110
- }
1111
- if (!isValidDate(val2)) {
1112
- return false;
1113
- }
1114
-
1115
- if (new Date(val1).getTime() > new Date(val2).getTime()) {
1116
- return true;
1117
- }
1118
-
1119
- return false;
1120
- },
1121
-
1122
- before_or_equal: function (val, req) {
1123
- var val1 = this.validator.input[req];
1124
- var val2 = val;
1125
-
1126
- if (!isValidDate(val1)) {
1127
- return false;
1128
- }
1129
- if (!isValidDate(val2)) {
1130
- return false;
1131
- }
1132
-
1133
- if (new Date(val1).getTime() >= new Date(val2).getTime()) {
1134
- return true;
1135
- }
1136
-
1137
- return false;
1138
- },
1139
-
1140
- hex: function (val) {
1141
- return /^[0-9a-f]+$/i.test(val);
1142
- },
1143
-
1144
- ipv4: function (val, req, attribute) {
1145
- if (typeof val != 'string')
1146
- return false;
1147
-
1148
- // regex to check that each octet is valid
1149
- var er = /^[0-9]+$/;
1150
- // ipv4 octets are delimited by dot
1151
- octets = val.split('.');
1152
- // check 1: ipv4 address should contains 4 octets
1153
- if (octets.length != 4)
1154
- return false;
1155
-
1156
- for (let i = 0; i < octets.length; i++) {
1157
- const element = octets[i];
1158
- // check 2: each octet should be integer bigger than 0
1159
- if (!er.test(element))
1160
- return false;
1161
-
1162
- // check 3: each octet value should be less than 256
1163
- var octetValue = parseInt(element);
1164
- if (octetValue >= 256)
1165
- return false;
1166
- }
1167
-
1168
- // if all checks passed, we know it's valid IPv4 address!
1169
- return true;
1170
- },
1171
-
1172
- ipv6: function (val, req, attribute) {
1173
- if (typeof val != 'string')
1174
- return false;
1175
-
1176
- // regex to check that each hextet is valid
1177
- var er = /^[0-9a-f]+$/;
1178
- // ipv6 hextets are delimited by colon
1179
- hextets = val.split(':');
1180
-
1181
- // check 1: ipv6 should contain only one consecutive colons
1182
- colons = val.match(/::/);
1183
- if (colons != null && val.match(/::/g).length > 1)
1184
- return false;
1185
-
1186
- // check 2: ipv6 should not be ending or starting with colon
1187
- // edge case: not with consecutive colons
1188
- if (val[0] == ':' && (colons == null || (colons != null && colons.index != 0)))
1189
- return false;
1190
- if (val[val.length - 1] == ':' && (colons == null || (colons != null && colons.index != val.length - 2)))
1191
- return false;
1192
-
1193
- // check 3: ipv6 should contain no less than 3 sector
1194
- // minimum ipv6 addres - ::1
1195
- if (3 > hextets.length)
1196
- return false;
1197
-
1198
- // check 4: ipv6 should contain no more than 8 sectors
1199
- // only 1 edge case: when first or last sector is ommited
1200
- var isEdgeCase = (hextets.length == 9 && colons != null && (colons.index == 0 || colons.index == val.length - 2));
1201
- if (hextets.length > 8 && !isEdgeCase)
1202
- return false;
1203
-
1204
- // check 5: ipv6 should contain exactly one consecutive colons if it has less than 8 sectors
1205
- if (hextets.length != 8 && colons == null)
1206
- return false;
1207
-
1208
- for (let i = 0; i < hextets.length; i++) {
1209
- const element = hextets[i];
1210
-
1211
- if (element.length == 0)
1212
- continue;
1213
-
1214
- // check 6: all of hextets should contain numbers from 0 to f (in hexadecimal)
1215
- if (!er.test(element))
1216
- return false;
1217
-
1218
- // check 7: all of hextet values should be less then ffff (in hexadeimal)
1219
- // checking using length of hextet. lowest invalid value's length is 5.
1220
- // so all valid hextets are length of 4 or less
1221
- if (element.length > 4)
1222
- return false;
1223
- }
1224
- return true;
1225
- },
1226
-
1227
- ip: function (val, req, attribute) {
1228
- return rules['ipv4'](val, req, attribute) || rules['ipv6'](val, req, attribute);
1229
- }
1230
-
1231
- };
1232
-
1233
- var missedRuleValidator = function () {
1234
- throw new Error("Validator `" + this.name + "` is not defined!");
1235
- };
1236
- var missedRuleMessage;
1237
-
1238
- function Rule(name, fn, async) {
1239
- this.name = name;
1240
- this.fn = fn;
1241
- this.passes = null;
1242
- this._customMessage = undefined;
1243
- this.async = async;
1244
- }
1245
-
1246
- Rule.prototype = {
1247
- /**
1248
- * Validate rule
1249
- *
1250
- * @param {mixed} inputValue
1251
- * @param {mixed} ruleValue
1252
- * @param {string} attribute
1253
- * @param {function} callback
1254
- * @return {boolean|undefined}
1255
- */
1256
- validate: function (inputValue, ruleValue, attribute, callback) {
1257
- var _this = this;
1258
- this._setValidatingData(attribute, inputValue, ruleValue);
1259
- if (typeof callback === "function") {
1260
- this.callback = callback;
1261
- var handleResponse = function (passes, message) {
1262
- _this.response(passes, message);
1263
- };
1264
-
1265
- if (this.async) {
1266
- return this._apply(inputValue, ruleValue, attribute, handleResponse);
1267
- } else {
1268
- return handleResponse(this._apply(inputValue, ruleValue, attribute));
1269
- }
1270
- }
1271
- return this._apply(inputValue, ruleValue, attribute);
1272
- },
1273
-
1274
- /**
1275
- * Apply validation function
1276
- *
1277
- * @param {mixed} inputValue
1278
- * @param {mixed} ruleValue
1279
- * @param {string} attribute
1280
- * @param {function} callback
1281
- * @return {boolean|undefined}
1282
- */
1283
- _apply: function (inputValue, ruleValue, attribute, callback) {
1284
- var fn = this.isMissed() ? missedRuleValidator : this.fn;
1285
-
1286
- return fn.apply(this, [inputValue, ruleValue, attribute, callback]);
1287
- },
1288
-
1289
- /**
1290
- * Set validating data
1291
- *
1292
- * @param {string} attribute
1293
- * @param {mixed} inputValue
1294
- * @param {mixed} ruleValue
1295
- * @return {void}
1296
- */
1297
- _setValidatingData: function (attribute, inputValue, ruleValue) {
1298
- this.attribute = attribute;
1299
- this.inputValue = inputValue;
1300
- this.ruleValue = ruleValue;
1301
- },
1302
-
1303
- /**
1304
- * Get parameters
1305
- *
1306
- * @return {array}
1307
- */
1308
- getParameters: function () {
1309
- var value = [];
1310
-
1311
- if (typeof this.ruleValue === "string") {
1312
- value = this.ruleValue.split(",");
1313
- }
1314
-
1315
- if (typeof this.ruleValue === "number") {
1316
- value.push(this.ruleValue);
1317
- }
1318
-
1319
- if (this.ruleValue instanceof Array) {
1320
- value = this.ruleValue;
1321
- }
1322
-
1323
- return value;
1324
- },
1325
-
1326
- /**
1327
- * Get true size of value
1328
- *
1329
- * @return {integer|float}
1330
- */
1331
- getSize: function () {
1332
- var value = this.inputValue;
1333
-
1334
- if (value instanceof Array) {
1335
- return value.length;
1336
- }
1337
-
1338
- if (typeof value === "number") {
1339
- return value;
1340
- }
1341
-
1342
- if (this.validator._hasNumericRule(this.attribute)) {
1343
- return parseFloat(value, 10);
1344
- }
1345
-
1346
- return value.length;
1347
- },
1348
-
1349
- /**
1350
- * Get the type of value being checked; numeric or string.
1351
- *
1352
- * @return {string}
1353
- */
1354
- _getValueType: function () {
1355
- if (typeof this.inputValue === "number" || this.validator._hasNumericRule(this.attribute)) {
1356
- return "numeric";
1357
- }
1358
-
1359
- return "string";
1360
- },
1361
-
1362
- /**
1363
- * Set the async callback response
1364
- *
1365
- * @param {boolean|undefined} passes Whether validation passed
1366
- * @param {string|undefined} message Custom error message
1367
- * @return {void}
1368
- */
1369
- response: function (passes, message) {
1370
- this.passes = passes === undefined || passes === true;
1371
- this._customMessage = message;
1372
- this.callback(this.passes, message);
1373
- },
1374
-
1375
- /**
1376
- * Set validator instance
1377
- *
1378
- * @param {Validator} validator
1379
- * @return {void}
1380
- */
1381
- setValidator: function (validator) {
1382
- this.validator = validator;
1383
- },
1384
-
1385
- /**
1386
- * Check if rule is missed
1387
- *
1388
- * @return {boolean}
1389
- */
1390
- isMissed: function () {
1391
- return typeof this.fn !== "function";
1392
- },
1393
-
1394
- get customMessage() {
1395
- return this.isMissed() ? missedRuleMessage : this._customMessage;
1396
- }
1397
- };
1398
-
1399
- var manager = {
1400
- /**
1401
- * List of async rule names
1402
- *
1403
- * @type {Array}
1404
- */
1405
- asyncRules: [],
1406
-
1407
- /**
1408
- * Implicit rules (rules to always validate)
1409
- *
1410
- * @type {Array}
1411
- */
1412
- implicitRules: [
1413
- "required",
1414
- "required_if",
1415
- "required_unless",
1416
- "required_with",
1417
- "required_with_all",
1418
- "required_without",
1419
- "required_without_all",
1420
- "accepted",
1421
- "present"
1422
- ],
1423
-
1424
- /**
1425
- * Get rule by name
1426
- *
1427
- * @param {string} name
1428
- * @param {Validator}
1429
- * @return {Rule}
1430
- */
1431
- make: function (name, validator) {
1432
- var async = this.isAsync(name);
1433
- var rule = new Rule(name, rules[name], async);
1434
- rule.setValidator(validator);
1435
- return rule;
1436
- },
1437
-
1438
- /**
1439
- * Determine if given rule is async
1440
- *
1441
- * @param {string} name
1442
- * @return {boolean}
1443
- */
1444
- isAsync: function (name) {
1445
- for (var i = 0, len = this.asyncRules.length; i < len; i++) {
1446
- if (this.asyncRules[i] === name) {
1447
- return true;
1448
- }
1449
- }
1450
- return false;
1451
- },
1452
-
1453
- /**
1454
- * Determine if rule is implicit (should always validate)
1455
- *
1456
- * @param {string} name
1457
- * @return {boolean}
1458
- */
1459
- isImplicit: function (name) {
1460
- return this.implicitRules.indexOf(name) > -1;
1461
- },
1462
-
1463
- /**
1464
- * Register new rule
1465
- *
1466
- * @param {string} name
1467
- * @param {function} fn
1468
- * @return {void}
1469
- */
1470
- register: function (name, fn) {
1471
- rules[name] = fn;
1472
- },
1473
-
1474
- /**
1475
- * Register new implicit rule
1476
- *
1477
- * @param {string} name
1478
- * @param {function} fn
1479
- * @return {void}
1480
- */
1481
- registerImplicit: function (name, fn) {
1482
- this.register(name, fn);
1483
- this.implicitRules.push(name);
1484
- },
1485
-
1486
- /**
1487
- * Register async rule
1488
- *
1489
- * @param {string} name
1490
- * @param {function} fn
1491
- * @return {void}
1492
- */
1493
- registerAsync: function (name, fn) {
1494
- this.register(name, fn);
1495
- this.asyncRules.push(name);
1496
- },
1497
-
1498
- /**
1499
- * Register implicit async rule
1500
- *
1501
- * @param {string} name
1502
- * @param {function} fn
1503
- * @return {void}
1504
- */
1505
- registerAsyncImplicit: function (name, fn) {
1506
- this.registerImplicit(name, fn);
1507
- this.asyncRules.push(name);
1508
- },
1509
-
1510
- registerMissedRuleValidator: function (fn, message) {
1511
- missedRuleValidator = fn;
1512
- missedRuleMessage = message;
1513
- }
1514
- };
1515
-
1516
- module.exports = manager;
1517
-
1518
- },{}],8:[function(require,module,exports){
1519
- var Rules = require('./rules');
1520
- var Lang = require('./lang');
1521
- var Errors = require('./errors');
1522
- var Attributes = require('./attributes');
1523
- var AsyncResolvers = require('./async');
1524
-
1525
- var Validator = function (input, rules, customMessages) {
1526
- var lang = Validator.getDefaultLang();
1527
- this.input = input || {};
1528
-
1529
- this.messages = Lang._make(lang);
1530
- this.messages._setCustom(customMessages);
1531
- this.setAttributeFormatter(Validator.prototype.attributeFormatter);
1532
-
1533
- this.errors = new Errors();
1534
- this.errorCount = 0;
1535
-
1536
- this.hasAsync = false;
1537
- this.rules = this._parseRules(rules);
1538
- };
1539
-
1540
- Validator.prototype = {
1541
-
1542
- constructor: Validator,
1543
-
1544
- /**
1545
- * Default language
1546
- *
1547
- * @type {string}
1548
- */
1549
- lang: 'en',
1550
-
1551
- /**
1552
- * Numeric based rules
1553
- *
1554
- * @type {array}
1555
- */
1556
- numericRules: ['integer', 'numeric'],
1557
-
1558
- /**
1559
- * Attribute formatter.
1560
- *
1561
- * @type {function}
1562
- */
1563
- attributeFormatter: Attributes.formatter,
1564
-
1565
- /**
1566
- * Run validator
1567
- *
1568
- * @return {boolean} Whether it passes; true = passes, false = fails
1569
- */
1570
- check: function () {
1571
- var self = this;
1572
-
1573
- for (var attribute in this.rules) {
1574
- var attributeRules = this.rules[attribute];
1575
- var inputValue = this._objectPath(this.input, attribute);
1576
-
1577
- if (this._hasRule(attribute, ['sometimes']) && !this._suppliedWithData(attribute)) {
1578
- continue;
1579
- }
1580
-
1581
- for (var i = 0, len = attributeRules.length, rule, ruleOptions, rulePassed; i < len; i++) {
1582
- ruleOptions = attributeRules[i];
1583
- rule = this.getRule(ruleOptions.name);
1584
-
1585
- if (!this._isValidatable(rule, inputValue)) {
1586
- continue;
1587
- }
1588
-
1589
- rulePassed = rule.validate(inputValue, ruleOptions.value, attribute);
1590
- if (!rulePassed) {
1591
- this._addFailure(rule);
1592
- }
1593
-
1594
- if (this._shouldStopValidating(attribute, rulePassed)) {
1595
- break;
1596
- }
1597
- }
1598
- }
1599
-
1600
- return this.errorCount === 0;
1601
- },
1602
-
1603
- /**
1604
- * Run async validator
1605
- *
1606
- * @param {function} passes
1607
- * @param {function} fails
1608
- * @return {void}
1609
- */
1610
- checkAsync: function (passes, fails) {
1611
- var _this = this;
1612
- passes = passes || function () {};
1613
- fails = fails || function () {};
1614
-
1615
- var failsOne = function (rule, message) {
1616
- _this._addFailure(rule, message);
1617
- };
1618
-
1619
- var resolvedAll = function (allPassed) {
1620
- if (allPassed) {
1621
- passes();
1622
- } else {
1623
- fails();
1624
- }
1625
- };
1626
-
1627
- var asyncResolvers = new AsyncResolvers(failsOne, resolvedAll);
1628
-
1629
- var validateRule = function (inputValue, ruleOptions, attribute, rule) {
1630
- return function () {
1631
- var resolverIndex = asyncResolvers.add(rule);
1632
- rule.validate(inputValue, ruleOptions.value, attribute, function () {
1633
- asyncResolvers.resolve(resolverIndex);
1634
- });
1635
- };
1636
- };
1637
-
1638
- for (var attribute in this.rules) {
1639
- var attributeRules = this.rules[attribute];
1640
- var inputValue = this._objectPath(this.input, attribute);
1641
-
1642
- if (this._hasRule(attribute, ['sometimes']) && !this._suppliedWithData(attribute)) {
1643
- continue;
1644
- }
1645
-
1646
- for (var i = 0, len = attributeRules.length, rule, ruleOptions; i < len; i++) {
1647
- ruleOptions = attributeRules[i];
1648
-
1649
- rule = this.getRule(ruleOptions.name);
1650
-
1651
- if (!this._isValidatable(rule, inputValue)) {
1652
- continue;
1653
- }
1654
-
1655
- validateRule(inputValue, ruleOptions, attribute, rule)();
1656
- }
1657
- }
1658
-
1659
- asyncResolvers.enableFiring();
1660
- asyncResolvers.fire();
1661
- },
1662
-
1663
- /**
1664
- * Add failure and error message for given rule
1665
- *
1666
- * @param {Rule} rule
1667
- */
1668
- _addFailure: function (rule) {
1669
- var msg = this.messages.render(rule);
1670
- this.errors.add(rule.attribute, msg);
1671
- this.errorCount++;
1672
- },
1673
-
1674
- /**
1675
- * Flatten nested object, normalizing { foo: { bar: 1 } } into: { 'foo.bar': 1 }
1676
- *
1677
- * @param {object} nested object
1678
- * @return {object} flattened object
1679
- */
1680
- _flattenObject: function (obj) {
1681
- var flattened = {};
1682
-
1683
- function recurse(current, property) {
1684
- if (!property && Object.getOwnPropertyNames(current).length === 0) {
1685
- return;
1686
- }
1687
- if (Object(current) !== current || Array.isArray(current)) {
1688
- flattened[property] = current;
1689
- } else {
1690
- var isEmpty = true;
1691
- for (var p in current) {
1692
- isEmpty = false;
1693
- recurse(current[p], property ? property + '.' + p : p);
1694
- }
1695
- if (isEmpty) {
1696
- flattened[property] = {};
1697
- }
1698
- }
1699
- }
1700
- if (obj) {
1701
- recurse(obj);
1702
- }
1703
- return flattened;
1704
- },
1705
-
1706
- /**
1707
- * Extract value from nested object using string path with dot notation
1708
- *
1709
- * @param {object} object to search in
1710
- * @param {string} path inside object
1711
- * @return {any|void} value under the path
1712
- */
1713
- _objectPath: function (obj, path) {
1714
- if (Object.prototype.hasOwnProperty.call(obj, path)) {
1715
- return obj[path];
1716
- }
1717
-
1718
- var keys = path.replace(/\[(\w+)\]/g, '.$1').replace(/^\./, '').split('.');
1719
- var copy = {};
1720
- for (var attr in obj) {
1721
- if (Object.prototype.hasOwnProperty.call(obj, attr)) {
1722
- copy[attr] = obj[attr];
1723
- }
1724
- }
1725
-
1726
- for (var i = 0, l = keys.length; i < l; i++) {
1727
- if (typeof copy === 'object' && copy !== null && Object.hasOwnProperty.call(copy, keys[i])) {
1728
- copy = copy[keys[i]];
1729
- } else {
1730
- return;
1731
- }
1732
- }
1733
- return copy;
1734
- },
1735
-
1736
- /**
1737
- * Parse rules, normalizing format into: { attribute: [{ name: 'age', value: 3 }] }
1738
- *
1739
- * @param {object} rules
1740
- * @return {object}
1741
- */
1742
- _parseRules: function (rules) {
1743
-
1744
- var parsedRules = {};
1745
- rules = this._flattenObject(rules);
1746
-
1747
- for (var attribute in rules) {
1748
-
1749
- var rulesArray = rules[attribute];
1750
-
1751
- this._parseRulesCheck(attribute, rulesArray, parsedRules);
1752
- }
1753
- return parsedRules;
1754
-
1755
-
1756
- },
1757
-
1758
- _parseRulesCheck: function (attribute, rulesArray, parsedRules, wildCardValues) {
1759
- if (attribute.indexOf('*') > -1) {
1760
- this._parsedRulesRecurse(attribute, rulesArray, parsedRules, wildCardValues);
1761
- } else {
1762
- this._parseRulesDefault(attribute, rulesArray, parsedRules, wildCardValues);
1763
- }
1764
- },
1765
-
1766
- _parsedRulesRecurse: function (attribute, rulesArray, parsedRules, wildCardValues) {
1767
- var parentPath = attribute.substr(0, attribute.indexOf('*') - 1);
1768
- var propertyValue = this._objectPath(this.input, parentPath);
1769
-
1770
- if (propertyValue) {
1771
- for (var propertyNumber = 0; propertyNumber < propertyValue.length; propertyNumber++) {
1772
- var workingValues = wildCardValues ? wildCardValues.slice() : [];
1773
- workingValues.push(propertyNumber);
1774
- this._parseRulesCheck(attribute.replace('*', propertyNumber), rulesArray, parsedRules, workingValues);
1775
- }
1776
- }
1777
- },
1778
-
1779
- _parseRulesDefault: function (attribute, rulesArray, parsedRules, wildCardValues) {
1780
- var attributeRules = [];
1781
-
1782
- if (rulesArray instanceof Array) {
1783
- rulesArray = this._prepareRulesArray(rulesArray);
1784
- }
1785
-
1786
- if (typeof rulesArray === 'string') {
1787
- rulesArray = rulesArray.split('|');
1788
- }
1789
-
1790
- for (var i = 0, len = rulesArray.length, rule; i < len; i++) {
1791
- rule = typeof rulesArray[i] === 'string' ? this._extractRuleAndRuleValue(rulesArray[i]) : rulesArray[i];
1792
- if (rule.value) {
1793
- rule.value = this._replaceWildCards(rule.value, wildCardValues);
1794
- this._replaceWildCardsMessages(wildCardValues);
1795
- }
1796
-
1797
- if (Rules.isAsync(rule.name)) {
1798
- this.hasAsync = true;
1799
- }
1800
- attributeRules.push(rule);
1801
- }
1802
-
1803
- parsedRules[attribute] = attributeRules;
1804
- },
1805
-
1806
- _replaceWildCards: function (path, nums) {
1807
-
1808
- if (!nums) {
1809
- return path;
1810
- }
1811
-
1812
- var path2 = path;
1813
- nums.forEach(function (value) {
1814
- if(Array.isArray(path2)){
1815
- path2 = path2[0];
1816
- }
1817
- const pos = path2.indexOf('*');
1818
- if (pos === -1) {
1819
- return path2;
1820
- }
1821
- path2 = path2.substr(0, pos) + value + path2.substr(pos + 1);
1822
- });
1823
- if(Array.isArray(path)){
1824
- path[0] = path2;
1825
- path2 = path;
1826
- }
1827
- return path2;
1828
- },
1829
-
1830
- _replaceWildCardsMessages: function (nums) {
1831
- var customMessages = this.messages.customMessages;
1832
- var self = this;
1833
- Object.keys(customMessages).forEach(function (key) {
1834
- if (nums) {
1835
- var newKey = self._replaceWildCards(key, nums);
1836
- customMessages[newKey] = customMessages[key];
1837
- }
1838
- });
1839
-
1840
- this.messages._setCustom(customMessages);
1841
- },
1842
- /**
1843
- * Prepare rules if it comes in Array. Check for objects. Need for type validation.
1844
- *
1845
- * @param {array} rulesArray
1846
- * @return {array}
1847
- */
1848
- _prepareRulesArray: function (rulesArray) {
1849
- var rules = [];
1850
-
1851
- for (var i = 0, len = rulesArray.length; i < len; i++) {
1852
- if (typeof rulesArray[i] === 'object') {
1853
- for (var rule in rulesArray[i]) {
1854
- rules.push({
1855
- name: rule,
1856
- value: rulesArray[i][rule]
1857
- });
1858
- }
1859
- } else {
1860
- rules.push(rulesArray[i]);
1861
- }
1862
- }
1863
-
1864
- return rules;
1865
- },
1866
-
1867
- /**
1868
- * Determines if the attribute is supplied with the original data object.
1869
- *
1870
- * @param {array} attribute
1871
- * @return {boolean}
1872
- */
1873
- _suppliedWithData: function (attribute) {
1874
- return this.input.hasOwnProperty(attribute);
1875
- },
1876
-
1877
- /**
1878
- * Extract a rule and a value from a ruleString (i.e. min:3), rule = min, value = 3
1879
- *
1880
- * @param {string} ruleString min:3
1881
- * @return {object} object containing the name of the rule and value
1882
- */
1883
- _extractRuleAndRuleValue: function (ruleString) {
1884
- var rule = {},
1885
- ruleArray;
1886
-
1887
- rule.name = ruleString;
1888
-
1889
- if (ruleString.indexOf(':') >= 0) {
1890
- ruleArray = ruleString.split(':');
1891
- rule.name = ruleArray[0];
1892
- rule.value = ruleArray.slice(1).join(':');
1893
- }
1894
-
1895
- return rule;
1896
- },
1897
-
1898
- /**
1899
- * Determine if attribute has any of the given rules
1900
- *
1901
- * @param {string} attribute
1902
- * @param {array} findRules
1903
- * @return {boolean}
1904
- */
1905
- _hasRule: function (attribute, findRules) {
1906
- var rules = this.rules[attribute] || [];
1907
- for (var i = 0, len = rules.length; i < len; i++) {
1908
- if (findRules.indexOf(rules[i].name) > -1) {
1909
- return true;
1910
- }
1911
- }
1912
- return false;
1913
- },
1914
-
1915
- /**
1916
- * Determine if attribute has any numeric-based rules.
1917
- *
1918
- * @param {string} attribute
1919
- * @return {Boolean}
1920
- */
1921
- _hasNumericRule: function (attribute) {
1922
- return this._hasRule(attribute, this.numericRules);
1923
- },
1924
-
1925
- /**
1926
- * Determine if rule is validatable
1927
- *
1928
- * @param {Rule} rule
1929
- * @param {mixed} value
1930
- * @return {boolean}
1931
- */
1932
- _isValidatable: function (rule, value) {
1933
- if (Array.isArray(value)) {
1934
- return true;
1935
- }
1936
- if (Rules.isImplicit(rule.name)) {
1937
- return true;
1938
- }
1939
-
1940
- return this.getRule('required').validate(value);
1941
- },
1942
-
1943
- /**
1944
- * Determine if we should stop validating.
1945
- *
1946
- * @param {string} attribute
1947
- * @param {boolean} rulePassed
1948
- * @return {boolean}
1949
- */
1950
- _shouldStopValidating: function (attribute, rulePassed) {
1951
-
1952
- var stopOnAttributes = this.stopOnAttributes;
1953
- if (typeof stopOnAttributes === 'undefined' || stopOnAttributes === false || rulePassed === true) {
1954
- return false;
1955
- }
1956
-
1957
- if (stopOnAttributes instanceof Array) {
1958
- return stopOnAttributes.indexOf(attribute) > -1;
1959
- }
1960
-
1961
- return true;
1962
- },
1963
-
1964
- /**
1965
- * Set custom attribute names.
1966
- *
1967
- * @param {object} attributes
1968
- * @return {void}
1969
- */
1970
- setAttributeNames: function (attributes) {
1971
- this.messages._setAttributeNames(attributes);
1972
- },
1973
-
1974
- /**
1975
- * Set the attribute formatter.
1976
- *
1977
- * @param {fuction} func
1978
- * @return {void}
1979
- */
1980
- setAttributeFormatter: function (func) {
1981
- this.messages._setAttributeFormatter(func);
1982
- },
1983
-
1984
- /**
1985
- * Get validation rule
1986
- *
1987
- * @param {string} name
1988
- * @return {Rule}
1989
- */
1990
- getRule: function (name) {
1991
- return Rules.make(name, this);
1992
- },
1993
-
1994
- /**
1995
- * Stop on first error.
1996
- *
1997
- * @param {boolean|array} An array of attributes or boolean true/false for all attributes.
1998
- * @return {void}
1999
- */
2000
- stopOnError: function (attributes) {
2001
- this.stopOnAttributes = attributes;
2002
- },
2003
-
2004
- /**
2005
- * Determine if validation passes
2006
- *
2007
- * @param {function} passes
2008
- * @return {boolean|undefined}
2009
- */
2010
- passes: function (passes) {
2011
- var async = this._checkAsync('passes', passes);
2012
- if (async) {
2013
- return this.checkAsync(passes);
2014
- }
2015
- return this.check();
2016
- },
2017
-
2018
- /**
2019
- * Determine if validation fails
2020
- *
2021
- * @param {function} fails
2022
- * @return {boolean|undefined}
2023
- */
2024
- fails: function (fails) {
2025
- var async = this._checkAsync('fails', fails);
2026
- if (async) {
2027
- return this.checkAsync(function () {}, fails);
2028
- }
2029
- return !this.check();
2030
- },
2031
-
2032
- /**
2033
- * Check if validation should be called asynchronously
2034
- *
2035
- * @param {string} funcName Name of the caller
2036
- * @param {function} callback
2037
- * @return {boolean}
2038
- */
2039
- _checkAsync: function (funcName, callback) {
2040
- var hasCallback = typeof callback === 'function';
2041
- if (this.hasAsync && !hasCallback) {
2042
- throw funcName + ' expects a callback when async rules are being tested.';
2043
- }
2044
-
2045
- return this.hasAsync || hasCallback;
2046
- }
2047
-
2048
- };
2049
-
2050
- /**
2051
- * Set messages for language
2052
- *
2053
- * @param {string} lang
2054
- * @param {object} messages
2055
- * @return {this}
2056
- */
2057
- Validator.setMessages = function (lang, messages) {
2058
- Lang._set(lang, messages);
2059
- return this;
2060
- };
2061
-
2062
- /**
2063
- * Get messages for given language
2064
- *
2065
- * @param {string} lang
2066
- * @return {Messages}
2067
- */
2068
- Validator.getMessages = function (lang) {
2069
- return Lang._get(lang);
2070
- };
2071
-
2072
- /**
2073
- * Set default language to use
2074
- *
2075
- * @param {string} lang
2076
- * @return {void}
2077
- */
2078
- Validator.useLang = function (lang) {
2079
- this.prototype.lang = lang;
2080
- };
2081
-
2082
- /**
2083
- * Get default language
2084
- *
2085
- * @return {string}
2086
- */
2087
- Validator.getDefaultLang = function () {
2088
- return this.prototype.lang;
2089
- };
2090
-
2091
- /**
2092
- * Set the attribute formatter.
2093
- *
2094
- * @param {fuction} func
2095
- * @return {void}
2096
- */
2097
- Validator.setAttributeFormatter = function (func) {
2098
- this.prototype.attributeFormatter = func;
2099
- };
2100
-
2101
- /**
2102
- * Stop on first error.
2103
- *
2104
- * @param {boolean|array} An array of attributes or boolean true/false for all attributes.
2105
- * @return {void}
2106
- */
2107
- Validator.stopOnError = function (attributes) {
2108
- this.prototype.stopOnAttributes = attributes;
2109
- };
2110
-
2111
- /**
2112
- * Register custom validation rule
2113
- *
2114
- * @param {string} name
2115
- * @param {function} fn
2116
- * @param {string} message
2117
- * @return {void}
2118
- */
2119
- Validator.register = function (name, fn, message, fnReplacement) {
2120
- var lang = Validator.getDefaultLang();
2121
- Rules.register(name, fn);
2122
- Lang._setRuleMessage(lang, name, message);
2123
- };
2124
-
2125
- /**
2126
- * Register custom validation rule
2127
- *
2128
- * @param {string} name
2129
- * @param {function} fn
2130
- * @param {string} message
2131
- * @param {function} fnReplacement
2132
- * @return {void}
2133
- */
2134
- Validator.registerImplicit = function (name, fn, message, fnReplacement) {
2135
- var lang = Validator.getDefaultLang();
2136
- Rules.registerImplicit(name, fn);
2137
- Lang._setRuleMessage(lang, name, message);
2138
- };
2139
-
2140
- /**
2141
- * Register asynchronous validation rule
2142
- *
2143
- * @param {string} name
2144
- * @param {function} fn
2145
- * @param {string} message
2146
- * @return {void}
2147
- */
2148
- Validator.registerAsync = function (name, fn, message, fnReplacement) {
2149
- var lang = Validator.getDefaultLang();
2150
- Rules.registerAsync(name, fn);
2151
- Lang._setRuleMessage(lang, name, message);
2152
- };
2153
-
2154
- /**
2155
- * Register asynchronous validation rule
2156
- *
2157
- * @param {string} name
2158
- * @param {function} fn
2159
- * @param {string} message
2160
- * @return {void}
2161
- */
2162
- Validator.registerAsyncImplicit = function (name, fn, message) {
2163
- var lang = Validator.getDefaultLang();
2164
- Rules.registerAsyncImplicit(name, fn);
2165
- Lang._setRuleMessage(lang, name, message);
2166
- };
2167
-
2168
- /**
2169
- * Register validator for missed validation rule
2170
- *
2171
- * @param {string} name
2172
- * @param {function} fn
2173
- * @param {string} message
2174
- * @return {void}
2175
- */
2176
- Validator.registerMissedRuleValidator = function(fn, message) {
2177
- Rules.registerMissedRuleValidator(fn, message);
2178
- };
2179
-
2180
- module.exports = Validator;
2181
-
2182
- },{"./async":1,"./attributes":2,"./errors":3,"./lang":4,"./rules":7}]},{},[8])(8)
2183
- });
1
+ /*! @impelsys/validatorjs - 2025-12-30 */
2
+ (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({},{},[])