quival 0.2.7 → 0.3.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/.prettierrc.json +1 -0
- package/README.md +3 -9
- package/dist/locales/en.js +2 -1
- package/dist/locales/en.min.js +2 -2
- package/dist/locales/ms.js +2 -1
- package/dist/locales/ms.min.js +2 -2
- package/dist/quival.js +233 -200
- package/dist/quival.min.js +2 -2
- package/package.json +5 -6
- package/rollup.config.js +11 -13
- package/src/Checkers.js +16 -12
- package/src/ErrorBag.js +14 -2
- package/src/Validator.js +61 -55
- package/src/helpers.js +3 -3
- package/src/locales/en.js +1 -0
- package/src/locales/ms.js +1 -0
- package/test/functionalities.js +85 -0
- package/test/helpers.js +174 -0
- package/test/{test.js → validation.js} +79 -213
package/dist/quival.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* quival v0.
|
|
2
|
+
* quival v0.3.0 (https://github.com/apih/quival)
|
|
3
3
|
* (c) 2023 Mohd Hafizuddin M Marzuki <hafizuddin_83@yahoo.com>
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -7,10 +7,14 @@ var quival = (function (exports) {
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
9
|
function toCamelCase(string) {
|
|
10
|
-
return string
|
|
10
|
+
return string
|
|
11
|
+
.replace(/[-_]/g, ' ')
|
|
12
|
+
.replace(/\s+/, ' ')
|
|
13
|
+
.trim()
|
|
14
|
+
.replace(/(\s\w)/g, (match) => match[1].toUpperCase());
|
|
11
15
|
}
|
|
12
16
|
function toSnakeCase(string) {
|
|
13
|
-
return string.replace(/(.)(?=[A-Z])/g, match => match + '_').toLowerCase();
|
|
17
|
+
return string.replace(/(.)(?=[A-Z])/g, (match) => match + '_').toLowerCase();
|
|
14
18
|
}
|
|
15
19
|
function escapeRegExp(string) {
|
|
16
20
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
@@ -19,15 +23,14 @@ var quival = (function (exports) {
|
|
|
19
23
|
const keys = path.split('.');
|
|
20
24
|
let current = obj;
|
|
21
25
|
for (const key of keys) {
|
|
22
|
-
if (!
|
|
26
|
+
if (!Object.hasOwn(current, key)) {
|
|
23
27
|
return defaultValue;
|
|
24
28
|
}
|
|
25
29
|
current = current[key];
|
|
26
30
|
}
|
|
27
31
|
return current;
|
|
28
32
|
}
|
|
29
|
-
function flattenObject(obj) {
|
|
30
|
-
let prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
33
|
+
function flattenObject(obj, prefix = '') {
|
|
31
34
|
return Object.keys(obj).reduce((accumulator, key) => {
|
|
32
35
|
const prefixedKey = prefix ? `${prefix}.${key}` : key;
|
|
33
36
|
if (typeof obj[key] === 'object' && obj[key] !== null) {
|
|
@@ -71,21 +74,24 @@ var quival = (function (exports) {
|
|
|
71
74
|
return value;
|
|
72
75
|
}
|
|
73
76
|
let match, years, months, days, hours, minutes, seconds, meridiem;
|
|
74
|
-
const castToIntegers = value => value && /^\d*$/.test(value) ? parseInt(value) : value;
|
|
77
|
+
const castToIntegers = (value) => (value && /^\d*$/.test(value) ? parseInt(value) : value);
|
|
75
78
|
if ((match = value.match(/^(\d{1,2})[.\/-](\d{1,2})[.\/-](\d{2,4})\s?((\d{1,2}):(\d{1,2})(:(\d{1,2}))?\s?(am|pm)?)?/i)) !== null) {
|
|
76
|
-
[, days, months, years
|
|
77
|
-
} else if (
|
|
78
|
-
[,
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
79
|
+
[, days, months, years, , hours = 0, minutes = 0, , seconds = 0, meridiem = 'am'] = match.map(castToIntegers);
|
|
80
|
+
} else if (
|
|
81
|
+
(match = value.match(/^(\d{2,4})[.\/-](\d{1,2})[.\/-](\d{1,2})\s?((\d{1,2}):(\d{1,2})(:(\d{1,2}))?\s?(am|pm)?)?/i)) !== null ||
|
|
82
|
+
(match = value.match(/^(\d{4})(\d{2})(\d{2})\s?((\d{2})(\d{2})((\d{2}))?\s?(am|pm)?)?/i)) !== null
|
|
83
|
+
) {
|
|
84
|
+
[, years, months, days, , hours = 0, minutes = 0, , seconds = 0, meridiem = 'am'] = match.map(castToIntegers);
|
|
85
|
+
} else if ((match = value.match(/(\d{1,2}):(\d{1,2})(:(\d{1,2}))?\s?(am|pm)?\s?(\d{4})[.\/-](\d{2})[.\/-](\d{2})/i))) {
|
|
86
|
+
[, hours, minutes, , seconds, meridiem = 'am', years, months, days] = match.map(castToIntegers);
|
|
87
|
+
} else if ((match = value.match(/(\d{1,2}):(\d{1,2})(:(\d{1,2}))?\s?(am|pm)?\s?(\d{2})[.\/-](\d{2})[.\/-](\d{4})/i))) {
|
|
88
|
+
[, hours, minutes, , seconds, meridiem = 'am', days, months, years] = match.map(castToIntegers);
|
|
89
|
+
} else if ((match = value.match(/(\d{1,2}):(\d{1,2})(:(\d{1,2}))?\s?(am|pm)?/i))) {
|
|
84
90
|
const current = new Date();
|
|
85
91
|
years = current.getFullYear();
|
|
86
92
|
months = current.getMonth() + 1;
|
|
87
93
|
days = current.getDate();
|
|
88
|
-
[, hours = 0, minutes = 0
|
|
94
|
+
[, hours = 0, minutes = 0, , seconds = 0, meridiem = 'am'] = match.map(castToIntegers);
|
|
89
95
|
} else {
|
|
90
96
|
return new Date(value);
|
|
91
97
|
}
|
|
@@ -116,7 +122,7 @@ var quival = (function (exports) {
|
|
|
116
122
|
i: '(\\d{2})',
|
|
117
123
|
s: '(\\d{2})',
|
|
118
124
|
A: '(AM|PM)',
|
|
119
|
-
a: '(am|pm)'
|
|
125
|
+
a: '(am|pm)',
|
|
120
126
|
};
|
|
121
127
|
let pattern = '^';
|
|
122
128
|
let indices = {
|
|
@@ -126,11 +132,11 @@ var quival = (function (exports) {
|
|
|
126
132
|
hours: -1,
|
|
127
133
|
minutes: -1,
|
|
128
134
|
seconds: -1,
|
|
129
|
-
meridiem: -1
|
|
135
|
+
meridiem: -1,
|
|
130
136
|
};
|
|
131
137
|
let index = 1;
|
|
132
138
|
for (const char of format) {
|
|
133
|
-
if (
|
|
139
|
+
if (Object.hasOwn(formats, char)) {
|
|
134
140
|
pattern += formats[char];
|
|
135
141
|
if (['Y', 'y'].indexOf(char) !== -1) {
|
|
136
142
|
indices.years = index++;
|
|
@@ -156,7 +162,7 @@ var quival = (function (exports) {
|
|
|
156
162
|
if (match === null) {
|
|
157
163
|
return new Date('');
|
|
158
164
|
}
|
|
159
|
-
match = match.map(value => value && /^\d*$/.test(value) ? parseInt(value) : value);
|
|
165
|
+
match = match.map((value) => (value && /^\d*$/.test(value) ? parseInt(value) : value));
|
|
160
166
|
const current = new Date();
|
|
161
167
|
let years = match[indices.years];
|
|
162
168
|
let months = match[indices.months];
|
|
@@ -205,13 +211,13 @@ var quival = (function (exports) {
|
|
|
205
211
|
}
|
|
206
212
|
|
|
207
213
|
class Checkers {
|
|
208
|
-
|
|
209
|
-
#
|
|
210
|
-
#imageCache = {};
|
|
214
|
+
#distinctCache;
|
|
215
|
+
#imageCache;
|
|
211
216
|
constructor(validator) {
|
|
217
|
+
this.#distinctCache = {};
|
|
218
|
+
this.#imageCache = {};
|
|
212
219
|
this.validator = validator;
|
|
213
220
|
}
|
|
214
|
-
|
|
215
221
|
// Internal helpers
|
|
216
222
|
clearCaches() {
|
|
217
223
|
this.#distinctCache = {};
|
|
@@ -219,7 +225,7 @@ var quival = (function (exports) {
|
|
|
219
225
|
}
|
|
220
226
|
isDependent(parameters) {
|
|
221
227
|
const other = this.validator.getValue(parameters[0]);
|
|
222
|
-
return parameters.slice(1).some(value => value == other);
|
|
228
|
+
return parameters.slice(1).some((value) => value == other);
|
|
223
229
|
}
|
|
224
230
|
collectRequiredsThenTest(attribute, value, parameters, callback) {
|
|
225
231
|
let result = [];
|
|
@@ -241,8 +247,7 @@ var quival = (function (exports) {
|
|
|
241
247
|
}
|
|
242
248
|
return true;
|
|
243
249
|
}
|
|
244
|
-
testStringUsingRegex(attribute, value, asciiRegex, unicodeRegex) {
|
|
245
|
-
let isAscii = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
|
|
250
|
+
testStringUsingRegex(attribute, value, asciiRegex, unicodeRegex, isAscii = false) {
|
|
246
251
|
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
247
252
|
return false;
|
|
248
253
|
}
|
|
@@ -274,7 +279,7 @@ var quival = (function (exports) {
|
|
|
274
279
|
}
|
|
275
280
|
compareDates(attribute, value, parameters, callback) {
|
|
276
281
|
const rule = this.validator.getRule(attribute);
|
|
277
|
-
value =
|
|
282
|
+
value = Object.hasOwn(rule, 'date_format') ? parseDateByFormat(value, rule.date_format[0]) : parseDate(value);
|
|
278
283
|
if (!isValidDate(value)) {
|
|
279
284
|
return false;
|
|
280
285
|
}
|
|
@@ -284,14 +289,13 @@ var quival = (function (exports) {
|
|
|
284
289
|
otherValue = parseDate(other);
|
|
285
290
|
} else {
|
|
286
291
|
const otherRule = this.validator.getRule(other);
|
|
287
|
-
otherValue =
|
|
292
|
+
otherValue = Object.hasOwn(otherRule, 'date_format') ? parseDateByFormat(otherValue, otherRule.date_format[0]) : parseDate(otherValue);
|
|
288
293
|
}
|
|
289
294
|
if (!isValidDate(otherValue)) {
|
|
290
295
|
return false;
|
|
291
296
|
}
|
|
292
297
|
return callback(value.getTime(), otherValue.getTime());
|
|
293
298
|
}
|
|
294
|
-
|
|
295
299
|
// Type
|
|
296
300
|
checkArray(attribute, value, parameters) {
|
|
297
301
|
if (!(Array.isArray(value) || isPlainObject(value))) {
|
|
@@ -306,6 +310,9 @@ var quival = (function (exports) {
|
|
|
306
310
|
}
|
|
307
311
|
return true;
|
|
308
312
|
}
|
|
313
|
+
checkList(attribute, value, parameters) {
|
|
314
|
+
return Array.isArray(value);
|
|
315
|
+
}
|
|
309
316
|
checkBoolean(attribute, value, parameters) {
|
|
310
317
|
return [true, false, 0, 1, '0', '1'].includes(value);
|
|
311
318
|
}
|
|
@@ -324,7 +331,6 @@ var quival = (function (exports) {
|
|
|
324
331
|
checkString(attribute, value, parameters) {
|
|
325
332
|
return typeof value === 'string';
|
|
326
333
|
}
|
|
327
|
-
|
|
328
334
|
// Numeric
|
|
329
335
|
checkDecimal(attribute, value, parameters) {
|
|
330
336
|
if (!this.checkNumeric(attribute, value)) {
|
|
@@ -351,7 +357,6 @@ var quival = (function (exports) {
|
|
|
351
357
|
}
|
|
352
358
|
return numerator % denominator === 0;
|
|
353
359
|
}
|
|
354
|
-
|
|
355
360
|
// Agreement
|
|
356
361
|
checkAccepted(attribute, value, parameters) {
|
|
357
362
|
return ['yes', 'on', '1', 1, true, 'true'].includes(value);
|
|
@@ -371,7 +376,6 @@ var quival = (function (exports) {
|
|
|
371
376
|
}
|
|
372
377
|
return true;
|
|
373
378
|
}
|
|
374
|
-
|
|
375
379
|
// Existence
|
|
376
380
|
checkRequired(attribute, value, parameters) {
|
|
377
381
|
if (isEmpty(value)) {
|
|
@@ -415,16 +419,16 @@ var quival = (function (exports) {
|
|
|
415
419
|
return true;
|
|
416
420
|
}
|
|
417
421
|
checkRequiredWith(attribute, value, parameters) {
|
|
418
|
-
return this.collectRequiredsThenTest(attribute, value, parameters, result => result.includes(true));
|
|
422
|
+
return this.collectRequiredsThenTest(attribute, value, parameters, (result) => result.includes(true));
|
|
419
423
|
}
|
|
420
424
|
checkRequiredWithAll(attribute, value, parameters) {
|
|
421
|
-
return this.collectRequiredsThenTest(attribute, value, parameters, result => !result.includes(false));
|
|
425
|
+
return this.collectRequiredsThenTest(attribute, value, parameters, (result) => !result.includes(false));
|
|
422
426
|
}
|
|
423
427
|
checkRequiredWithout(attribute, value, parameters) {
|
|
424
|
-
return this.collectRequiredsThenTest(attribute, value, parameters, result => result.includes(false));
|
|
428
|
+
return this.collectRequiredsThenTest(attribute, value, parameters, (result) => result.includes(false));
|
|
425
429
|
}
|
|
426
430
|
checkRequiredWithoutAll(attribute, value, parameters) {
|
|
427
|
-
return this.collectRequiredsThenTest(attribute, value, parameters, result => !result.includes(true));
|
|
431
|
+
return this.collectRequiredsThenTest(attribute, value, parameters, (result) => !result.includes(true));
|
|
428
432
|
}
|
|
429
433
|
checkFilled(attribute, value, parameters) {
|
|
430
434
|
if (typeof value !== 'undefined') {
|
|
@@ -435,7 +439,6 @@ var quival = (function (exports) {
|
|
|
435
439
|
checkPresent(attribute, value, parameters) {
|
|
436
440
|
return typeof value !== 'undefined';
|
|
437
441
|
}
|
|
438
|
-
|
|
439
442
|
// Missing
|
|
440
443
|
checkMissing(attribute, value, parameters) {
|
|
441
444
|
return !this.validator.hasAttribute(attribute);
|
|
@@ -453,12 +456,11 @@ var quival = (function (exports) {
|
|
|
453
456
|
return true;
|
|
454
457
|
}
|
|
455
458
|
checkMissingWith(attribute, value, parameters) {
|
|
456
|
-
return this.collectMissingsThenTest(attribute, value, parameters, result => result.includes(false));
|
|
459
|
+
return this.collectMissingsThenTest(attribute, value, parameters, (result) => result.includes(false));
|
|
457
460
|
}
|
|
458
461
|
checkMissingWithAll(attribute, value, parameters) {
|
|
459
|
-
return this.collectMissingsThenTest(attribute, value, parameters, result => !result.includes(true));
|
|
462
|
+
return this.collectMissingsThenTest(attribute, value, parameters, (result) => !result.includes(true));
|
|
460
463
|
}
|
|
461
|
-
|
|
462
464
|
// Prohibition
|
|
463
465
|
checkProhibited(attribute, value, parameters) {
|
|
464
466
|
return !this.checkRequired(attribute, value);
|
|
@@ -485,7 +487,6 @@ var quival = (function (exports) {
|
|
|
485
487
|
}
|
|
486
488
|
return true;
|
|
487
489
|
}
|
|
488
|
-
|
|
489
490
|
// Size
|
|
490
491
|
checkSize(attribute, value, parameters) {
|
|
491
492
|
return this.validator.getSize(attribute, value) === parseFloat(parameters[0]);
|
|
@@ -499,10 +500,8 @@ var quival = (function (exports) {
|
|
|
499
500
|
checkBetween(attribute, value, parameters) {
|
|
500
501
|
return this.checkMin(attribute, value, [parameters[0]]) && this.checkMax(attribute, value, [parameters[1]]);
|
|
501
502
|
}
|
|
502
|
-
|
|
503
503
|
// Digits
|
|
504
|
-
checkDigits(attribute, value, parameters) {
|
|
505
|
-
let callback = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : (length, value) => length === value;
|
|
504
|
+
checkDigits(attribute, value, parameters, callback = (length, value) => length === value) {
|
|
506
505
|
value = String(value ?? '');
|
|
507
506
|
if (!isDigits(value)) {
|
|
508
507
|
return false;
|
|
@@ -518,7 +517,6 @@ var quival = (function (exports) {
|
|
|
518
517
|
checkDigitsBetween(attribute, value, parameters) {
|
|
519
518
|
return this.checkDigits(attribute, value, parameters, (length, value1, value2) => length >= value1 && length <= value2);
|
|
520
519
|
}
|
|
521
|
-
|
|
522
520
|
// String
|
|
523
521
|
checkAlpha(attribute, value, parameters) {
|
|
524
522
|
return this.testStringUsingRegex(attribute, value, /^[a-z]+$/i, /^[\p{L}\p{M}]+$/u, parameters.includes('ascii'));
|
|
@@ -532,8 +530,7 @@ var quival = (function (exports) {
|
|
|
532
530
|
checkAscii(attribute, value, parameters) {
|
|
533
531
|
return !/[^\x09\x10\x13\x0A\x0D\x20-\x7E]/.test(value);
|
|
534
532
|
}
|
|
535
|
-
checkRegex(attribute, value, parameters) {
|
|
536
|
-
let invert = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
533
|
+
checkRegex(attribute, value, parameters, invert = false) {
|
|
537
534
|
if (!(typeof value === 'string' || isNumeric(value))) {
|
|
538
535
|
return false;
|
|
539
536
|
}
|
|
@@ -543,7 +540,11 @@ var quival = (function (exports) {
|
|
|
543
540
|
throw new Error(`Invalid regular expression pattern: ${expression}`);
|
|
544
541
|
}
|
|
545
542
|
if (flags.includes('u')) {
|
|
546
|
-
pattern = pattern
|
|
543
|
+
pattern = pattern
|
|
544
|
+
.replace(/\\A/g, '^')
|
|
545
|
+
.replace(/\\z/gi, '$')
|
|
546
|
+
.replace(/\\([pP])([CLMNPSZ])/g, '\\$1{$2}')
|
|
547
|
+
.replace(/\\\x\{([0-9a-f]+)\}/g, '\\u{$1}');
|
|
547
548
|
}
|
|
548
549
|
const result = new RegExp(pattern, flags).test(value);
|
|
549
550
|
return invert ? !result : result;
|
|
@@ -581,7 +582,6 @@ var quival = (function (exports) {
|
|
|
581
582
|
checkDoesntEndWith(attribute, value, parameters) {
|
|
582
583
|
return !this.checkEndsWith(attribute, value, parameters);
|
|
583
584
|
}
|
|
584
|
-
|
|
585
585
|
// Compare values
|
|
586
586
|
checkSame(attribute, value, parameters) {
|
|
587
587
|
const other = this.validator.getValue(parameters[0]);
|
|
@@ -611,7 +611,6 @@ var quival = (function (exports) {
|
|
|
611
611
|
checkLte(attribute, value, parameters) {
|
|
612
612
|
return this.compareValues(attribute, value, parameters, (val1, val2) => val1 <= val2);
|
|
613
613
|
}
|
|
614
|
-
|
|
615
614
|
// Dates
|
|
616
615
|
checkAfter(attribute, value, parameters) {
|
|
617
616
|
return this.compareDates(attribute, value, parameters, (val1, val2) => val1 > val2);
|
|
@@ -644,11 +643,11 @@ var quival = (function (exports) {
|
|
|
644
643
|
i: '(\\d{2})',
|
|
645
644
|
s: '(\\d{2})',
|
|
646
645
|
A: '(AM|PM)',
|
|
647
|
-
a: '(am|pm)'
|
|
646
|
+
a: '(am|pm)',
|
|
648
647
|
};
|
|
649
648
|
let pattern = '^';
|
|
650
649
|
for (const char of format) {
|
|
651
|
-
if (
|
|
650
|
+
if (Object.hasOwn(formats, char)) {
|
|
652
651
|
pattern += formats[char];
|
|
653
652
|
} else {
|
|
654
653
|
pattern += '\\' + char;
|
|
@@ -657,7 +656,6 @@ var quival = (function (exports) {
|
|
|
657
656
|
pattern += '$';
|
|
658
657
|
return new RegExp(pattern).test(value);
|
|
659
658
|
}
|
|
660
|
-
|
|
661
659
|
// Array / Object
|
|
662
660
|
checkDistinct(attribute, value, parameters) {
|
|
663
661
|
const unparsed = this.validator.getPrimaryAttribute(attribute);
|
|
@@ -667,7 +665,7 @@ var quival = (function (exports) {
|
|
|
667
665
|
const index = unparsed.indexOf('*');
|
|
668
666
|
const parentPath = unparsed.substring(0, index - 1);
|
|
669
667
|
let stringified;
|
|
670
|
-
if (this.#distinctCache
|
|
668
|
+
if (Object.hasOwn(this.#distinctCache, parentPath)) {
|
|
671
669
|
stringified = this.#distinctCache[parentPath];
|
|
672
670
|
} else {
|
|
673
671
|
stringified = JSON.stringify(flattenObject(this.validator.getValue(parentPath) ?? {}));
|
|
@@ -697,14 +695,14 @@ var quival = (function (exports) {
|
|
|
697
695
|
return false;
|
|
698
696
|
}
|
|
699
697
|
const data = this.validator.getValue(unparsed.split('.*')[0]) ?? {};
|
|
700
|
-
return Object.values(flattenObject(data)).some(item => item == value);
|
|
698
|
+
return Object.values(flattenObject(data)).some((item) => item == value);
|
|
701
699
|
}
|
|
702
700
|
checkIn(attribute, value, parameters) {
|
|
703
701
|
if (!(this.checkArray(attribute, value) && this.validator.hasRule(attribute, 'array'))) {
|
|
704
|
-
return parameters.some(parameter => parameter == value);
|
|
702
|
+
return parameters.some((parameter) => parameter == value);
|
|
705
703
|
}
|
|
706
704
|
for (const item of Object.values(value)) {
|
|
707
|
-
if (!parameters.some(parameter => parameter == item)) {
|
|
705
|
+
if (!parameters.some((parameter) => parameter == item)) {
|
|
708
706
|
return false;
|
|
709
707
|
}
|
|
710
708
|
}
|
|
@@ -713,7 +711,6 @@ var quival = (function (exports) {
|
|
|
713
711
|
checkNotIn(attribute, value, parameters) {
|
|
714
712
|
return !this.checkIn(attribute, value, parameters);
|
|
715
713
|
}
|
|
716
|
-
|
|
717
714
|
// File
|
|
718
715
|
checkMimetypes(attribute, value, parameters) {
|
|
719
716
|
if (this.checkFile(attribute, value)) {
|
|
@@ -737,28 +734,30 @@ var quival = (function (exports) {
|
|
|
737
734
|
}
|
|
738
735
|
await new Promise((resolve, reject) => {
|
|
739
736
|
const reader = new FileReader();
|
|
740
|
-
reader.onload = event => resolve(event.target.result);
|
|
737
|
+
reader.onload = (event) => resolve(event.target.result);
|
|
741
738
|
reader.onerror = reject;
|
|
742
739
|
reader.readAsDataURL(value);
|
|
743
|
-
})
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
740
|
+
})
|
|
741
|
+
.then(async (data) => {
|
|
742
|
+
const image = new Image();
|
|
743
|
+
image.src = data;
|
|
744
|
+
await image.decode();
|
|
745
|
+
this.#imageCache[attribute] = image;
|
|
746
|
+
})
|
|
747
|
+
.catch(() => {
|
|
748
|
+
result = false;
|
|
749
|
+
});
|
|
751
750
|
return result;
|
|
752
751
|
}
|
|
753
752
|
async checkDimensions(attribute, value, parameters) {
|
|
754
|
-
if (!this.checkImage(attribute, value) || !this.#imageCache
|
|
753
|
+
if (!this.checkImage(attribute, value) || !Object.hasOwn(this.#imageCache, attribute)) {
|
|
755
754
|
return false;
|
|
756
755
|
}
|
|
757
756
|
const constraints = {};
|
|
758
757
|
for (const parameter of parameters) {
|
|
759
758
|
const [key, value] = parameter.split('=', 2);
|
|
760
759
|
if (key === 'ratio' && value.includes('/')) {
|
|
761
|
-
const [numerator, denominator] = value.split('/', 2).map(part => parseFloat(part, 10));
|
|
760
|
+
const [numerator, denominator] = value.split('/', 2).map((part) => parseFloat(part, 10));
|
|
762
761
|
constraints[key] = numerator / denominator;
|
|
763
762
|
} else {
|
|
764
763
|
constraints[key] = parseFloat(value, 10);
|
|
@@ -767,20 +766,28 @@ var quival = (function (exports) {
|
|
|
767
766
|
const image = this.#imageCache[attribute];
|
|
768
767
|
const width = image.naturalWidth;
|
|
769
768
|
const height = image.naturalHeight;
|
|
770
|
-
if (
|
|
769
|
+
if (
|
|
770
|
+
(Object.hasOwn(constraints, 'width') && constraints.width !== width) ||
|
|
771
|
+
(Object.hasOwn(constraints, 'height') && constraints.height !== height) ||
|
|
772
|
+
(Object.hasOwn(constraints, 'min_width') && constraints.min_width > width) ||
|
|
773
|
+
(Object.hasOwn(constraints, 'min_height') && constraints.min_height > height) ||
|
|
774
|
+
(Object.hasOwn(constraints, 'max_width') && constraints.max_width < width) ||
|
|
775
|
+
(Object.hasOwn(constraints, 'max_height') && constraints.max_height < height)
|
|
776
|
+
) {
|
|
771
777
|
return false;
|
|
772
778
|
}
|
|
773
|
-
if (
|
|
779
|
+
if (Object.hasOwn(constraints, 'ratio')) {
|
|
774
780
|
return Math.abs(constraints.ratio - width / height) <= 1 / (Math.max(width, height) + 1);
|
|
775
781
|
}
|
|
776
782
|
return true;
|
|
777
783
|
}
|
|
778
|
-
|
|
779
784
|
// Miscellaneous
|
|
780
785
|
checkEmail(attribute, value, parameters) {
|
|
781
|
-
const firstRegex =
|
|
786
|
+
const firstRegex =
|
|
787
|
+
/^(([^<>()[\]\\.,;:\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,}))$/;
|
|
782
788
|
if (!firstRegex.test(value)) {
|
|
783
|
-
const secondRegex =
|
|
789
|
+
const secondRegex =
|
|
790
|
+
/^((?:[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]))?)+)*$/;
|
|
784
791
|
return secondRegex.test(value);
|
|
785
792
|
}
|
|
786
793
|
return true;
|
|
@@ -801,7 +808,7 @@ var quival = (function (exports) {
|
|
|
801
808
|
const separators = {
|
|
802
809
|
'-': 2,
|
|
803
810
|
':': 2,
|
|
804
|
-
'.': 4
|
|
811
|
+
'.': 4,
|
|
805
812
|
};
|
|
806
813
|
let separator, digits;
|
|
807
814
|
for ([separator, digits] of Object.entries(separators)) {
|
|
@@ -857,7 +864,7 @@ var quival = (function (exports) {
|
|
|
857
864
|
checkTimezone(attribute, value, parameters) {
|
|
858
865
|
try {
|
|
859
866
|
Intl.DateTimeFormat(undefined, {
|
|
860
|
-
timeZone: value
|
|
867
|
+
timeZone: value,
|
|
861
868
|
});
|
|
862
869
|
} catch (error) {
|
|
863
870
|
if (String(error).toLowerCase().includes('invalid time zone')) {
|
|
@@ -883,7 +890,7 @@ var quival = (function (exports) {
|
|
|
883
890
|
}
|
|
884
891
|
|
|
885
892
|
class ErrorBag {
|
|
886
|
-
#data
|
|
893
|
+
#data;
|
|
887
894
|
keys() {
|
|
888
895
|
return Object.keys(this.#data);
|
|
889
896
|
}
|
|
@@ -894,7 +901,7 @@ var quival = (function (exports) {
|
|
|
894
901
|
return Object.entries(this.#data);
|
|
895
902
|
}
|
|
896
903
|
add(key, message) {
|
|
897
|
-
if (this.#data
|
|
904
|
+
if (Object.hasOwn(this.#data, key)) {
|
|
898
905
|
this.#data[key].push(message);
|
|
899
906
|
} else {
|
|
900
907
|
this.#data[key] = [message];
|
|
@@ -902,7 +909,7 @@ var quival = (function (exports) {
|
|
|
902
909
|
}
|
|
903
910
|
get(key) {
|
|
904
911
|
if (!key.includes('*')) {
|
|
905
|
-
return this.#data
|
|
912
|
+
return Object.hasOwn(this.#data, key) ? this.#data[key] : {};
|
|
906
913
|
}
|
|
907
914
|
const pattern = new RegExp('^' + key.replaceAll('*', '.*?') + '$');
|
|
908
915
|
const result = {};
|
|
@@ -927,12 +934,12 @@ var quival = (function (exports) {
|
|
|
927
934
|
}
|
|
928
935
|
all() {
|
|
929
936
|
const result = [];
|
|
930
|
-
this.values().forEach(messages => result.push(...messages));
|
|
937
|
+
this.values().forEach((messages) => result.push(...messages));
|
|
931
938
|
return result;
|
|
932
939
|
}
|
|
933
940
|
count() {
|
|
934
941
|
let count = 0;
|
|
935
|
-
this.values().forEach(messages => count += messages.length);
|
|
942
|
+
this.values().forEach((messages) => (count += messages.length));
|
|
936
943
|
return count;
|
|
937
944
|
}
|
|
938
945
|
isEmpty() {
|
|
@@ -941,6 +948,18 @@ var quival = (function (exports) {
|
|
|
941
948
|
isNotEmpty() {
|
|
942
949
|
return !this.isEmpty();
|
|
943
950
|
}
|
|
951
|
+
sortByKeys(keys) {
|
|
952
|
+
const data = {};
|
|
953
|
+
for (const key of keys) {
|
|
954
|
+
if (Object.hasOwn(this.#data, key)) {
|
|
955
|
+
data[key] = this.#data[key];
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
this.#data = data;
|
|
959
|
+
}
|
|
960
|
+
constructor() {
|
|
961
|
+
this.#data = {};
|
|
962
|
+
}
|
|
944
963
|
}
|
|
945
964
|
|
|
946
965
|
class Lang {
|
|
@@ -971,45 +990,38 @@ var quival = (function (exports) {
|
|
|
971
990
|
}
|
|
972
991
|
|
|
973
992
|
class Replacers {
|
|
974
|
-
validator;
|
|
975
993
|
constructor(validator) {
|
|
976
994
|
this.validator = validator;
|
|
977
995
|
}
|
|
978
996
|
replace(message, data) {
|
|
979
|
-
Object.entries(data).forEach(
|
|
980
|
-
let [key, value] = _ref;
|
|
981
|
-
return message = message.replaceAll(':' + key, value);
|
|
982
|
-
});
|
|
997
|
+
Object.entries(data).forEach(([key, value]) => (message = message.replaceAll(':' + key, value)));
|
|
983
998
|
return message;
|
|
984
999
|
}
|
|
985
|
-
|
|
986
1000
|
// Numeric
|
|
987
1001
|
replaceDecimal(message, attribute, rule, parameters) {
|
|
988
1002
|
return this.replace(message, {
|
|
989
|
-
decimal: parameters.join('-')
|
|
1003
|
+
decimal: parameters.join('-'),
|
|
990
1004
|
});
|
|
991
1005
|
}
|
|
992
1006
|
replaceMultipleOf(message, attribute, rule, parameters) {
|
|
993
1007
|
return this.replace(message, {
|
|
994
|
-
value: parameters[0]
|
|
1008
|
+
value: parameters[0],
|
|
995
1009
|
});
|
|
996
1010
|
}
|
|
997
|
-
|
|
998
1011
|
// Agreement
|
|
999
1012
|
replaceAcceptedIf(message, attribute, rule, parameters) {
|
|
1000
1013
|
return this.replace(message, {
|
|
1001
1014
|
other: this.validator.getDisplayableAttribute(parameters[0]),
|
|
1002
|
-
value: this.validator.getDisplayableValue(parameters[0], this.validator.getValue(parameters[0]))
|
|
1015
|
+
value: this.validator.getDisplayableValue(parameters[0], this.validator.getValue(parameters[0])),
|
|
1003
1016
|
});
|
|
1004
1017
|
}
|
|
1005
1018
|
replaceDeclinedIf(message, attribute, rule, parameters) {
|
|
1006
1019
|
return this.replaceAcceptedIf(message, attribute, rule, parameters);
|
|
1007
1020
|
}
|
|
1008
|
-
|
|
1009
1021
|
// Existence
|
|
1010
1022
|
replaceRequiredArrayKeys(message, attribute, rule, parameters) {
|
|
1011
1023
|
return this.replace(message, {
|
|
1012
|
-
values: parameters.map(value => this.validator.getDisplayableValue(attribute, value)).join(', ')
|
|
1024
|
+
values: parameters.map((value) => this.validator.getDisplayableValue(attribute, value)).join(', '),
|
|
1013
1025
|
});
|
|
1014
1026
|
}
|
|
1015
1027
|
replaceRequiredIf(message, attribute, rule, parameters) {
|
|
@@ -1021,12 +1033,15 @@ var quival = (function (exports) {
|
|
|
1021
1033
|
replaceRequiredUnless(message, attribute, rule, parameters) {
|
|
1022
1034
|
return this.replace(message, {
|
|
1023
1035
|
other: this.validator.getDisplayableAttribute(parameters[0]),
|
|
1024
|
-
values: parameters
|
|
1036
|
+
values: parameters
|
|
1037
|
+
.slice(1)
|
|
1038
|
+
.map((value) => this.validator.getDisplayableValue(parameters[0], value))
|
|
1039
|
+
.join(', '),
|
|
1025
1040
|
});
|
|
1026
1041
|
}
|
|
1027
1042
|
replaceRequiredWith(message, attribute, rule, parameters) {
|
|
1028
1043
|
return this.replace(message, {
|
|
1029
|
-
values: parameters.map(value => this.validator.getDisplayableAttribute(value)).join(' / ')
|
|
1044
|
+
values: parameters.map((value) => this.validator.getDisplayableAttribute(value)).join(' / '),
|
|
1030
1045
|
});
|
|
1031
1046
|
}
|
|
1032
1047
|
replaceRequiredWithAll(message, attribute, rule, parameters) {
|
|
@@ -1038,14 +1053,13 @@ var quival = (function (exports) {
|
|
|
1038
1053
|
replaceRequiredWithoutAll(message, attribute, rule, parameters) {
|
|
1039
1054
|
return this.replaceRequiredWith(message, attribute, rule, parameters);
|
|
1040
1055
|
}
|
|
1041
|
-
|
|
1042
1056
|
// Missing
|
|
1043
1057
|
replaceMissingIf(message, attribute, rule, parameters) {
|
|
1044
1058
|
return this.replaceAcceptedIf(message, attribute, rule, parameters);
|
|
1045
1059
|
}
|
|
1046
1060
|
replaceMissingUnless(message, attribute, rule, parameters) {
|
|
1047
1061
|
return this.replace(this.replaceRequiredUnless(message, attribute, rule, parameters), {
|
|
1048
|
-
value: this.validator.getDisplayableValue(parameters[0], parameters[1])
|
|
1062
|
+
value: this.validator.getDisplayableValue(parameters[0], parameters[1]),
|
|
1049
1063
|
});
|
|
1050
1064
|
}
|
|
1051
1065
|
replaceMissingWith(message, attribute, rule, parameters) {
|
|
@@ -1054,7 +1068,6 @@ var quival = (function (exports) {
|
|
|
1054
1068
|
replaceMissingWithAll(message, attribute, rule, parameters) {
|
|
1055
1069
|
return this.replaceRequiredWith(message, attribute, rule, parameters);
|
|
1056
1070
|
}
|
|
1057
|
-
|
|
1058
1071
|
// Prohibition
|
|
1059
1072
|
replaceProhibitedIf(message, attribute, rule, parameters) {
|
|
1060
1073
|
return this.replaceAcceptedIf(message, attribute, rule, parameters);
|
|
@@ -1064,37 +1077,35 @@ var quival = (function (exports) {
|
|
|
1064
1077
|
}
|
|
1065
1078
|
replaceProhibits(message, attribute, rule, parameters) {
|
|
1066
1079
|
return this.replace(message, {
|
|
1067
|
-
other: parameters.map(value => this.validator.getDisplayableAttribute(value)).join(' / ')
|
|
1080
|
+
other: parameters.map((value) => this.validator.getDisplayableAttribute(value)).join(' / '),
|
|
1068
1081
|
});
|
|
1069
1082
|
}
|
|
1070
|
-
|
|
1071
1083
|
// Size
|
|
1072
1084
|
replaceSize(message, attribute, rule, parameters) {
|
|
1073
1085
|
return this.replace(message, {
|
|
1074
|
-
size: parameters[0]
|
|
1086
|
+
size: parameters[0],
|
|
1075
1087
|
});
|
|
1076
1088
|
}
|
|
1077
1089
|
replaceMin(message, attribute, rule, parameters) {
|
|
1078
1090
|
return this.replace(message, {
|
|
1079
|
-
min: parameters[0]
|
|
1091
|
+
min: parameters[0],
|
|
1080
1092
|
});
|
|
1081
1093
|
}
|
|
1082
1094
|
replaceMax(message, attribute, rule, parameters) {
|
|
1083
1095
|
return this.replace(message, {
|
|
1084
|
-
max: parameters[0]
|
|
1096
|
+
max: parameters[0],
|
|
1085
1097
|
});
|
|
1086
1098
|
}
|
|
1087
1099
|
replaceBetween(message, attribute, rule, parameters) {
|
|
1088
1100
|
return this.replace(message, {
|
|
1089
1101
|
min: parameters[0],
|
|
1090
|
-
max: parameters[1]
|
|
1102
|
+
max: parameters[1],
|
|
1091
1103
|
});
|
|
1092
1104
|
}
|
|
1093
|
-
|
|
1094
1105
|
// Digits
|
|
1095
1106
|
replaceDigits(message, attribute, rule, parameters) {
|
|
1096
1107
|
return this.replace(message, {
|
|
1097
|
-
digits: parameters[0]
|
|
1108
|
+
digits: parameters[0],
|
|
1098
1109
|
});
|
|
1099
1110
|
}
|
|
1100
1111
|
replaceMinDigits(message, attribute, rule, parameters) {
|
|
@@ -1106,7 +1117,6 @@ var quival = (function (exports) {
|
|
|
1106
1117
|
replaceDigitsBetween(message, attribute, rule, parameters) {
|
|
1107
1118
|
return this.replaceBetween(message, attribute, rule, parameters);
|
|
1108
1119
|
}
|
|
1109
|
-
|
|
1110
1120
|
// String
|
|
1111
1121
|
replaceStartsWith(message, attribute, rule, parameters) {
|
|
1112
1122
|
return this.replaceRequiredArrayKeys(message, attribute, rule, parameters);
|
|
@@ -1120,7 +1130,6 @@ var quival = (function (exports) {
|
|
|
1120
1130
|
replaceDoesntEndWith(message, attribute, rule, parameters) {
|
|
1121
1131
|
return this.replaceRequiredArrayKeys(message, attribute, rule, parameters);
|
|
1122
1132
|
}
|
|
1123
|
-
|
|
1124
1133
|
// Compare values
|
|
1125
1134
|
replaceSame(message, attribute, rule, parameters) {
|
|
1126
1135
|
return this.replaceAcceptedIf(message, attribute, rule, parameters);
|
|
@@ -1131,7 +1140,7 @@ var quival = (function (exports) {
|
|
|
1131
1140
|
replaceGt(message, attribute, rule, parameters) {
|
|
1132
1141
|
const value = this.validator.getValue(parameters[0]);
|
|
1133
1142
|
return this.replace(message, {
|
|
1134
|
-
value: value ? this.validator.getSize(parameters[0], value) : this.validator.getDisplayableAttribute(parameters[0])
|
|
1143
|
+
value: value ? this.validator.getSize(parameters[0], value) : this.validator.getDisplayableAttribute(parameters[0]),
|
|
1135
1144
|
});
|
|
1136
1145
|
}
|
|
1137
1146
|
replaceGte(message, attribute, rule, parameters) {
|
|
@@ -1143,12 +1152,11 @@ var quival = (function (exports) {
|
|
|
1143
1152
|
replaceLte(message, attribute, rule, parameters) {
|
|
1144
1153
|
return this.replaceGt(message, attribute, rule, parameters);
|
|
1145
1154
|
}
|
|
1146
|
-
|
|
1147
1155
|
// Dates
|
|
1148
1156
|
replaceAfter(message, attribute, rule, parameters) {
|
|
1149
1157
|
const other = parameters[0];
|
|
1150
1158
|
return this.replace(message, {
|
|
1151
|
-
date: this.validator.hasAttribute(other) ? this.validator.getDisplayableAttribute(other) : other
|
|
1159
|
+
date: this.validator.hasAttribute(other) ? this.validator.getDisplayableAttribute(other) : other,
|
|
1152
1160
|
});
|
|
1153
1161
|
}
|
|
1154
1162
|
replaceAfterOrEqual(message, attribute, rule, parameters) {
|
|
@@ -1165,10 +1173,9 @@ var quival = (function (exports) {
|
|
|
1165
1173
|
}
|
|
1166
1174
|
replaceDateFormat(message, attribute, rule, parameters) {
|
|
1167
1175
|
return this.replace(message, {
|
|
1168
|
-
format: parameters[0]
|
|
1176
|
+
format: parameters[0],
|
|
1169
1177
|
});
|
|
1170
1178
|
}
|
|
1171
|
-
|
|
1172
1179
|
// Array
|
|
1173
1180
|
replaceInArray(message, attribute, rule, parameters) {
|
|
1174
1181
|
return this.replaceAcceptedIf(message, attribute, rule, parameters);
|
|
@@ -1179,11 +1186,10 @@ var quival = (function (exports) {
|
|
|
1179
1186
|
replaceNotIn(message, attribute, rule, parameters) {
|
|
1180
1187
|
return this.replaceRequiredArrayKeys(message, attribute, rule, parameters);
|
|
1181
1188
|
}
|
|
1182
|
-
|
|
1183
1189
|
// File
|
|
1184
1190
|
replaceMimetypes(message, attribute, rule, parameters) {
|
|
1185
1191
|
return this.replace(message, {
|
|
1186
|
-
values: parameters.join(', ')
|
|
1192
|
+
values: parameters.join(', '),
|
|
1187
1193
|
});
|
|
1188
1194
|
}
|
|
1189
1195
|
replaceMimes(message, attribute, rule, parameters) {
|
|
@@ -1197,8 +1203,43 @@ var quival = (function (exports) {
|
|
|
1197
1203
|
class Validator {
|
|
1198
1204
|
static #customCheckers = {};
|
|
1199
1205
|
static #customReplacers = {};
|
|
1200
|
-
static #dummyRules = [
|
|
1201
|
-
|
|
1206
|
+
static #dummyRules = [
|
|
1207
|
+
'active_url',
|
|
1208
|
+
'bail',
|
|
1209
|
+
'can',
|
|
1210
|
+
'current_password',
|
|
1211
|
+
'enum',
|
|
1212
|
+
'exclude',
|
|
1213
|
+
'exclude_if',
|
|
1214
|
+
'exclude_unless',
|
|
1215
|
+
'exclude_with',
|
|
1216
|
+
'exclude_without',
|
|
1217
|
+
'exists',
|
|
1218
|
+
'nullable',
|
|
1219
|
+
'sometimes',
|
|
1220
|
+
'unique',
|
|
1221
|
+
];
|
|
1222
|
+
static #implicitRules = [
|
|
1223
|
+
'accepted',
|
|
1224
|
+
'accepted_if',
|
|
1225
|
+
'declined',
|
|
1226
|
+
'declined_if',
|
|
1227
|
+
'filled',
|
|
1228
|
+
'missing',
|
|
1229
|
+
'missing_if',
|
|
1230
|
+
'missing_unless',
|
|
1231
|
+
'missing_with',
|
|
1232
|
+
'missing_with_all',
|
|
1233
|
+
'present',
|
|
1234
|
+
'required',
|
|
1235
|
+
'required_if',
|
|
1236
|
+
'required_if_accepted',
|
|
1237
|
+
'required_unless',
|
|
1238
|
+
'required_with',
|
|
1239
|
+
'required_with_all',
|
|
1240
|
+
'required_without',
|
|
1241
|
+
'required_without_all',
|
|
1242
|
+
];
|
|
1202
1243
|
#data;
|
|
1203
1244
|
#rules;
|
|
1204
1245
|
#customMessages;
|
|
@@ -1207,12 +1248,9 @@ var quival = (function (exports) {
|
|
|
1207
1248
|
#checkers;
|
|
1208
1249
|
#replacers;
|
|
1209
1250
|
#errors;
|
|
1210
|
-
#implicitAttributes
|
|
1211
|
-
#stopOnFirstFailure
|
|
1212
|
-
#alwaysBail
|
|
1213
|
-
fileRules = ['file', 'image', 'mimetypes', 'mimes'];
|
|
1214
|
-
numericRules = ['decimal', 'numeric', 'integer'];
|
|
1215
|
-
sizeRules = ['size', 'between', 'min', 'max', 'gt', 'lt', 'gte', 'lte'];
|
|
1251
|
+
#implicitAttributes;
|
|
1252
|
+
#stopOnFirstFailure;
|
|
1253
|
+
#alwaysBail;
|
|
1216
1254
|
static setLocale(locale) {
|
|
1217
1255
|
Lang.locale(locale);
|
|
1218
1256
|
}
|
|
@@ -1235,12 +1273,13 @@ var quival = (function (exports) {
|
|
|
1235
1273
|
static addDummyRule(rule) {
|
|
1236
1274
|
Validator.#dummyRules.push(rule);
|
|
1237
1275
|
}
|
|
1238
|
-
constructor() {
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1276
|
+
constructor(data = {}, rules = {}, messages = {}, attributes = {}, values = {}) {
|
|
1277
|
+
this.#implicitAttributes = {};
|
|
1278
|
+
this.#stopOnFirstFailure = false;
|
|
1279
|
+
this.#alwaysBail = false;
|
|
1280
|
+
this.fileRules = ['file', 'image', 'mimetypes', 'mimes'];
|
|
1281
|
+
this.numericRules = ['decimal', 'numeric', 'integer'];
|
|
1282
|
+
this.sizeRules = ['size', 'between', 'min', 'max', 'gt', 'lt', 'gte', 'lte'];
|
|
1244
1283
|
this.setProperties(data, rules, messages, attributes, values);
|
|
1245
1284
|
this.#checkers = new Checkers(this);
|
|
1246
1285
|
this.#replacers = new Replacers(this);
|
|
@@ -1252,12 +1291,7 @@ var quival = (function (exports) {
|
|
|
1252
1291
|
}
|
|
1253
1292
|
this.#errors = new ErrorBag();
|
|
1254
1293
|
}
|
|
1255
|
-
setProperties() {
|
|
1256
|
-
let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1257
|
-
let rules = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1258
|
-
let messages = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1259
|
-
let attributes = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
1260
|
-
let values = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
1294
|
+
setProperties(data = {}, rules = {}, messages = {}, attributes = {}, values = {}) {
|
|
1261
1295
|
this.#data = data;
|
|
1262
1296
|
this.#rules = this.parseRules(rules);
|
|
1263
1297
|
this.#customMessages = messages;
|
|
@@ -1289,13 +1323,11 @@ var quival = (function (exports) {
|
|
|
1289
1323
|
this.#implicitAttributes[implicitAttribute] = attribute;
|
|
1290
1324
|
return this;
|
|
1291
1325
|
}
|
|
1292
|
-
stopOnFirstFailure() {
|
|
1293
|
-
let flag = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
1326
|
+
stopOnFirstFailure(flag = true) {
|
|
1294
1327
|
this.#stopOnFirstFailure = flag;
|
|
1295
1328
|
return this;
|
|
1296
1329
|
}
|
|
1297
|
-
alwaysBail() {
|
|
1298
|
-
let flag = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
1330
|
+
alwaysBail(flag = true) {
|
|
1299
1331
|
this.#alwaysBail = flag;
|
|
1300
1332
|
return this;
|
|
1301
1333
|
}
|
|
@@ -1323,12 +1355,11 @@ var quival = (function (exports) {
|
|
|
1323
1355
|
if (!(Array.isArray(data) || isPlainObject(data))) {
|
|
1324
1356
|
return [attribute];
|
|
1325
1357
|
}
|
|
1326
|
-
Object.entries(data).forEach(
|
|
1327
|
-
let [key, value] = _ref;
|
|
1358
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
1328
1359
|
const implicitAttribute = `${parentPath}.${key}.${childPath}`.replace(/\.$/, '');
|
|
1329
1360
|
const implicitAttributes = implicitAttribute.includes('*') ? this.parseWildcardAttribute(implicitAttribute) : [implicitAttribute];
|
|
1330
1361
|
attributes.push(...implicitAttributes);
|
|
1331
|
-
implicitAttributes.forEach(value => this.#implicitAttributes[value] = attribute);
|
|
1362
|
+
implicitAttributes.forEach((value) => (this.#implicitAttributes[value] = attribute));
|
|
1332
1363
|
});
|
|
1333
1364
|
return attributes;
|
|
1334
1365
|
}
|
|
@@ -1353,63 +1384,66 @@ var quival = (function (exports) {
|
|
|
1353
1384
|
async validate() {
|
|
1354
1385
|
this.#checkers.clearCaches();
|
|
1355
1386
|
this.#errors = new ErrorBag();
|
|
1387
|
+
const tasks = [];
|
|
1356
1388
|
for (const [attribute, rules] of Object.entries(this.#rules)) {
|
|
1357
1389
|
let value = this.getValue(attribute);
|
|
1358
|
-
if (
|
|
1390
|
+
if (Object.hasOwn(rules, 'sometimes') && typeof value === 'undefined') {
|
|
1359
1391
|
continue;
|
|
1360
1392
|
}
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
}
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1393
|
+
tasks.push(async () => {
|
|
1394
|
+
const doBail = this.#alwaysBail || Object.hasOwn(rules, 'bail');
|
|
1395
|
+
const isNullable = Object.hasOwn(rules, 'nullable');
|
|
1396
|
+
let noError = true;
|
|
1397
|
+
for (const [rule, parameters] of Object.entries(rules)) {
|
|
1398
|
+
if (rule === '') {
|
|
1399
|
+
continue;
|
|
1400
|
+
}
|
|
1401
|
+
if (
|
|
1402
|
+
!Validator.#implicitRules.includes(rule) &&
|
|
1403
|
+
(typeof value === 'undefined' || (typeof value === 'string' && value.trim() === '') || (isNullable && value === null))
|
|
1404
|
+
) {
|
|
1405
|
+
continue;
|
|
1406
|
+
}
|
|
1407
|
+
let result, success, message;
|
|
1408
|
+
const camelRule = toCamelCase('check_' + rule);
|
|
1409
|
+
if (typeof this.#checkers[camelRule] === 'function') {
|
|
1410
|
+
result = await this.#checkers[camelRule](attribute, value, parameters);
|
|
1411
|
+
} else if (Validator.#dummyRules.includes(rule)) {
|
|
1412
|
+
result = true;
|
|
1413
|
+
} else {
|
|
1414
|
+
throw new Error(`Invalid validation rule: ${rule}`);
|
|
1415
|
+
}
|
|
1416
|
+
if (typeof result === 'boolean') {
|
|
1417
|
+
success = result;
|
|
1418
|
+
} else {
|
|
1419
|
+
({ success, message } = result);
|
|
1420
|
+
}
|
|
1421
|
+
if (!success) {
|
|
1422
|
+
noError = false;
|
|
1423
|
+
message = isEmpty(message) ? this.getMessage(attribute, rule) : message;
|
|
1424
|
+
message = this.makeReplacements(message, attribute, rule, parameters);
|
|
1425
|
+
this.#errors.add(attribute, message);
|
|
1426
|
+
if (doBail || Validator.#implicitRules.includes(rule)) {
|
|
1427
|
+
break;
|
|
1428
|
+
}
|
|
1395
1429
|
}
|
|
1396
1430
|
}
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
break;
|
|
1400
|
-
}
|
|
1431
|
+
return noError;
|
|
1432
|
+
});
|
|
1401
1433
|
}
|
|
1402
|
-
if (this.#
|
|
1403
|
-
|
|
1434
|
+
if (this.#stopOnFirstFailure) {
|
|
1435
|
+
for (const task of tasks) {
|
|
1436
|
+
if (!(await task())) break;
|
|
1437
|
+
}
|
|
1438
|
+
} else {
|
|
1439
|
+
await Promise.allSettled(tasks.map((task) => task()));
|
|
1440
|
+
this.#errors.sortByKeys(Object.keys(this.#rules));
|
|
1404
1441
|
}
|
|
1442
|
+
return this.#errors;
|
|
1405
1443
|
}
|
|
1406
1444
|
async passes() {
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
} catch (error) {
|
|
1410
|
-
if (error instanceof Error) {
|
|
1411
|
-
throw error;
|
|
1412
|
-
}
|
|
1445
|
+
await this.validate();
|
|
1446
|
+
if (this.#errors.isNotEmpty()) {
|
|
1413
1447
|
return false;
|
|
1414
1448
|
}
|
|
1415
1449
|
return true;
|
|
@@ -1422,7 +1456,7 @@ var quival = (function (exports) {
|
|
|
1422
1456
|
attribute = this.getPrimaryAttribute(attribute);
|
|
1423
1457
|
let message;
|
|
1424
1458
|
for (const key of [`${attribute}.${rule}`, rule]) {
|
|
1425
|
-
if (this.#customMessages
|
|
1459
|
+
if (Object.hasOwn(this.#customMessages, key)) {
|
|
1426
1460
|
message = this.#customMessages[key];
|
|
1427
1461
|
break;
|
|
1428
1462
|
}
|
|
@@ -1434,7 +1468,7 @@ var quival = (function (exports) {
|
|
|
1434
1468
|
key += '.array';
|
|
1435
1469
|
} else if (value instanceof File || this.hasRule(attribute, this.fileRules)) {
|
|
1436
1470
|
key += '.file';
|
|
1437
|
-
} else if (
|
|
1471
|
+
} else if (isNumeric(value) || this.hasRule(attribute, this.numericRules)) {
|
|
1438
1472
|
key += '.numeric';
|
|
1439
1473
|
} else {
|
|
1440
1474
|
key += '.string';
|
|
@@ -1451,7 +1485,7 @@ var quival = (function (exports) {
|
|
|
1451
1485
|
attribute: attributeName,
|
|
1452
1486
|
ATTRIBUTE: attributeName.toLocaleUpperCase(),
|
|
1453
1487
|
Attribute: attributeName.charAt(0).toLocaleUpperCase() + attributeName.substring(1),
|
|
1454
|
-
input: this.getDisplayableValue(attribute, value)
|
|
1488
|
+
input: this.getDisplayableValue(attribute, value),
|
|
1455
1489
|
};
|
|
1456
1490
|
for (const [key, value] of Object.entries(data)) {
|
|
1457
1491
|
message = message.replaceAll(':' + key, value);
|
|
@@ -1470,13 +1504,13 @@ var quival = (function (exports) {
|
|
|
1470
1504
|
getDisplayableAttribute(attribute) {
|
|
1471
1505
|
const unparsed = this.getPrimaryAttribute(attribute);
|
|
1472
1506
|
for (const name of [attribute, unparsed]) {
|
|
1473
|
-
if (this.#customAttributes
|
|
1507
|
+
if (Object.hasOwn(this.#customAttributes, name)) {
|
|
1474
1508
|
return this.#customAttributes[name];
|
|
1475
1509
|
} else if (Lang.has(`attributes.${name}`)) {
|
|
1476
1510
|
return Lang.get(`attributes.${name}`);
|
|
1477
1511
|
}
|
|
1478
1512
|
}
|
|
1479
|
-
if (this.#implicitAttributes
|
|
1513
|
+
if (Object.hasOwn(this.#implicitAttributes, attribute)) {
|
|
1480
1514
|
return attribute;
|
|
1481
1515
|
}
|
|
1482
1516
|
return toSnakeCase(attribute).replaceAll('_', ' ');
|
|
@@ -1488,7 +1522,7 @@ var quival = (function (exports) {
|
|
|
1488
1522
|
return 'empty';
|
|
1489
1523
|
} else if (typeof value === 'boolean' || this.hasRule(attribute, 'boolean')) {
|
|
1490
1524
|
return Number(value) ? 'true' : 'false';
|
|
1491
|
-
} else if (this.#customValues
|
|
1525
|
+
} else if (Object.hasOwn(this.#customValues, path)) {
|
|
1492
1526
|
return this.#customValues[path];
|
|
1493
1527
|
} else if (Lang.has(`values.${path}`)) {
|
|
1494
1528
|
return Lang.get(`values.${path}`);
|
|
@@ -1504,7 +1538,7 @@ var quival = (function (exports) {
|
|
|
1504
1538
|
return value.size / 1024;
|
|
1505
1539
|
} else if (isPlainObject(value)) {
|
|
1506
1540
|
return Object.keys(value).length;
|
|
1507
|
-
} else if (
|
|
1541
|
+
} else if (Object.hasOwn(value, 'length')) {
|
|
1508
1542
|
return value.length;
|
|
1509
1543
|
}
|
|
1510
1544
|
return value;
|
|
@@ -1516,7 +1550,7 @@ var quival = (function (exports) {
|
|
|
1516
1550
|
hasRule(attribute, rules) {
|
|
1517
1551
|
attribute = this.getPrimaryAttribute(attribute);
|
|
1518
1552
|
rules = typeof rules === 'string' ? [rules] : rules;
|
|
1519
|
-
if (!this.#rules
|
|
1553
|
+
if (!Object.hasOwn(this.#rules, attribute)) {
|
|
1520
1554
|
return false;
|
|
1521
1555
|
}
|
|
1522
1556
|
for (const rule of rules) {
|
|
@@ -1527,7 +1561,7 @@ var quival = (function (exports) {
|
|
|
1527
1561
|
return false;
|
|
1528
1562
|
}
|
|
1529
1563
|
getPrimaryAttribute(attribute) {
|
|
1530
|
-
return this.#implicitAttributes
|
|
1564
|
+
return Object.hasOwn(this.#implicitAttributes, attribute) ? this.#implicitAttributes[attribute] : attribute;
|
|
1531
1565
|
}
|
|
1532
1566
|
hasAttribute(attribute) {
|
|
1533
1567
|
return typeof this.getValue(attribute) !== 'undefined';
|
|
@@ -1547,5 +1581,4 @@ var quival = (function (exports) {
|
|
|
1547
1581
|
exports.Validator = Validator;
|
|
1548
1582
|
|
|
1549
1583
|
return exports;
|
|
1550
|
-
|
|
1551
1584
|
})({});
|