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