@regle/rules 1.2.3 → 1.3.0-beta.1

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.
@@ -1,1056 +0,0 @@
1
- import { createRule, InternalRuleType, unwrapRuleParameters } from '@regle/core';
2
- import { unref, computed, toValue } from 'vue';
3
-
4
- // src/helpers/withMessage.ts
5
- function withMessage(rule, newMessage) {
6
- let _type;
7
- let validator;
8
- let _active;
9
- let _params;
10
- if (typeof rule === "function" && !("_validator" in rule)) {
11
- _type = InternalRuleType.Inline;
12
- validator = rule;
13
- } else {
14
- ({ _type, validator, _active, _params } = rule);
15
- }
16
- const newRule = createRule({
17
- type: _type,
18
- validator,
19
- active: _active,
20
- message: newMessage
21
- });
22
- const newParams = [..._params ?? []];
23
- newRule._params = newParams;
24
- newRule._message_patched = true;
25
- if (typeof newRule === "function") {
26
- if (_params != null) {
27
- const executedRule = newRule(...newParams);
28
- executedRule._message_patched = true;
29
- return executedRule;
30
- }
31
- return newRule;
32
- } else {
33
- return newRule;
34
- }
35
- }
36
- function withTooltip(rule, newTooltip) {
37
- let _type;
38
- let validator;
39
- let _active;
40
- let _params;
41
- let _message;
42
- if (typeof rule === "function" && !("_validator" in rule)) {
43
- _type = InternalRuleType.Inline;
44
- validator = rule;
45
- } else {
46
- ({ _type, validator, _active, _params, _message } = rule);
47
- }
48
- const newRule = createRule({
49
- type: _type,
50
- validator,
51
- active: _active,
52
- message: _message,
53
- tooltip: newTooltip
54
- });
55
- const newParams = [..._params ?? []];
56
- newRule._params = newParams;
57
- newRule._tooltip_patched = true;
58
- if (typeof newRule === "function") {
59
- const executedRule = newRule(...newParams);
60
- newRule._tooltip_patched = true;
61
- return executedRule;
62
- } else {
63
- return newRule;
64
- }
65
- }
66
- function withAsync(rule, depsArray) {
67
- let _type;
68
- let validator;
69
- let _params = [];
70
- let _message = "";
71
- if (typeof rule === "function") {
72
- _type = InternalRuleType.Inline;
73
- validator = async (value, ...params) => {
74
- return rule(value, ...params);
75
- };
76
- _params = [depsArray];
77
- } else {
78
- ({ _type, _message } = rule);
79
- _params = _params = rule._params?.concat(depsArray);
80
- validator = async (...args) => rule.validator(args);
81
- }
82
- const newRule = createRule({
83
- type: InternalRuleType.Async,
84
- validator,
85
- message: _message
86
- });
87
- newRule._params = newRule._params?.concat(_params);
88
- return newRule(...depsArray ?? []);
89
- }
90
- function withParams(rule, depsArray) {
91
- let _type;
92
- let validator;
93
- let _params = [];
94
- let _message = "";
95
- if (typeof rule === "function") {
96
- _type = InternalRuleType.Inline;
97
- validator = (value, ...params) => {
98
- return rule(value, ...params);
99
- };
100
- _params = [depsArray];
101
- } else {
102
- ({ _type, validator, _message } = rule);
103
- _params = _params = rule._params?.concat(depsArray);
104
- }
105
- const newRule = createRule({
106
- type: InternalRuleType.Inline,
107
- validator,
108
- message: _message
109
- });
110
- newRule._params = newRule._params?.concat(_params);
111
- return newRule(...depsArray);
112
- }
113
- function applyIf(_condition, rule) {
114
- let _type;
115
- let validator;
116
- let _params = [];
117
- let _message = "";
118
- if (typeof rule === "function") {
119
- _type = InternalRuleType.Inline;
120
- validator = rule;
121
- _params = [_condition];
122
- } else {
123
- ({ _type, validator, _message } = rule);
124
- _params = rule._params?.concat([_condition]);
125
- }
126
- function newValidator(value, ...args) {
127
- const [condition] = unwrapRuleParameters([_condition]);
128
- if (condition) {
129
- return validator(value, ...args);
130
- }
131
- return true;
132
- }
133
- function newActive(metadata) {
134
- const [condition] = unwrapRuleParameters([_condition]);
135
- return condition;
136
- }
137
- const newRule = createRule({
138
- type: _type,
139
- validator: newValidator,
140
- active: newActive,
141
- message: _message
142
- });
143
- const newParams = [..._params ?? []];
144
- newRule._params = newParams;
145
- if (typeof newRule === "function") {
146
- const executedRule = newRule(...newParams);
147
- return executedRule;
148
- } else {
149
- return newRule;
150
- }
151
- }
152
-
153
- // ../shared/utils/isFile.ts
154
- function isFile(value) {
155
- return value?.constructor.name == "File" || value?.constructor.name == "FileList";
156
- }
157
-
158
- // ../shared/utils/isEmpty.ts
159
- function isEmpty(value, considerEmptyArrayInvalid = true) {
160
- if (value === void 0 || value === null) {
161
- return true;
162
- }
163
- if (value instanceof Date) {
164
- return isNaN(value.getTime());
165
- } else if (isFile(value)) {
166
- return value.size <= 0;
167
- } else if (Array.isArray(value)) {
168
- if (considerEmptyArrayInvalid) {
169
- return value.length === 0;
170
- }
171
- return false;
172
- } else if (typeof value === "object" && value != null) {
173
- return Object.keys(value).length === 0;
174
- }
175
- return !String(value).length;
176
- }
177
-
178
- // ../shared/utils/isDate.ts
179
- function isDate(value) {
180
- if (isEmpty(value)) {
181
- return false;
182
- }
183
- try {
184
- let possibleDate = null;
185
- if (value instanceof Date) {
186
- possibleDate = value;
187
- } else if (typeof value === "string") {
188
- const date2 = new Date(value);
189
- if (date2.toString() === "Invalid Date") {
190
- return false;
191
- }
192
- possibleDate = date2;
193
- }
194
- return !!possibleDate;
195
- } catch (e) {
196
- return false;
197
- }
198
- }
199
-
200
- // ../shared/utils/toDate.ts
201
- function toDate(argument) {
202
- const argStr = Object.prototype.toString.call(argument);
203
- if (argument == null) {
204
- return /* @__PURE__ */ new Date(NaN);
205
- } else if (argument instanceof Date || typeof argument === "object" && argStr === "[object Date]") {
206
- return new Date(argument.getTime());
207
- } else if (typeof argument === "number" || argStr === "[object Number]") {
208
- return new Date(argument);
209
- } else if (typeof argument === "string" || argStr === "[object String]") {
210
- return new Date(argument);
211
- } else {
212
- return /* @__PURE__ */ new Date(NaN);
213
- }
214
- }
215
-
216
- // src/helpers/ruleHelpers/isFilled.ts
217
- function isFilled(value, considerEmptyArrayInvalid = true) {
218
- return !isEmpty(typeof value === "string" ? value.trim() : value, considerEmptyArrayInvalid);
219
- }
220
-
221
- // src/helpers/ruleHelpers/isNumber.ts
222
- function isNumber(value) {
223
- if (value == null) return false;
224
- else if (typeof value !== "number") {
225
- return false;
226
- } else if (isNaN(value)) {
227
- return false;
228
- } else {
229
- return true;
230
- }
231
- }
232
-
233
- // src/helpers/ruleHelpers/matchRegex.ts
234
- function matchRegex(_value, ...expr) {
235
- if (isEmpty(_value)) {
236
- return true;
237
- }
238
- const value = typeof _value === "number" ? _value.toString() : _value;
239
- return expr.every((reg) => {
240
- reg.lastIndex = 0;
241
- return reg.test(value);
242
- });
243
- }
244
- function getSize(value) {
245
- const _value = unref(value);
246
- if (Array.isArray(_value)) return _value.length;
247
- if (typeof _value === "object") {
248
- return Object.keys(_value).length;
249
- }
250
- if (typeof _value === "number") {
251
- if (isNaN(_value)) {
252
- return 0;
253
- }
254
- return _value;
255
- }
256
- return String(_value).length;
257
- }
258
-
259
- // src/helpers/ruleHelpers/toNumber.ts
260
- function toNumber(argument) {
261
- if (typeof argument === "number") {
262
- return argument;
263
- } else if (argument != null) {
264
- if (typeof argument === "string") {
265
- const isPadded = argument.trim() !== argument;
266
- if (isPadded) {
267
- return NaN;
268
- }
269
- return +argument;
270
- }
271
- return NaN;
272
- }
273
- return NaN;
274
- }
275
- function and(...rules) {
276
- const isAnyRuleAsync = rules.some((rule) => {
277
- if (typeof rule === "function") {
278
- return rule.constructor.name === "AsyncFunction";
279
- } else {
280
- return rule._async;
281
- }
282
- });
283
- const _params = rules.map((rule) => {
284
- if (typeof rule === "function") {
285
- return null;
286
- } else {
287
- const $params = rule._params;
288
- if (!$params?.length) {
289
- return [];
290
- } else {
291
- return $params;
292
- }
293
- }
294
- }).flat().filter((param) => !!param);
295
- function computeRules(rules2, value, ...params) {
296
- const $rules = [];
297
- let paramIndex = 0;
298
- for (let rule of rules2) {
299
- if (typeof rule === "function") {
300
- $rules.push(rule(value));
301
- paramIndex++;
302
- } else {
303
- const paramsLength = rule._params?.length ?? 0;
304
- $rules.push(rule.validator(value, ...params.slice(paramIndex, paramsLength)));
305
- if (paramsLength) {
306
- paramIndex += paramsLength;
307
- }
308
- }
309
- }
310
- return $rules;
311
- }
312
- function computeMetadata(results) {
313
- const isAnyResultMetaData = results?.some((s) => typeof s !== "boolean");
314
- if (isAnyResultMetaData) {
315
- return {
316
- $valid: results.every((result) => {
317
- if (typeof result === "boolean") {
318
- return !!result;
319
- }
320
- return result.$valid;
321
- }),
322
- ...results.reduce((acc, result) => {
323
- if (typeof result === "boolean") {
324
- return acc;
325
- }
326
- const { $valid, ...rest } = result;
327
- return { ...acc, ...rest };
328
- }, {})
329
- };
330
- } else {
331
- return results.every((result) => !!result);
332
- }
333
- }
334
- let validator;
335
- if (!!rules.length) {
336
- validator = isAnyRuleAsync ? async (value, ...params) => {
337
- const results = await Promise.all(computeRules(rules, value, ...params));
338
- return computeMetadata(results);
339
- } : (value, ...params) => {
340
- const $rules = computeRules(rules, value, ...params);
341
- return computeMetadata($rules);
342
- };
343
- } else {
344
- validator = (value) => {
345
- return false;
346
- };
347
- }
348
- const newRule = createRule({
349
- type: "and",
350
- validator,
351
- message: "The value does not match all of the provided validators"
352
- });
353
- const newParams = [..._params ?? []];
354
- newRule._params = newParams;
355
- if (typeof newRule === "function") {
356
- const executedRule = newRule(...newParams);
357
- return executedRule;
358
- } else {
359
- return newRule;
360
- }
361
- }
362
- function or(...rules) {
363
- const isAnyRuleAsync = rules.some((rule) => {
364
- if (typeof rule === "function") {
365
- return rule.constructor.name === "AsyncFunction";
366
- } else {
367
- return rule._async;
368
- }
369
- });
370
- const _params = rules.map((rule) => {
371
- if (typeof rule === "function") {
372
- return null;
373
- } else {
374
- return rule._params;
375
- }
376
- }).flat().filter((param) => !!param);
377
- function computeRules(rules2, value, ...params) {
378
- const $rules = [];
379
- let paramIndex = 0;
380
- for (let rule of rules2) {
381
- if (typeof rule === "function") {
382
- $rules.push(rule(value));
383
- paramIndex++;
384
- } else {
385
- const paramsLength = rule._params?.length ?? 0;
386
- $rules.push(rule.validator(value, ...params.slice(paramIndex, paramsLength)));
387
- if (paramsLength) {
388
- paramIndex += paramsLength;
389
- }
390
- }
391
- }
392
- return $rules;
393
- }
394
- function computeMetadata(results) {
395
- const isAnyResultMetaData = results.some((s) => typeof s !== "boolean");
396
- if (isAnyResultMetaData) {
397
- return {
398
- $valid: results.some((result) => {
399
- if (typeof result === "boolean") {
400
- return !!result;
401
- }
402
- return result.$valid;
403
- }),
404
- ...results.reduce((acc, result) => {
405
- if (typeof result === "boolean") {
406
- return acc;
407
- }
408
- const { $valid, ...rest } = result;
409
- return { ...acc, ...rest };
410
- }, {})
411
- };
412
- } else {
413
- return results.some((result) => !!result);
414
- }
415
- }
416
- const validator = isAnyRuleAsync ? async (value, ...params) => {
417
- const results = await Promise.all(computeRules(rules, value, ...params));
418
- return computeMetadata(results);
419
- } : (value, ...params) => {
420
- const $rules = computeRules(rules, value, ...params);
421
- return computeMetadata($rules);
422
- };
423
- const newRule = createRule({
424
- type: "or",
425
- validator,
426
- message: "The value does not match any of the provided validators"
427
- });
428
- const newParams = [..._params ?? []];
429
- newRule._params = newParams;
430
- if (typeof newRule === "function") {
431
- const executedRule = newRule(...newParams);
432
- return executedRule;
433
- } else {
434
- return newRule;
435
- }
436
- }
437
- function not(rule, message) {
438
- let _type;
439
- let validator;
440
- let newValidator;
441
- let _params;
442
- let _async;
443
- if (typeof rule === "function") {
444
- validator = rule;
445
- _async = rule.constructor.name === "AsyncFunction";
446
- } else {
447
- ({ _type, validator, _params } = rule);
448
- _async = rule._async;
449
- }
450
- if (_async) {
451
- newValidator = async (value, ...params) => {
452
- if (isFilled(value)) {
453
- const result = await validator(value, ...params);
454
- return !result;
455
- }
456
- return true;
457
- };
458
- } else {
459
- newValidator = (value, ...params) => {
460
- if (isFilled(value)) {
461
- return !validator(value, ...params);
462
- }
463
- return true;
464
- };
465
- }
466
- const newRule = createRule({
467
- type: "not",
468
- validator: newValidator,
469
- message: message ?? "Error"
470
- });
471
- const newParams = [..._params ?? []];
472
- newRule._params = newParams;
473
- if (typeof newRule === "function") {
474
- const executedRule = newRule(...newParams);
475
- return executedRule;
476
- } else {
477
- return newRule;
478
- }
479
- }
480
- var alphaRegex = /^[a-zA-Z]*$/;
481
- var alphaSymbolRegex = /^[\w.]+$/;
482
- var alpha = createRule({
483
- type: "alpha",
484
- validator(value, options) {
485
- if (isEmpty(value)) {
486
- return true;
487
- }
488
- if (options?.allowSymbols) {
489
- return matchRegex(value, alphaSymbolRegex);
490
- }
491
- return matchRegex(value, alphaRegex);
492
- },
493
- message: "The value is not alphabetical"
494
- });
495
- var alphaNumRegex = /^[a-zA-Z0-9]*$/;
496
- var alphaNumSymbolRegex = /^[a-zA-Z0-9_]*$/;
497
- var alphaNum = createRule({
498
- type: "alphaNum",
499
- validator(value, options) {
500
- if (isEmpty(value)) {
501
- return true;
502
- }
503
- if (options?.allowSymbols) {
504
- return matchRegex(value, alphaNumSymbolRegex);
505
- }
506
- return matchRegex(value, alphaNumRegex);
507
- },
508
- message: "The value must be alpha-numeric"
509
- });
510
- var between = createRule({
511
- type: "between",
512
- validator: (value, min, max, options) => {
513
- const { allowEqual = true } = options ?? {};
514
- if (isFilled(value) && isFilled(min) && isFilled(max)) {
515
- const tValue = toNumber(value);
516
- const tMin = toNumber(min);
517
- const tMax = toNumber(max);
518
- if (isNumber(tValue) && isNumber(tMin) && isNumber(tMax)) {
519
- if (allowEqual) {
520
- return tValue >= tMin && tValue <= tMax;
521
- } else {
522
- return tValue > tMin && tValue < tMax;
523
- }
524
- }
525
- console.warn(`[between] Value or parameters aren't numbers, got value: ${value}, min: ${min}, max: ${max}`);
526
- return false;
527
- }
528
- return true;
529
- },
530
- message: ({ $params: [min, max] }) => {
531
- return `The value must be between ${min} and ${max}`;
532
- }
533
- });
534
- var boolean = createRule({
535
- type: "boolean",
536
- validator: (value) => {
537
- if (isFilled(value)) {
538
- return typeof value === "boolean";
539
- }
540
- return true;
541
- },
542
- message: "The value must be a native boolean"
543
- });
544
- var checked = createRule({
545
- type: "checked",
546
- validator: (value) => {
547
- if (isFilled(value)) {
548
- return value === true;
549
- }
550
- return true;
551
- },
552
- message: "The field must be checked"
553
- });
554
- var contains = createRule({
555
- type: "contains",
556
- validator(value, part) {
557
- if (isFilled(value) && isFilled(part)) {
558
- return value.includes(part);
559
- }
560
- return true;
561
- },
562
- message({ $params: [part] }) {
563
- return `The value must contain ${part}`;
564
- }
565
- });
566
- var date = createRule({
567
- type: "date",
568
- validator: (value) => {
569
- if (isFilled(value)) {
570
- return value instanceof Date;
571
- }
572
- return true;
573
- },
574
- message: "The value must be a native Date constructor"
575
- });
576
-
577
- // src/utils/getLocale.util.ts
578
- function getUserLocale() {
579
- if (navigator.languages != void 0) return navigator.languages[0];
580
- return navigator.language;
581
- }
582
- function formatLocaleDate(date2) {
583
- if (date2) {
584
- return new Intl.DateTimeFormat(getUserLocale(), { dateStyle: "short" }).format(new Date(date2));
585
- }
586
- return "?";
587
- }
588
-
589
- // src/rules/dateAfter.ts
590
- var dateAfter = createRule({
591
- type: "dateAfter",
592
- validator: (value, after, options) => {
593
- const { allowEqual = true } = options ?? {};
594
- if (isFilled(value) && isFilled(after)) {
595
- if (isDate(value) && isDate(after)) {
596
- const result = allowEqual ? toDate(value).getTime() >= toDate(after).getTime() : toDate(value).getTime() > toDate(after).getTime();
597
- if (result) {
598
- return true;
599
- }
600
- return { $valid: false, error: "date-not-after" };
601
- }
602
- return { $valid: false, error: "value-or-parameter-not-a-date" };
603
- }
604
- return true;
605
- },
606
- message: ({ $params: [after], error }) => {
607
- if (error === "value-or-parameter-not-a-date") {
608
- return "The values must be dates";
609
- }
610
- return `The date must be after ${formatLocaleDate(after)}`;
611
- }
612
- });
613
- var dateBefore = createRule({
614
- type: "dateBefore",
615
- validator: (value, before, options) => {
616
- const { allowEqual = true } = options ?? {};
617
- if (isFilled(value) && isFilled(before)) {
618
- if (isDate(value) && isDate(before)) {
619
- const result = allowEqual ? toDate(value).getTime() <= toDate(before).getTime() : toDate(value).getTime() < toDate(before).getTime();
620
- if (result) {
621
- return true;
622
- }
623
- return { $valid: false, error: "date-not-before" };
624
- }
625
- return { $valid: false, error: "value-or-parameter-not-a-date" };
626
- }
627
- return true;
628
- },
629
- message: ({ $params: [before], error }) => {
630
- if (error === "value-or-parameter-not-a-date") {
631
- return "The values must be dates";
632
- }
633
- return `The date must be before ${formatLocaleDate(before)}`;
634
- }
635
- });
636
- var dateBetween = createRule({
637
- type: "dateBetween",
638
- validator: (value, before, after, options) => {
639
- const { allowEqual = true } = options ?? {};
640
- if (isDate(value) && isDate(before) && isDate(after)) {
641
- if (allowEqual) {
642
- return toDate(value).getTime() >= toDate(before).getTime() && toDate(value).getTime() <= toDate(after).getTime();
643
- } else {
644
- return toDate(value).getTime() > toDate(before).getTime() && toDate(value).getTime() < toDate(after).getTime();
645
- }
646
- }
647
- return true;
648
- },
649
- message: ({ $params: [before, after] }) => {
650
- return `The date must be between ${formatLocaleDate(before)} and ${formatLocaleDate(after)}`;
651
- }
652
- });
653
- var decimalRegex = /^[-]?\d*(\.\d+)?$/;
654
- var decimal = createRule({
655
- type: "decimal",
656
- validator(value) {
657
- if (isEmpty(value)) {
658
- return true;
659
- }
660
- return matchRegex(value, decimalRegex);
661
- },
662
- message: "The value must be decimal"
663
- });
664
- var emailRegex = /^(?:[A-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]{2,}(?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i;
665
- var email = createRule({
666
- type: "email",
667
- validator(value) {
668
- if (isEmpty(value)) {
669
- return true;
670
- }
671
- return matchRegex(value, emailRegex);
672
- },
673
- message: "The value must be an valid email address"
674
- });
675
- var endsWith = createRule({
676
- type: "endsWith",
677
- validator(value, part) {
678
- if (isFilled(value) && isFilled(part)) {
679
- return value.endsWith(part);
680
- }
681
- return true;
682
- },
683
- message({ $params: [part] }) {
684
- return `The value must end with ${part}`;
685
- }
686
- });
687
- var exactLength = createRule({
688
- type: "exactLength",
689
- validator: (value, count) => {
690
- if (isFilled(value, false) && isFilled(count)) {
691
- if (isNumber(count)) {
692
- return getSize(value) === count;
693
- }
694
- console.warn(`[minLength] Parameter isn't a number, got parameter: ${count}`);
695
- return false;
696
- }
697
- return true;
698
- },
699
- message: ({ $params: [count] }) => {
700
- return `The value should be exactly ${count} characters long`;
701
- }
702
- });
703
- var exactValue = createRule({
704
- type: "exactValue",
705
- validator: (value, count) => {
706
- if (isFilled(value) && isFilled(count)) {
707
- if (isNumber(count) && !isNaN(toNumber(value))) {
708
- return toNumber(value) === count;
709
- }
710
- console.warn(`[exactValue] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
711
- return true;
712
- }
713
- return true;
714
- },
715
- message: ({ $params: [count] }) => {
716
- return `The value must be equal to ${count}`;
717
- }
718
- });
719
- var hexadecimalRegex = /^[a-fA-F0-9]*$/;
720
- var hexadecimal = createRule({
721
- type: "hexadecimal",
722
- validator(value) {
723
- if (isEmpty(value)) {
724
- return true;
725
- }
726
- return matchRegex(value, hexadecimalRegex);
727
- },
728
- message: "The value must be hexadecimal"
729
- });
730
- var integerRegex = /(^[0-9]*$)|(^-[0-9]+$)/;
731
- var integer = createRule({
732
- type: "integer",
733
- validator(value) {
734
- if (isEmpty(value)) {
735
- return true;
736
- }
737
- return matchRegex(value, integerRegex);
738
- },
739
- message: "The value must be an integer"
740
- });
741
- function nibbleValid(nibble) {
742
- if (nibble.length > 3 || nibble.length === 0) {
743
- return false;
744
- }
745
- if (nibble[0] === "0" && nibble !== "0") {
746
- return false;
747
- }
748
- if (!nibble.match(/^\d+$/)) {
749
- return false;
750
- }
751
- const numeric2 = +nibble | 0;
752
- return numeric2 >= 0 && numeric2 <= 255;
753
- }
754
- var ipv4Address = createRule({
755
- type: "ipv4Address",
756
- validator(value) {
757
- if (isEmpty(value)) {
758
- return true;
759
- }
760
- if (typeof value !== "string") {
761
- return false;
762
- }
763
- const nibbles = value.split(".");
764
- return nibbles.length === 4 && nibbles.every(nibbleValid);
765
- },
766
- message: "The value is not a valid IPv4 address"
767
- });
768
- function literal(literal2) {
769
- const params = computed(() => toValue(literal2));
770
- const rule = withMessage(
771
- withParams(
772
- (value, literal3) => {
773
- if (isFilled(value) && isFilled(literal3)) {
774
- return literal3 === value;
775
- }
776
- return true;
777
- },
778
- [params]
779
- ),
780
- ({ $params: [literal3] }) => `Value should be ${literal3}.`
781
- );
782
- return rule;
783
- }
784
- var macAddress = createRule({
785
- type: "macAddress",
786
- validator(value, separator = ":") {
787
- if (isEmpty(value)) {
788
- return true;
789
- }
790
- if (typeof value !== "string") {
791
- return false;
792
- }
793
- const parts = typeof separator === "string" && separator !== "" ? value.split(separator) : value.length === 12 || value.length === 16 ? value.match(/.{2}/g) : null;
794
- return parts !== null && (parts.length === 6 || parts.length === 8) && parts.every(hexValid);
795
- },
796
- message: "The value is not a valid MAC Address"
797
- });
798
- var hexValid = (hex) => hex.toLowerCase().match(/^[0-9a-f]{2}$/);
799
- var maxLength = createRule({
800
- type: "maxLength",
801
- validator: (value, count, options) => {
802
- const { allowEqual = true } = options ?? {};
803
- if (isFilled(value, false) && isFilled(count)) {
804
- if (isNumber(count)) {
805
- if (allowEqual) {
806
- return getSize(value) <= count;
807
- } else {
808
- return getSize(value) < count;
809
- }
810
- }
811
- console.warn(`[maxLength] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
812
- return false;
813
- }
814
- return true;
815
- },
816
- message: ({ $value, $params: [count] }) => {
817
- if (Array.isArray($value)) {
818
- return `This list should have maximum ${count} items`;
819
- }
820
- return `The value length should not exceed ${count}`;
821
- }
822
- });
823
- var maxValue = createRule({
824
- type: "maxValue",
825
- validator: (value, count, options) => {
826
- const { allowEqual = true } = options ?? {};
827
- if (isFilled(value) && isFilled(count)) {
828
- if (isNumber(count) && !isNaN(toNumber(value))) {
829
- if (allowEqual) {
830
- return toNumber(value) <= count;
831
- } else {
832
- return toNumber(value) < count;
833
- }
834
- }
835
- console.warn(`[maxValue] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
836
- return true;
837
- }
838
- return true;
839
- },
840
- message: ({ $params: [count, options] }) => {
841
- const { allowEqual = true } = options ?? {};
842
- if (allowEqual) {
843
- return `The value must be less than or equal to ${count}`;
844
- } else {
845
- return `The value must be less than ${count}`;
846
- }
847
- }
848
- });
849
- var minLength = createRule({
850
- type: "minLength",
851
- validator: (value, count, options) => {
852
- const { allowEqual = true } = options ?? {};
853
- if (isFilled(value, false) && isFilled(count)) {
854
- if (isNumber(count)) {
855
- if (allowEqual) {
856
- return getSize(value) >= count;
857
- } else {
858
- return getSize(value) > count;
859
- }
860
- }
861
- console.warn(`[minLength] Parameter isn't a number, got parameter: ${count}`);
862
- return false;
863
- }
864
- return true;
865
- },
866
- message: ({ $value, $params: [count] }) => {
867
- if (Array.isArray($value)) {
868
- return `The list should have at least ${count} items`;
869
- }
870
- return `The value length should be at least ${count}`;
871
- }
872
- });
873
- var minValue = createRule({
874
- type: "minValue",
875
- validator: (value, count, options) => {
876
- const { allowEqual = true } = options ?? {};
877
- if (isFilled(value) && isFilled(count)) {
878
- if (isNumber(count) && !isNaN(toNumber(value))) {
879
- if (allowEqual) {
880
- return toNumber(value) >= count;
881
- } else {
882
- return toNumber(value) > count;
883
- }
884
- }
885
- console.warn(`[minValue] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
886
- return true;
887
- }
888
- return true;
889
- },
890
- message: ({ $params: [count, options] }) => {
891
- const { allowEqual = true } = options ?? {};
892
- if (allowEqual) {
893
- return `The value must be greater than or equal to ${count}`;
894
- } else {
895
- return `The value must be greater than ${count}`;
896
- }
897
- }
898
- });
899
- function getValidEnumValues(obj) {
900
- const validKeys = Object.keys(obj).filter((k) => typeof obj[obj[k]] !== "number");
901
- const filtered = {};
902
- for (const k of validKeys) {
903
- filtered[k] = obj[k];
904
- }
905
- return Object.values(filtered);
906
- }
907
- function nativeEnum(enumLike) {
908
- const params = computed(() => toValue(enumLike));
909
- const rule = withMessage(
910
- withParams(
911
- (value, enumLike2) => {
912
- if (isFilled(value) && !isEmpty(enumLike2)) {
913
- const validValues = getValidEnumValues(enumLike2);
914
- return validValues.includes(value);
915
- }
916
- return true;
917
- },
918
- [params]
919
- ),
920
- ({ $params: [enumLike2] }) => `The value should be one of those options: ${Object.values(enumLike2).join(", ")}.`
921
- );
922
- return rule;
923
- }
924
- var number = createRule({
925
- type: "number",
926
- validator: (value) => {
927
- if (isFilled(value)) {
928
- return isNumber(value);
929
- }
930
- return true;
931
- },
932
- message: "The value must be a native number"
933
- });
934
- var numericRegex = /^\d*(\.\d+)?$/;
935
- var numeric = createRule({
936
- type: "numeric",
937
- validator(value) {
938
- if (isEmpty(value)) {
939
- return true;
940
- }
941
- return matchRegex(value, numericRegex);
942
- },
943
- message: "The value must be numeric"
944
- });
945
- function oneOf(options) {
946
- const params = computed(() => toValue(options));
947
- const rule = withMessage(
948
- withParams(
949
- (value, options2) => {
950
- if (isFilled(value) && isFilled(options2, false)) {
951
- return options2.includes(value);
952
- }
953
- return true;
954
- },
955
- [params]
956
- ),
957
- ({ $params: [options2] }) => `The value should be one of those options: ${options2.join(", ")}.`
958
- );
959
- return rule;
960
- }
961
- var regex = createRule({
962
- type: "regex",
963
- validator(value, regexp) {
964
- if (isFilled(value)) {
965
- const filteredRegexp = Array.isArray(regexp) ? regexp : [regexp];
966
- return matchRegex(value, ...filteredRegexp);
967
- }
968
- return true;
969
- },
970
- message: "The value does not match the required pattern"
971
- });
972
- var required = createRule({
973
- type: "required",
974
- validator: (value) => {
975
- return isFilled(value);
976
- },
977
- message: "This field is required"
978
- });
979
- var requiredIf = createRule({
980
- type: "required",
981
- validator(value, condition) {
982
- if (condition) {
983
- return isFilled(value);
984
- }
985
- return true;
986
- },
987
- message: "This field is required",
988
- active({ $params: [condition] }) {
989
- return condition;
990
- }
991
- });
992
- var requiredUnless = createRule({
993
- type: "required",
994
- validator(value, condition) {
995
- if (!condition) {
996
- return isFilled(value);
997
- }
998
- return true;
999
- },
1000
- message: "This field is required",
1001
- active({ $params: [condition] }) {
1002
- return !condition;
1003
- }
1004
- });
1005
- var sameAs = createRule({
1006
- type: "sameAs",
1007
- validator(value, target, otherName) {
1008
- if (isEmpty(value)) {
1009
- return true;
1010
- }
1011
- return value === target;
1012
- },
1013
- message({ $params: [_, otherName = "other"] }) {
1014
- return `The value must be equal to the ${otherName} value`;
1015
- }
1016
- });
1017
- var startsWith = createRule({
1018
- type: "startsWith",
1019
- validator(value, part) {
1020
- if (isFilled(value) && isFilled(part)) {
1021
- return value.startsWith(part);
1022
- }
1023
- return true;
1024
- },
1025
- message({ $params: [part] }) {
1026
- return `The value must end with ${part}`;
1027
- }
1028
- });
1029
- var string = createRule({
1030
- type: "string",
1031
- validator: (value) => {
1032
- if (isFilled(value)) {
1033
- return typeof value === "string";
1034
- }
1035
- return true;
1036
- },
1037
- message: "The value must be a string"
1038
- });
1039
-
1040
- // src/rules/type.ts
1041
- function type() {
1042
- return () => true;
1043
- }
1044
- var urlRegex = /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i;
1045
- var url = createRule({
1046
- type: "url",
1047
- validator(value) {
1048
- if (isEmpty(value)) {
1049
- return true;
1050
- }
1051
- return matchRegex(value, urlRegex);
1052
- },
1053
- message: "The value is not a valid URL address"
1054
- });
1055
-
1056
- export { alpha, alphaNum, and, applyIf, between, boolean, checked, contains, date, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, getSize, hexadecimal, integer, ipv4Address, isDate, isEmpty, isFilled, isNumber, literal, macAddress, matchRegex, maxLength, maxValue, minLength, minValue, nativeEnum, not, number, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, string, toDate, toNumber, type, url, withAsync, withMessage, withParams, withTooltip };