@regle/rules 1.2.0-beta.1 → 1.2.0-beta.3
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/README.md +16 -11
- package/dist/regle-rules.d.ts +175 -133
- package/dist/regle-rules.min.mjs +1 -1
- package/dist/regle-rules.mjs +372 -308
- package/package.json +9 -9
package/dist/regle-rules.mjs
CHANGED
|
@@ -23,9 +23,12 @@ function withMessage(rule, newMessage) {
|
|
|
23
23
|
newRule._params = newParams;
|
|
24
24
|
newRule._message_patched = true;
|
|
25
25
|
if (typeof newRule === "function") {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
if (_params != null) {
|
|
27
|
+
const executedRule = newRule(...newParams);
|
|
28
|
+
executedRule._message_patched = true;
|
|
29
|
+
return executedRule;
|
|
30
|
+
}
|
|
31
|
+
return newRule;
|
|
29
32
|
} else {
|
|
30
33
|
return newRule;
|
|
31
34
|
}
|
|
@@ -177,11 +180,11 @@ function isDate(value) {
|
|
|
177
180
|
if (value instanceof Date) {
|
|
178
181
|
possibleDate = value;
|
|
179
182
|
} else if (typeof value === "string") {
|
|
180
|
-
const
|
|
181
|
-
if (
|
|
183
|
+
const date2 = new Date(value);
|
|
184
|
+
if (date2.toString() === "Invalid Date") {
|
|
182
185
|
return false;
|
|
183
186
|
}
|
|
184
|
-
possibleDate =
|
|
187
|
+
possibleDate = date2;
|
|
185
188
|
}
|
|
186
189
|
return !!possibleDate;
|
|
187
190
|
} catch (e) {
|
|
@@ -469,50 +472,6 @@ function not(rule, message) {
|
|
|
469
472
|
return newRule;
|
|
470
473
|
}
|
|
471
474
|
}
|
|
472
|
-
var required = createRule({
|
|
473
|
-
type: "required",
|
|
474
|
-
validator: (value) => {
|
|
475
|
-
return isFilled(value);
|
|
476
|
-
},
|
|
477
|
-
message: "This field is required"
|
|
478
|
-
});
|
|
479
|
-
var maxLength = createRule({
|
|
480
|
-
type: "maxLength",
|
|
481
|
-
validator: (value, count, options) => {
|
|
482
|
-
const { allowEqual = true } = options ?? {};
|
|
483
|
-
if (isFilled(value, false) && isFilled(count)) {
|
|
484
|
-
if (isNumber(count)) {
|
|
485
|
-
if (allowEqual) {
|
|
486
|
-
return getSize(value) <= count;
|
|
487
|
-
} else {
|
|
488
|
-
return getSize(value) < count;
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
console.warn(`[maxLength] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
|
|
492
|
-
return false;
|
|
493
|
-
}
|
|
494
|
-
return true;
|
|
495
|
-
},
|
|
496
|
-
message: ({ $value, $params: [count] }) => {
|
|
497
|
-
if (Array.isArray($value)) {
|
|
498
|
-
return `This list should have maximum ${count} items`;
|
|
499
|
-
}
|
|
500
|
-
return `The value length should not exceed ${count}`;
|
|
501
|
-
}
|
|
502
|
-
});
|
|
503
|
-
var requiredIf = createRule({
|
|
504
|
-
type: "required",
|
|
505
|
-
validator(value, condition) {
|
|
506
|
-
if (condition) {
|
|
507
|
-
return isFilled(value);
|
|
508
|
-
}
|
|
509
|
-
return true;
|
|
510
|
-
},
|
|
511
|
-
message: "This field is required",
|
|
512
|
-
active({ $params: [condition] }) {
|
|
513
|
-
return condition;
|
|
514
|
-
}
|
|
515
|
-
});
|
|
516
475
|
var alphaRegex = /^[a-zA-Z]*$/;
|
|
517
476
|
var alphaSymbolRegex = /^[\w.]+$/;
|
|
518
477
|
var alpha = createRule({
|
|
@@ -567,193 +526,47 @@ var between = createRule({
|
|
|
567
526
|
return `The value must be between ${min} and ${max}`;
|
|
568
527
|
}
|
|
569
528
|
});
|
|
570
|
-
var
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
return true;
|
|
576
|
-
}
|
|
577
|
-
return matchRegex(value, decimalRegex);
|
|
578
|
-
},
|
|
579
|
-
message: "The value must be decimal"
|
|
580
|
-
});
|
|
581
|
-
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;
|
|
582
|
-
var email = createRule({
|
|
583
|
-
type: "email",
|
|
584
|
-
validator(value) {
|
|
585
|
-
if (isEmpty(value)) {
|
|
586
|
-
return true;
|
|
587
|
-
}
|
|
588
|
-
return matchRegex(value, emailRegex);
|
|
589
|
-
},
|
|
590
|
-
message: "The value must be an valid email address"
|
|
591
|
-
});
|
|
592
|
-
var integerRegex = /(^[0-9]*$)|(^-[0-9]+$)/;
|
|
593
|
-
var integer = createRule({
|
|
594
|
-
type: "integer",
|
|
595
|
-
validator(value) {
|
|
596
|
-
if (isEmpty(value)) {
|
|
597
|
-
return true;
|
|
598
|
-
}
|
|
599
|
-
return matchRegex(value, integerRegex);
|
|
600
|
-
},
|
|
601
|
-
message: "The value must be an integer"
|
|
602
|
-
});
|
|
603
|
-
var maxValue = createRule({
|
|
604
|
-
type: "maxValue",
|
|
605
|
-
validator: (value, count, options) => {
|
|
606
|
-
const { allowEqual = true } = options ?? {};
|
|
607
|
-
if (isFilled(value) && isFilled(count)) {
|
|
608
|
-
if (isNumber(count) && !isNaN(toNumber(value))) {
|
|
609
|
-
if (allowEqual) {
|
|
610
|
-
return toNumber(value) <= count;
|
|
611
|
-
} else {
|
|
612
|
-
return toNumber(value) < count;
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
console.warn(`[maxValue] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
|
|
616
|
-
return true;
|
|
617
|
-
}
|
|
618
|
-
return true;
|
|
619
|
-
},
|
|
620
|
-
message: ({ $params: [count, options] }) => {
|
|
621
|
-
const { allowEqual = true } = options ?? {};
|
|
622
|
-
if (allowEqual) {
|
|
623
|
-
return `The value must be less than or equal to ${count}`;
|
|
624
|
-
} else {
|
|
625
|
-
return `The value must be less than ${count}`;
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
});
|
|
629
|
-
var minLength = createRule({
|
|
630
|
-
type: "minLength",
|
|
631
|
-
validator: (value, count, options) => {
|
|
632
|
-
const { allowEqual = true } = options ?? {};
|
|
633
|
-
if (isFilled(value, false) && isFilled(count)) {
|
|
634
|
-
if (isNumber(count)) {
|
|
635
|
-
if (allowEqual) {
|
|
636
|
-
return getSize(value) >= count;
|
|
637
|
-
} else {
|
|
638
|
-
return getSize(value) > count;
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
console.warn(`[minLength] Parameter isn't a number, got parameter: ${count}`);
|
|
642
|
-
return false;
|
|
643
|
-
}
|
|
644
|
-
return true;
|
|
645
|
-
},
|
|
646
|
-
message: ({ $value, $params: [count] }) => {
|
|
647
|
-
if (Array.isArray($value)) {
|
|
648
|
-
return `The list should have at least ${count} items`;
|
|
649
|
-
}
|
|
650
|
-
return `The value length should be at least ${count}`;
|
|
651
|
-
}
|
|
652
|
-
});
|
|
653
|
-
var minValue = createRule({
|
|
654
|
-
type: "minValue",
|
|
655
|
-
validator: (value, count, options) => {
|
|
656
|
-
const { allowEqual = true } = options ?? {};
|
|
657
|
-
if (isFilled(value) && isFilled(count)) {
|
|
658
|
-
if (isNumber(count) && !isNaN(toNumber(value))) {
|
|
659
|
-
if (allowEqual) {
|
|
660
|
-
return toNumber(value) >= count;
|
|
661
|
-
} else {
|
|
662
|
-
return toNumber(value) > count;
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
console.warn(`[minValue] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
|
|
666
|
-
return true;
|
|
529
|
+
var boolean = createRule({
|
|
530
|
+
type: "boolean",
|
|
531
|
+
validator: (value) => {
|
|
532
|
+
if (isFilled(value)) {
|
|
533
|
+
return typeof value === "boolean";
|
|
667
534
|
}
|
|
668
535
|
return true;
|
|
669
536
|
},
|
|
670
|
-
message:
|
|
671
|
-
const { allowEqual = true } = options ?? {};
|
|
672
|
-
if (allowEqual) {
|
|
673
|
-
return `The value must be greater than or equal to ${count}`;
|
|
674
|
-
} else {
|
|
675
|
-
return `The value must be greater than ${count}`;
|
|
676
|
-
}
|
|
677
|
-
}
|
|
537
|
+
message: "The value must be a native boolean"
|
|
678
538
|
});
|
|
679
|
-
var
|
|
680
|
-
type: "
|
|
681
|
-
validator: (value
|
|
682
|
-
if (isFilled(value)
|
|
683
|
-
|
|
684
|
-
return toNumber(value) === count;
|
|
685
|
-
}
|
|
686
|
-
console.warn(`[exactValue] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
|
|
687
|
-
return true;
|
|
539
|
+
var checked = createRule({
|
|
540
|
+
type: "checked",
|
|
541
|
+
validator: (value) => {
|
|
542
|
+
if (isFilled(value)) {
|
|
543
|
+
return value === true;
|
|
688
544
|
}
|
|
689
545
|
return true;
|
|
690
546
|
},
|
|
691
|
-
message:
|
|
692
|
-
return `The value must be equal to ${count}`;
|
|
693
|
-
}
|
|
547
|
+
message: "The field must be checked"
|
|
694
548
|
});
|
|
695
|
-
var
|
|
696
|
-
type: "
|
|
697
|
-
validator
|
|
698
|
-
if (isFilled(value
|
|
699
|
-
|
|
700
|
-
return getSize(value) === count;
|
|
701
|
-
}
|
|
702
|
-
console.warn(`[minLength] Parameter isn't a number, got parameter: ${count}`);
|
|
703
|
-
return false;
|
|
549
|
+
var contains = createRule({
|
|
550
|
+
type: "contains",
|
|
551
|
+
validator(value, part) {
|
|
552
|
+
if (isFilled(value) && isFilled(part)) {
|
|
553
|
+
return value.includes(part);
|
|
704
554
|
}
|
|
705
555
|
return true;
|
|
706
556
|
},
|
|
707
|
-
message
|
|
708
|
-
return `The value
|
|
557
|
+
message({ $params: [part] }) {
|
|
558
|
+
return `The value must contain ${part}`;
|
|
709
559
|
}
|
|
710
560
|
});
|
|
711
|
-
var
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
return true;
|
|
717
|
-
}
|
|
718
|
-
return matchRegex(value, numericRegex);
|
|
719
|
-
},
|
|
720
|
-
message: "The value must be numeric"
|
|
721
|
-
});
|
|
722
|
-
var requiredUnless = createRule({
|
|
723
|
-
type: "required",
|
|
724
|
-
validator(value, condition) {
|
|
725
|
-
if (!condition) {
|
|
726
|
-
return isFilled(value);
|
|
561
|
+
var date = createRule({
|
|
562
|
+
type: "date",
|
|
563
|
+
validator: (value) => {
|
|
564
|
+
if (isFilled(value)) {
|
|
565
|
+
return value instanceof Date;
|
|
727
566
|
}
|
|
728
567
|
return true;
|
|
729
568
|
},
|
|
730
|
-
message: "
|
|
731
|
-
active({ $params: [condition] }) {
|
|
732
|
-
return !condition;
|
|
733
|
-
}
|
|
734
|
-
});
|
|
735
|
-
var sameAs = createRule({
|
|
736
|
-
type: "sameAs",
|
|
737
|
-
validator(value, target, otherName) {
|
|
738
|
-
if (isEmpty(value)) {
|
|
739
|
-
return true;
|
|
740
|
-
}
|
|
741
|
-
return value === target;
|
|
742
|
-
},
|
|
743
|
-
message({ $params: [_, otherName = "other"] }) {
|
|
744
|
-
return `The value must be equal to the ${otherName} value`;
|
|
745
|
-
}
|
|
746
|
-
});
|
|
747
|
-
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;
|
|
748
|
-
var url = createRule({
|
|
749
|
-
type: "url",
|
|
750
|
-
validator(value) {
|
|
751
|
-
if (isEmpty(value)) {
|
|
752
|
-
return true;
|
|
753
|
-
}
|
|
754
|
-
return matchRegex(value, urlRegex);
|
|
755
|
-
},
|
|
756
|
-
message: "The value is not a valid URL address"
|
|
569
|
+
message: "The value must be a native Date constructor"
|
|
757
570
|
});
|
|
758
571
|
|
|
759
572
|
// src/utils/getLocale.util.ts
|
|
@@ -761,9 +574,9 @@ function getUserLocale() {
|
|
|
761
574
|
if (navigator.languages != void 0) return navigator.languages[0];
|
|
762
575
|
return navigator.language;
|
|
763
576
|
}
|
|
764
|
-
function formatLocaleDate(
|
|
765
|
-
if (
|
|
766
|
-
return new Intl.DateTimeFormat(getUserLocale(), { dateStyle: "short" }).format(new Date(
|
|
577
|
+
function formatLocaleDate(date2) {
|
|
578
|
+
if (date2) {
|
|
579
|
+
return new Intl.DateTimeFormat(getUserLocale(), { dateStyle: "short" }).format(new Date(date2));
|
|
767
580
|
}
|
|
768
581
|
return "?";
|
|
769
582
|
}
|
|
@@ -771,21 +584,22 @@ function formatLocaleDate(date) {
|
|
|
771
584
|
// src/rules/dateAfter.ts
|
|
772
585
|
var dateAfter = createRule({
|
|
773
586
|
type: "dateAfter",
|
|
774
|
-
validator: (value, after) => {
|
|
587
|
+
validator: (value, after, options) => {
|
|
588
|
+
const { allowEqual = true } = options ?? {};
|
|
775
589
|
if (isFilled(value) && isFilled(after)) {
|
|
776
590
|
if (isDate(value) && isDate(after)) {
|
|
777
|
-
const result = toDate(value).getTime() > toDate(after).getTime();
|
|
591
|
+
const result = allowEqual ? toDate(value).getTime() >= toDate(after).getTime() : toDate(value).getTime() > toDate(after).getTime();
|
|
778
592
|
if (result) {
|
|
779
593
|
return true;
|
|
780
594
|
}
|
|
781
595
|
return { $valid: false, error: "date-not-after" };
|
|
782
596
|
}
|
|
783
|
-
return { $valid: false, error: "value-or-
|
|
597
|
+
return { $valid: false, error: "value-or-parameter-not-a-date" };
|
|
784
598
|
}
|
|
785
599
|
return true;
|
|
786
600
|
},
|
|
787
601
|
message: ({ $params: [after], error }) => {
|
|
788
|
-
if (error === "value-or-
|
|
602
|
+
if (error === "value-or-parameter-not-a-date") {
|
|
789
603
|
return "The values must be dates";
|
|
790
604
|
}
|
|
791
605
|
return `The date must be after ${formatLocaleDate(after)}`;
|
|
@@ -793,21 +607,22 @@ var dateAfter = createRule({
|
|
|
793
607
|
});
|
|
794
608
|
var dateBefore = createRule({
|
|
795
609
|
type: "dateBefore",
|
|
796
|
-
validator: (value, before) => {
|
|
610
|
+
validator: (value, before, options) => {
|
|
611
|
+
const { allowEqual = true } = options ?? {};
|
|
797
612
|
if (isFilled(value) && isFilled(before)) {
|
|
798
613
|
if (isDate(value) && isDate(before)) {
|
|
799
|
-
const result = toDate(value).getTime() < toDate(before).getTime();
|
|
614
|
+
const result = allowEqual ? toDate(value).getTime() <= toDate(before).getTime() : toDate(value).getTime() < toDate(before).getTime();
|
|
800
615
|
if (result) {
|
|
801
616
|
return true;
|
|
802
617
|
}
|
|
803
618
|
return { $valid: false, error: "date-not-before" };
|
|
804
619
|
}
|
|
805
|
-
return { $valid: false, error: "value-or-
|
|
620
|
+
return { $valid: false, error: "value-or-parameter-not-a-date" };
|
|
806
621
|
}
|
|
807
622
|
return true;
|
|
808
623
|
},
|
|
809
624
|
message: ({ $params: [before], error }) => {
|
|
810
|
-
if (error === "value-or-
|
|
625
|
+
if (error === "value-or-parameter-not-a-date") {
|
|
811
626
|
return "The values must be dates";
|
|
812
627
|
}
|
|
813
628
|
return `The date must be before ${formatLocaleDate(before)}`;
|
|
@@ -815,9 +630,14 @@ var dateBefore = createRule({
|
|
|
815
630
|
});
|
|
816
631
|
var dateBetween = createRule({
|
|
817
632
|
type: "dateBetween",
|
|
818
|
-
validator: (value, before, after) => {
|
|
633
|
+
validator: (value, before, after, options) => {
|
|
634
|
+
const { allowEqual = true } = options ?? {};
|
|
819
635
|
if (isDate(value) && isDate(before) && isDate(after)) {
|
|
820
|
-
|
|
636
|
+
if (allowEqual) {
|
|
637
|
+
return toDate(value).getTime() >= toDate(before).getTime() && toDate(value).getTime() <= toDate(after).getTime();
|
|
638
|
+
} else {
|
|
639
|
+
return toDate(value).getTime() > toDate(before).getTime() && toDate(value).getTime() < toDate(after).getTime();
|
|
640
|
+
}
|
|
821
641
|
}
|
|
822
642
|
return true;
|
|
823
643
|
},
|
|
@@ -825,21 +645,109 @@ var dateBetween = createRule({
|
|
|
825
645
|
return `The date must be between ${formatLocaleDate(before)} and ${formatLocaleDate(after)}`;
|
|
826
646
|
}
|
|
827
647
|
});
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
648
|
+
var decimalRegex = /^[-]?\d*(\.\d+)?$/;
|
|
649
|
+
var decimal = createRule({
|
|
650
|
+
type: "decimal",
|
|
651
|
+
validator(value) {
|
|
652
|
+
if (isEmpty(value)) {
|
|
653
|
+
return true;
|
|
654
|
+
}
|
|
655
|
+
return matchRegex(value, decimalRegex);
|
|
656
|
+
},
|
|
657
|
+
message: "The value must be decimal"
|
|
658
|
+
});
|
|
659
|
+
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;
|
|
660
|
+
var email = createRule({
|
|
661
|
+
type: "email",
|
|
662
|
+
validator(value) {
|
|
663
|
+
if (isEmpty(value)) {
|
|
664
|
+
return true;
|
|
665
|
+
}
|
|
666
|
+
return matchRegex(value, emailRegex);
|
|
667
|
+
},
|
|
668
|
+
message: "The value must be an valid email address"
|
|
669
|
+
});
|
|
670
|
+
var endsWith = createRule({
|
|
671
|
+
type: "endsWith",
|
|
672
|
+
validator(value, part) {
|
|
673
|
+
if (isFilled(value) && isFilled(part)) {
|
|
674
|
+
return value.endsWith(part);
|
|
675
|
+
}
|
|
676
|
+
return true;
|
|
677
|
+
},
|
|
678
|
+
message({ $params: [part] }) {
|
|
679
|
+
return `The value must end with ${part}`;
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
var exactLength = createRule({
|
|
683
|
+
type: "exactLength",
|
|
684
|
+
validator: (value, count) => {
|
|
685
|
+
if (isFilled(value, false) && isFilled(count)) {
|
|
686
|
+
if (isNumber(count)) {
|
|
687
|
+
return getSize(value) === count;
|
|
688
|
+
}
|
|
689
|
+
console.warn(`[minLength] Parameter isn't a number, got parameter: ${count}`);
|
|
690
|
+
return false;
|
|
691
|
+
}
|
|
692
|
+
return true;
|
|
693
|
+
},
|
|
694
|
+
message: ({ $params: [count] }) => {
|
|
695
|
+
return `The value should be exactly ${count} characters long`;
|
|
696
|
+
}
|
|
697
|
+
});
|
|
698
|
+
var exactValue = createRule({
|
|
699
|
+
type: "exactValue",
|
|
700
|
+
validator: (value, count) => {
|
|
701
|
+
if (isFilled(value) && isFilled(count)) {
|
|
702
|
+
if (isNumber(count) && !isNaN(toNumber(value))) {
|
|
703
|
+
return toNumber(value) === count;
|
|
704
|
+
}
|
|
705
|
+
console.warn(`[exactValue] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
|
|
706
|
+
return true;
|
|
707
|
+
}
|
|
708
|
+
return true;
|
|
709
|
+
},
|
|
710
|
+
message: ({ $params: [count] }) => {
|
|
711
|
+
return `The value must be equal to ${count}`;
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
var hexadecimalRegex = /^[a-fA-F0-9]*$/;
|
|
715
|
+
var hexadecimal = createRule({
|
|
716
|
+
type: "hexadecimal",
|
|
717
|
+
validator(value) {
|
|
718
|
+
if (isEmpty(value)) {
|
|
719
|
+
return true;
|
|
720
|
+
}
|
|
721
|
+
return matchRegex(value, hexadecimalRegex);
|
|
722
|
+
},
|
|
723
|
+
message: "The value must be hexadecimal"
|
|
724
|
+
});
|
|
725
|
+
var integerRegex = /(^[0-9]*$)|(^-[0-9]+$)/;
|
|
726
|
+
var integer = createRule({
|
|
727
|
+
type: "integer",
|
|
728
|
+
validator(value) {
|
|
729
|
+
if (isEmpty(value)) {
|
|
730
|
+
return true;
|
|
731
|
+
}
|
|
732
|
+
return matchRegex(value, integerRegex);
|
|
733
|
+
},
|
|
734
|
+
message: "The value must be an integer"
|
|
735
|
+
});
|
|
736
|
+
function nibbleValid(nibble) {
|
|
737
|
+
if (nibble.length > 3 || nibble.length === 0) {
|
|
738
|
+
return false;
|
|
739
|
+
}
|
|
740
|
+
if (nibble[0] === "0" && nibble !== "0") {
|
|
741
|
+
return false;
|
|
742
|
+
}
|
|
743
|
+
if (!nibble.match(/^\d+$/)) {
|
|
744
|
+
return false;
|
|
745
|
+
}
|
|
746
|
+
const numeric2 = +nibble | 0;
|
|
747
|
+
return numeric2 >= 0 && numeric2 <= 255;
|
|
748
|
+
}
|
|
749
|
+
var ipv4Address = createRule({
|
|
750
|
+
type: "ipv4Address",
|
|
843
751
|
validator(value) {
|
|
844
752
|
if (isEmpty(value)) {
|
|
845
753
|
return true;
|
|
@@ -852,6 +760,22 @@ var ipv4Address = createRule({
|
|
|
852
760
|
},
|
|
853
761
|
message: "The value is not a valid IPv4 address"
|
|
854
762
|
});
|
|
763
|
+
function literal(literal2) {
|
|
764
|
+
const params = computed(() => toValue(literal2));
|
|
765
|
+
const rule = withMessage(
|
|
766
|
+
withParams(
|
|
767
|
+
(value, literal3) => {
|
|
768
|
+
if (isFilled(value) && isFilled(literal3)) {
|
|
769
|
+
return literal3 === value;
|
|
770
|
+
}
|
|
771
|
+
return true;
|
|
772
|
+
},
|
|
773
|
+
[params]
|
|
774
|
+
),
|
|
775
|
+
({ $params: [literal3] }) => `Value should be ${literal3}.`
|
|
776
|
+
);
|
|
777
|
+
return rule;
|
|
778
|
+
}
|
|
855
779
|
var macAddress = createRule({
|
|
856
780
|
type: "macAddress",
|
|
857
781
|
validator(value, separator = ":") {
|
|
@@ -867,79 +791,106 @@ var macAddress = createRule({
|
|
|
867
791
|
message: "The value is not a valid MAC Address"
|
|
868
792
|
});
|
|
869
793
|
var hexValid = (hex) => hex.toLowerCase().match(/^[0-9a-f]{2}$/);
|
|
870
|
-
var
|
|
871
|
-
type: "
|
|
872
|
-
validator: (value) => {
|
|
873
|
-
|
|
874
|
-
|
|
794
|
+
var maxLength = createRule({
|
|
795
|
+
type: "maxLength",
|
|
796
|
+
validator: (value, count, options) => {
|
|
797
|
+
const { allowEqual = true } = options ?? {};
|
|
798
|
+
if (isFilled(value, false) && isFilled(count)) {
|
|
799
|
+
if (isNumber(count)) {
|
|
800
|
+
if (allowEqual) {
|
|
801
|
+
return getSize(value) <= count;
|
|
802
|
+
} else {
|
|
803
|
+
return getSize(value) < count;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
console.warn(`[maxLength] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
|
|
807
|
+
return false;
|
|
875
808
|
}
|
|
876
809
|
return true;
|
|
877
810
|
},
|
|
878
|
-
message:
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
type: "contains",
|
|
882
|
-
validator(value, part) {
|
|
883
|
-
if (isFilled(value) && isFilled(part)) {
|
|
884
|
-
return value.includes(part);
|
|
811
|
+
message: ({ $value, $params: [count] }) => {
|
|
812
|
+
if (Array.isArray($value)) {
|
|
813
|
+
return `This list should have maximum ${count} items`;
|
|
885
814
|
}
|
|
886
|
-
return
|
|
887
|
-
},
|
|
888
|
-
message({ $params: [part] }) {
|
|
889
|
-
return `The value must contain ${part}`;
|
|
815
|
+
return `The value length should not exceed ${count}`;
|
|
890
816
|
}
|
|
891
817
|
});
|
|
892
|
-
var
|
|
893
|
-
type: "
|
|
894
|
-
validator(value,
|
|
895
|
-
|
|
896
|
-
|
|
818
|
+
var maxValue = createRule({
|
|
819
|
+
type: "maxValue",
|
|
820
|
+
validator: (value, count, options) => {
|
|
821
|
+
const { allowEqual = true } = options ?? {};
|
|
822
|
+
if (isFilled(value) && isFilled(count)) {
|
|
823
|
+
if (isNumber(count) && !isNaN(toNumber(value))) {
|
|
824
|
+
if (allowEqual) {
|
|
825
|
+
return toNumber(value) <= count;
|
|
826
|
+
} else {
|
|
827
|
+
return toNumber(value) < count;
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
console.warn(`[maxValue] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
|
|
831
|
+
return true;
|
|
897
832
|
}
|
|
898
833
|
return true;
|
|
899
834
|
},
|
|
900
|
-
message({ $params: [
|
|
901
|
-
|
|
835
|
+
message: ({ $params: [count, options] }) => {
|
|
836
|
+
const { allowEqual = true } = options ?? {};
|
|
837
|
+
if (allowEqual) {
|
|
838
|
+
return `The value must be less than or equal to ${count}`;
|
|
839
|
+
} else {
|
|
840
|
+
return `The value must be less than ${count}`;
|
|
841
|
+
}
|
|
902
842
|
}
|
|
903
843
|
});
|
|
904
|
-
var
|
|
905
|
-
type: "
|
|
906
|
-
validator(value,
|
|
907
|
-
|
|
908
|
-
|
|
844
|
+
var minLength = createRule({
|
|
845
|
+
type: "minLength",
|
|
846
|
+
validator: (value, count, options) => {
|
|
847
|
+
const { allowEqual = true } = options ?? {};
|
|
848
|
+
if (isFilled(value, false) && isFilled(count)) {
|
|
849
|
+
if (isNumber(count)) {
|
|
850
|
+
if (allowEqual) {
|
|
851
|
+
return getSize(value) >= count;
|
|
852
|
+
} else {
|
|
853
|
+
return getSize(value) > count;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
console.warn(`[minLength] Parameter isn't a number, got parameter: ${count}`);
|
|
857
|
+
return false;
|
|
909
858
|
}
|
|
910
859
|
return true;
|
|
911
860
|
},
|
|
912
|
-
message({ $params: [
|
|
913
|
-
|
|
861
|
+
message: ({ $value, $params: [count] }) => {
|
|
862
|
+
if (Array.isArray($value)) {
|
|
863
|
+
return `The list should have at least ${count} items`;
|
|
864
|
+
}
|
|
865
|
+
return `The value length should be at least ${count}`;
|
|
914
866
|
}
|
|
915
867
|
});
|
|
916
|
-
var
|
|
917
|
-
type: "
|
|
918
|
-
validator(value,
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
868
|
+
var minValue = createRule({
|
|
869
|
+
type: "minValue",
|
|
870
|
+
validator: (value, count, options) => {
|
|
871
|
+
const { allowEqual = true } = options ?? {};
|
|
872
|
+
if (isFilled(value) && isFilled(count)) {
|
|
873
|
+
if (isNumber(count) && !isNaN(toNumber(value))) {
|
|
874
|
+
if (allowEqual) {
|
|
875
|
+
return toNumber(value) >= count;
|
|
876
|
+
} else {
|
|
877
|
+
return toNumber(value) > count;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
console.warn(`[minValue] Value or parameter isn't a number, got value: ${value}, parameter: ${count}`);
|
|
881
|
+
return true;
|
|
922
882
|
}
|
|
923
883
|
return true;
|
|
924
884
|
},
|
|
925
|
-
message:
|
|
885
|
+
message: ({ $params: [count, options] }) => {
|
|
886
|
+
const { allowEqual = true } = options ?? {};
|
|
887
|
+
if (allowEqual) {
|
|
888
|
+
return `The value must be greater than or equal to ${count}`;
|
|
889
|
+
} else {
|
|
890
|
+
return `The value must be greater than ${count}`;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
926
893
|
});
|
|
927
|
-
function oneOf(options) {
|
|
928
|
-
const params = computed(() => toValue(options));
|
|
929
|
-
const rule = withMessage(
|
|
930
|
-
withParams(
|
|
931
|
-
(value, options2) => {
|
|
932
|
-
if (isFilled(value) && isFilled(options2, false)) {
|
|
933
|
-
return options2.includes(value);
|
|
934
|
-
}
|
|
935
|
-
return true;
|
|
936
|
-
},
|
|
937
|
-
[params]
|
|
938
|
-
),
|
|
939
|
-
({ $params: [options2] }) => `The value should be one of those options: ${options2.join(", ")}.`
|
|
940
|
-
);
|
|
941
|
-
return rule;
|
|
942
|
-
}
|
|
943
894
|
function getValidEnumValues(obj) {
|
|
944
895
|
const validKeys = Object.keys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
945
896
|
const filtered = {};
|
|
@@ -965,23 +916,136 @@ function nativeEnum(enumLike) {
|
|
|
965
916
|
);
|
|
966
917
|
return rule;
|
|
967
918
|
}
|
|
968
|
-
|
|
969
|
-
|
|
919
|
+
var number = createRule({
|
|
920
|
+
type: "number",
|
|
921
|
+
validator: (value) => {
|
|
922
|
+
if (isFilled(value)) {
|
|
923
|
+
return isNumber(value);
|
|
924
|
+
}
|
|
925
|
+
return true;
|
|
926
|
+
},
|
|
927
|
+
message: "The value must be a native number"
|
|
928
|
+
});
|
|
929
|
+
var numericRegex = /^\d*(\.\d+)?$/;
|
|
930
|
+
var numeric = createRule({
|
|
931
|
+
type: "numeric",
|
|
932
|
+
validator(value) {
|
|
933
|
+
if (isEmpty(value)) {
|
|
934
|
+
return true;
|
|
935
|
+
}
|
|
936
|
+
return matchRegex(value, numericRegex);
|
|
937
|
+
},
|
|
938
|
+
message: "The value must be numeric"
|
|
939
|
+
});
|
|
940
|
+
function oneOf(options) {
|
|
941
|
+
const params = computed(() => toValue(options));
|
|
970
942
|
const rule = withMessage(
|
|
971
943
|
withParams(
|
|
972
|
-
(value,
|
|
973
|
-
|
|
944
|
+
(value, options2) => {
|
|
945
|
+
if (isFilled(value) && isFilled(options2, false)) {
|
|
946
|
+
return options2.includes(value);
|
|
947
|
+
}
|
|
948
|
+
return true;
|
|
974
949
|
},
|
|
975
950
|
[params]
|
|
976
951
|
),
|
|
977
|
-
({ $params: [
|
|
952
|
+
({ $params: [options2] }) => `The value should be one of those options: ${options2.join(", ")}.`
|
|
978
953
|
);
|
|
979
954
|
return rule;
|
|
980
955
|
}
|
|
956
|
+
var regex = createRule({
|
|
957
|
+
type: "regex",
|
|
958
|
+
validator(value, regexp) {
|
|
959
|
+
if (isFilled(value)) {
|
|
960
|
+
const filteredRegexp = Array.isArray(regexp) ? regexp : [regexp];
|
|
961
|
+
return matchRegex(value, ...filteredRegexp);
|
|
962
|
+
}
|
|
963
|
+
return true;
|
|
964
|
+
},
|
|
965
|
+
message: "The value does not match the required pattern"
|
|
966
|
+
});
|
|
967
|
+
var required = createRule({
|
|
968
|
+
type: "required",
|
|
969
|
+
validator: (value) => {
|
|
970
|
+
return isFilled(value);
|
|
971
|
+
},
|
|
972
|
+
message: "This field is required"
|
|
973
|
+
});
|
|
974
|
+
var requiredIf = createRule({
|
|
975
|
+
type: "required",
|
|
976
|
+
validator(value, condition) {
|
|
977
|
+
if (condition) {
|
|
978
|
+
return isFilled(value);
|
|
979
|
+
}
|
|
980
|
+
return true;
|
|
981
|
+
},
|
|
982
|
+
message: "This field is required",
|
|
983
|
+
active({ $params: [condition] }) {
|
|
984
|
+
return condition;
|
|
985
|
+
}
|
|
986
|
+
});
|
|
987
|
+
var requiredUnless = createRule({
|
|
988
|
+
type: "required",
|
|
989
|
+
validator(value, condition) {
|
|
990
|
+
if (!condition) {
|
|
991
|
+
return isFilled(value);
|
|
992
|
+
}
|
|
993
|
+
return true;
|
|
994
|
+
},
|
|
995
|
+
message: "This field is required",
|
|
996
|
+
active({ $params: [condition] }) {
|
|
997
|
+
return !condition;
|
|
998
|
+
}
|
|
999
|
+
});
|
|
1000
|
+
var sameAs = createRule({
|
|
1001
|
+
type: "sameAs",
|
|
1002
|
+
validator(value, target, otherName) {
|
|
1003
|
+
if (isEmpty(value)) {
|
|
1004
|
+
return true;
|
|
1005
|
+
}
|
|
1006
|
+
return value === target;
|
|
1007
|
+
},
|
|
1008
|
+
message({ $params: [_, otherName = "other"] }) {
|
|
1009
|
+
return `The value must be equal to the ${otherName} value`;
|
|
1010
|
+
}
|
|
1011
|
+
});
|
|
1012
|
+
var startsWith = createRule({
|
|
1013
|
+
type: "startsWith",
|
|
1014
|
+
validator(value, part) {
|
|
1015
|
+
if (isFilled(value) && isFilled(part)) {
|
|
1016
|
+
return value.startsWith(part);
|
|
1017
|
+
}
|
|
1018
|
+
return true;
|
|
1019
|
+
},
|
|
1020
|
+
message({ $params: [part] }) {
|
|
1021
|
+
return `The value must end with ${part}`;
|
|
1022
|
+
}
|
|
1023
|
+
});
|
|
1024
|
+
var string = createRule({
|
|
1025
|
+
type: "string",
|
|
1026
|
+
validator: (value) => {
|
|
1027
|
+
if (isFilled(value)) {
|
|
1028
|
+
return typeof value === "string";
|
|
1029
|
+
}
|
|
1030
|
+
return true;
|
|
1031
|
+
},
|
|
1032
|
+
message: "The value must be a string"
|
|
1033
|
+
});
|
|
981
1034
|
|
|
982
1035
|
// src/rules/type.ts
|
|
983
1036
|
function type() {
|
|
984
1037
|
return () => true;
|
|
985
1038
|
}
|
|
1039
|
+
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;
|
|
1040
|
+
var url = createRule({
|
|
1041
|
+
type: "url",
|
|
1042
|
+
validator(value) {
|
|
1043
|
+
if (isEmpty(value)) {
|
|
1044
|
+
return true;
|
|
1045
|
+
}
|
|
1046
|
+
return matchRegex(value, urlRegex);
|
|
1047
|
+
},
|
|
1048
|
+
message: "The value is not a valid URL address"
|
|
1049
|
+
});
|
|
986
1050
|
|
|
987
|
-
export { alpha, alphaNum, and, applyIf, between, checked, contains, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, getSize, integer, ipv4Address, isDate, isEmpty, isFilled, isNumber, literal, macAddress, matchRegex, maxLength, maxValue, minLength, minValue, nativeEnum, not, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, toDate, toNumber, type, url, withAsync, withMessage, withParams, withTooltip };
|
|
1051
|
+
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 };
|