@regle/rules 1.15.2 → 1.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/regle-rules.d.ts +153 -18
- package/dist/regle-rules.js +263 -44
- package/dist/regle-rules.min.js +7 -1
- package/package.json +11 -11
package/dist/regle-rules.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @regle/rules v1.16.0
|
|
3
|
+
* (c) 2025 Victor Garcia
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
1
7
|
import { CommonAlphaOptions, CommonComparisonOptions, ExtendedRulesDeclarations, FormRuleDeclaration, InlineRuleDeclaration, Maybe, MaybeInput, MaybeReadonly, NonEmptyTuple, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionWithMetadataProcessor, RegleRuleMetadataConsumer, RegleRuleMetadataDefinition, RegleRuleWithParamsDefinition, UnwrapRegleUniversalParams } from "@regle/core";
|
|
2
8
|
import { ComputedRef, MaybeRef, MaybeRefOrGetter, Ref } from "vue";
|
|
3
9
|
import { EmptyObject } from "type-fest";
|
|
@@ -27,10 +33,10 @@ import { EmptyObject } from "type-fest";
|
|
|
27
33
|
*
|
|
28
34
|
* @see {@link https://reglejs.dev/core-concepts/rules/rule-wrappers#withmessage Documentation}
|
|
29
35
|
*/
|
|
30
|
-
declare function withMessage<TValue$1 extends any, TParams$1 extends any[], TReturn
|
|
31
|
-
declare function withMessage<TValue$1 extends any, TParams$1 extends unknown[], TReturn
|
|
32
|
-
declare function withMessage<TValue$1 extends any, TParams$1 extends any[], TReturn
|
|
33
|
-
declare function withMessage<TValue$1 extends any, TParams$1 extends any[], TReturn
|
|
36
|
+
declare function withMessage<TValue$1 extends any, TParams$1 extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TMetadata$1 extends RegleRuleMetadataDefinition = (TReturn extends Promise<infer M> ? M : TReturn), TAsync$1 extends boolean = (TReturn extends Promise<any> ? true : false)>(rule: RegleRuleWithParamsDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>, newMessage: RegleRuleDefinitionWithMetadataProcessor<TValue$1, RegleRuleMetadataConsumer<TValue$1, TParams$1, TMetadata$1>, string | string[]>): RegleRuleWithParamsDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>;
|
|
37
|
+
declare function withMessage<TValue$1 extends any, TParams$1 extends unknown[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TMetadata$1 extends RegleRuleMetadataDefinition = (TReturn extends Promise<infer M> ? M : TReturn), TAsync$1 extends boolean = (TReturn extends Promise<any> ? true : false)>(rule: RegleRuleDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>, newMessage: RegleRuleDefinitionWithMetadataProcessor<TValue$1, RegleRuleMetadataConsumer<TValue$1, TParams$1, TMetadata$1>, string | string[]>): RegleRuleDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>;
|
|
38
|
+
declare function withMessage<TValue$1 extends any, TParams$1 extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TAsync$1 extends boolean = (TReturn extends Promise<any> ? true : false)>(rule: InlineRuleDeclaration<TValue$1, TParams$1, TReturn>, newMessage: RegleRuleDefinitionWithMetadataProcessor<TValue$1, RegleRuleMetadataConsumer<TValue$1, TParams$1, TReturn extends Promise<infer M> ? M : TReturn>, string | string[]>): RegleRuleDefinition<TValue$1, TParams$1, TAsync$1, TReturn extends Promise<infer M> ? M : TReturn>;
|
|
39
|
+
declare function withMessage<TValue$1 extends any, TParams$1 extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TMetadata$1 extends RegleRuleMetadataDefinition = (TReturn extends Promise<infer M> ? M : TReturn), TAsync$1 extends boolean = (TReturn extends Promise<any> ? true : false)>(rule: (...args: any[]) => RegleRuleDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>, newMessage: RegleRuleDefinitionWithMetadataProcessor<TValue$1, RegleRuleMetadataConsumer<TValue$1, TParams$1, TMetadata$1>, string | string[]>): RegleRuleWithParamsDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>;
|
|
34
40
|
/**
|
|
35
41
|
* The `withTooltip` wrapper allows you to display additional messages for your field that aren't necessarily errors.
|
|
36
42
|
* Tooltips are aggregated and accessible via `$tooltips` property.
|
|
@@ -57,9 +63,9 @@ declare function withMessage<TValue$1 extends any, TParams$1 extends any[], TRet
|
|
|
57
63
|
*
|
|
58
64
|
* @see {@link https://reglejs.dev/core-concepts/rules/rule-wrappers#withtooltip Documentation}
|
|
59
65
|
*/
|
|
60
|
-
declare function withTooltip<TValue$1 extends any, TParams$1 extends any[], TReturn
|
|
61
|
-
declare function withTooltip<TValue$1 extends any, TParams$1 extends any[], TReturn
|
|
62
|
-
declare function withTooltip<TValue$1 extends any, TParams$1 extends any[], TReturn
|
|
66
|
+
declare function withTooltip<TValue$1 extends any, TParams$1 extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TMetadata$1 extends RegleRuleMetadataDefinition = (TReturn extends Promise<infer M> ? M : TReturn), TAsync$1 extends boolean = (TReturn extends Promise<any> ? true : false)>(rule: RegleRuleWithParamsDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>, newTooltip: RegleRuleDefinitionWithMetadataProcessor<TValue$1, RegleRuleMetadataConsumer<TValue$1, TParams$1, TMetadata$1>, string | string[]>): RegleRuleWithParamsDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>;
|
|
67
|
+
declare function withTooltip<TValue$1 extends any, TParams$1 extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TMetadata$1 extends RegleRuleMetadataDefinition = (TReturn extends Promise<infer M> ? M : TReturn), TAsync$1 extends boolean = (TReturn extends Promise<any> ? true : false)>(rule: RegleRuleDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>, newTooltip: RegleRuleDefinitionWithMetadataProcessor<TValue$1, RegleRuleMetadataConsumer<TValue$1, TParams$1, TMetadata$1>, string | string[]>): RegleRuleDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>;
|
|
68
|
+
declare function withTooltip<TValue$1 extends any, TParams$1 extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TAsync$1 extends boolean = (TReturn extends Promise<any> ? true : false)>(rule: InlineRuleDeclaration<TValue$1, TParams$1, TReturn>, newTooltip: RegleRuleDefinitionWithMetadataProcessor<TValue$1, RegleRuleMetadataConsumer<TValue$1, TParams$1, TReturn extends Promise<infer M> ? M : TReturn>, string | string[]>): RegleRuleDefinition<TValue$1, TParams$1, TAsync$1, TReturn extends Promise<infer M> ? M : TReturn>;
|
|
63
69
|
/**
|
|
64
70
|
* `withAsync` works like `withParams`, but is specifically designed for async rules that depend on external values.
|
|
65
71
|
*
|
|
@@ -83,7 +89,7 @@ declare function withTooltip<TValue$1 extends any, TParams$1 extends any[], TRet
|
|
|
83
89
|
*
|
|
84
90
|
* @see {@link https://reglejs.dev/core-concepts/rules/rule-wrappers#withasync Documentation}
|
|
85
91
|
*/
|
|
86
|
-
declare function withAsync<TValue$1, TParams$1 extends (Ref<unknown> | (() => unknown))[] = [], TReturn
|
|
92
|
+
declare function withAsync<TValue$1, TParams$1 extends (Ref<unknown> | (() => unknown))[] = [], TReturn extends Promise<RegleRuleMetadataDefinition> = Promise<RegleRuleMetadataDefinition>, TMetadata$1 extends RegleRuleMetadataDefinition = (TReturn extends Promise<infer M> ? M : TReturn)>(rule: InlineRuleDeclaration<TValue$1, TParams$1, TReturn>, depsArray?: [...TParams$1]): RegleRuleDefinition<TValue$1, UnwrapRegleUniversalParams<TParams$1>, true, TMetadata$1>;
|
|
87
93
|
declare function withAsync<TValue$1 extends any, TParams$1 extends any[], TMetadata$1 extends RegleRuleMetadataDefinition>(rule: RegleRuleWithParamsDefinition<TValue$1, TParams$1, true, TMetadata$1>, depsArray?: [...TParams$1]): RegleRuleWithParamsDefinition<TValue$1, TParams$1, true, TMetadata$1>;
|
|
88
94
|
/**
|
|
89
95
|
* The `withParams` wrapper allows your rule to depend on external parameters,
|
|
@@ -112,8 +118,8 @@ declare function withAsync<TValue$1 extends any, TParams$1 extends any[], TMetad
|
|
|
112
118
|
*
|
|
113
119
|
* @see {@link https://reglejs.dev/core-concepts/rules/rule-wrappers#withparams Documentation}
|
|
114
120
|
*/
|
|
115
|
-
declare function withParams<TValue$1, TParams$1 extends (Ref<unknown> | (() => unknown))[] = [], TReturn
|
|
116
|
-
declare function withParams<TValue$1 extends any, TParams$1 extends any[], TReturn
|
|
121
|
+
declare function withParams<TValue$1, TParams$1 extends (Ref<unknown> | (() => unknown))[] = [], TReturn extends RegleRuleMetadataDefinition = RegleRuleMetadataDefinition, TMetadata$1 extends RegleRuleMetadataDefinition = (TReturn extends Promise<infer M> ? M : TReturn), TAsync$1 extends boolean = (TReturn extends Promise<any> ? true : false)>(rule: InlineRuleDeclaration<TValue$1, TParams$1, TReturn> | RegleRuleDefinition<TValue$1, any[], TAsync$1, TMetadata$1>, depsArray: [...TParams$1]): RegleRuleDefinition<TValue$1, UnwrapRegleUniversalParams<TParams$1>, TAsync$1, TMetadata$1>;
|
|
122
|
+
declare function withParams<TValue$1 extends any, TParams$1 extends any[], TReturn extends RegleRuleMetadataDefinition = RegleRuleMetadataDefinition, TMetadata$1 extends RegleRuleMetadataDefinition = (TReturn extends Promise<infer M> ? M : TReturn), TAsync$1 extends boolean = (TReturn extends Promise<any> ? true : false)>(rule: RegleRuleWithParamsDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>, depsArray: [...TParams$1]): RegleRuleWithParamsDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>;
|
|
117
123
|
/**
|
|
118
124
|
* The `applyIf` operator is similar to `requiredIf`, but it can be used with **any rule**.
|
|
119
125
|
* It simplifies conditional rule declarations.
|
|
@@ -443,7 +449,7 @@ declare function or<TRules extends [FormRuleDeclaration<any, any>, ...FormRuleDe
|
|
|
443
449
|
*
|
|
444
450
|
* @see {@link https://reglejs.dev/core-concepts/rules/rules-operators#not Documentation}
|
|
445
451
|
*/
|
|
446
|
-
declare function not<TValue$1, TParams$1 extends any[] = [], TReturn
|
|
452
|
+
declare function not<TValue$1, TParams$1 extends any[] = [], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition> = RegleRuleMetadataDefinition, TMetadata$1 extends RegleRuleMetadataDefinition = (TReturn extends Promise<infer M> ? M : TReturn), TAsync$1 extends boolean = (TReturn extends Promise<any> ? true : false)>(rule: RegleRuleDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1> | InlineRuleDeclaration<TValue$1, TParams$1, TReturn>, message?: RegleRuleDefinitionWithMetadataProcessor<TValue$1, RegleRuleMetadataConsumer<TValue$1, TParams$1, TMetadata$1>, string | string[]>): RegleRuleDefinition<TValue$1, TParams$1, TAsync$1, TMetadata$1>;
|
|
447
453
|
/**
|
|
448
454
|
* The `assignIf` is a shorthand for conditional destructuring assignment.
|
|
449
455
|
* It allows applying **multiple rules** to a field conditionally.
|
|
@@ -471,6 +477,7 @@ declare function assignIf<TValue$1 extends unknown = any, TCustomRules extends P
|
|
|
471
477
|
* Allows only alphabetic characters.
|
|
472
478
|
*
|
|
473
479
|
* @param options - Optional configuration for alpha validation
|
|
480
|
+
* @param options.allowSymbols - Optional flag to allow symbols (_) in the alpha string
|
|
474
481
|
*
|
|
475
482
|
* @example
|
|
476
483
|
* ```ts
|
|
@@ -492,6 +499,7 @@ declare const alpha: RegleRuleWithParamsDefinition<string, [options?: CommonAlph
|
|
|
492
499
|
* Allows only alphanumeric characters.
|
|
493
500
|
*
|
|
494
501
|
* @param options - Optional configuration for alphanumeric validation
|
|
502
|
+
* @param options.allowSymbols - Optional flag to allow symbols (_) in the alphanumeric string
|
|
495
503
|
*
|
|
496
504
|
* @example
|
|
497
505
|
* ```ts
|
|
@@ -515,6 +523,7 @@ declare const alphaNum: RegleRuleWithParamsDefinition<string | number, [options?
|
|
|
515
523
|
* @param min - The minimum limit
|
|
516
524
|
* @param max - The maximum limit
|
|
517
525
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }` for exclusive bounds)
|
|
526
|
+
* @param options.allowEqual - Optional flag to allow equal bounds
|
|
518
527
|
*
|
|
519
528
|
* @example
|
|
520
529
|
* ```ts
|
|
@@ -615,6 +624,7 @@ declare const date: RegleRuleDefinition<unknown, [], false, boolean, MaybeInput<
|
|
|
615
624
|
*
|
|
616
625
|
* @param after - The date to compare to (can be a `Date`, string, ref, or getter)
|
|
617
626
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
627
|
+
* @param options.allowEqual - Optional flag to allow equal dates
|
|
618
628
|
*
|
|
619
629
|
* @example
|
|
620
630
|
* ```ts
|
|
@@ -643,6 +653,7 @@ declare const dateAfter: RegleRuleWithParamsDefinition<string | Date, [after: Ma
|
|
|
643
653
|
*
|
|
644
654
|
* @param before - The date to compare to (can be a `Date`, string, ref, or getter)
|
|
645
655
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
656
|
+
* @param options.allowEqual - Optional flag to allow equal dates
|
|
646
657
|
*
|
|
647
658
|
* @example
|
|
648
659
|
* ```ts
|
|
@@ -672,6 +683,7 @@ declare const dateBefore: RegleRuleWithParamsDefinition<string | Date, [before:
|
|
|
672
683
|
* @param before - The minimum date limit
|
|
673
684
|
* @param after - The maximum date limit
|
|
674
685
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
686
|
+
* @param options.allowEqual - Optional flag to allow equal dates
|
|
675
687
|
*
|
|
676
688
|
* @example
|
|
677
689
|
* ```ts
|
|
@@ -719,6 +731,21 @@ declare const decimal: RegleRuleDefinition<string | number, [], false, boolean,
|
|
|
719
731
|
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#email Documentation}
|
|
720
732
|
*/
|
|
721
733
|
declare const email: RegleRuleDefinition<string, [], false, boolean, MaybeInput<string>>;
|
|
734
|
+
/**
|
|
735
|
+
* Validates if the value is a valid emoji.
|
|
736
|
+
*
|
|
737
|
+
* @example
|
|
738
|
+
* ```ts
|
|
739
|
+
* import { emoji } from '@regle/rules';
|
|
740
|
+
*
|
|
741
|
+
* const { r$ } = useRegle({ emoji: '' }, {
|
|
742
|
+
* emoji: { emoji },
|
|
743
|
+
* })
|
|
744
|
+
* ```
|
|
745
|
+
*
|
|
746
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#emoji Documentation}
|
|
747
|
+
*/
|
|
748
|
+
declare const emoji: RegleRuleDefinition<string, [], false, boolean, unknown, string>;
|
|
722
749
|
/**
|
|
723
750
|
* Checks if the string ends with the specified substring.
|
|
724
751
|
*
|
|
@@ -736,6 +763,31 @@ declare const email: RegleRuleDefinition<string, [], false, boolean, MaybeInput<
|
|
|
736
763
|
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#endswith Documentation}
|
|
737
764
|
*/
|
|
738
765
|
declare const endsWith: RegleRuleWithParamsDefinition<string, [part: MaybeInput<string>], false, boolean, MaybeInput<string>>;
|
|
766
|
+
/**
|
|
767
|
+
* Requires the input value to have a strict specified length. Works with arrays, objects and strings.
|
|
768
|
+
*
|
|
769
|
+
* @param count - The exact required length
|
|
770
|
+
*
|
|
771
|
+
* @example
|
|
772
|
+
* ```ts
|
|
773
|
+
* import { exactDigits } from '@regle/rules';
|
|
774
|
+
*
|
|
775
|
+
* const exactValue = ref(6);
|
|
776
|
+
*
|
|
777
|
+
* const { r$ } = useRegle({ digits: '' }, {
|
|
778
|
+
* digits: {
|
|
779
|
+
* exactDigits: exactDigits(6),
|
|
780
|
+
* // or with reactive value
|
|
781
|
+
* exactDigits: exactDigits(exactValue),
|
|
782
|
+
* // or with getter
|
|
783
|
+
* exactDigits: exactDigits(() => exactValue.value)
|
|
784
|
+
* },
|
|
785
|
+
* })
|
|
786
|
+
* ```
|
|
787
|
+
*
|
|
788
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#exactdigits Documentation}
|
|
789
|
+
*/
|
|
790
|
+
declare const exactDigits: RegleRuleWithParamsDefinition<string | number, [count: number], false, boolean>;
|
|
739
791
|
/**
|
|
740
792
|
* Requires the input value to have a strict specified length. Works with arrays, objects and strings.
|
|
741
793
|
*
|
|
@@ -809,6 +861,8 @@ declare const file: RegleRuleDefinition<unknown, [], false, boolean, MaybeInput<
|
|
|
809
861
|
/**
|
|
810
862
|
* Requires a value to be a file with a specific type.
|
|
811
863
|
*
|
|
864
|
+
* @param accept - The allowed file types
|
|
865
|
+
*
|
|
812
866
|
* @example
|
|
813
867
|
* ```ts
|
|
814
868
|
* import { type InferInput } from '@regle/core';
|
|
@@ -838,6 +892,64 @@ declare const fileType: RegleRuleWithParamsDefinition<File, [accept: readonly st
|
|
|
838
892
|
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#hexadecimal Documentation}
|
|
839
893
|
*/
|
|
840
894
|
declare const hexadecimal: RegleRuleDefinition<string, [], false, boolean, MaybeInput<string>>;
|
|
895
|
+
/**
|
|
896
|
+
* Validates hostnames.
|
|
897
|
+
*
|
|
898
|
+
* @example
|
|
899
|
+
* ```ts
|
|
900
|
+
* import { hostname } from '@regle/rules';
|
|
901
|
+
*
|
|
902
|
+
* const { r$ } = useRegle({ siteHost: '' }, {
|
|
903
|
+
* siteHost: { hostname },
|
|
904
|
+
* })
|
|
905
|
+
* ```
|
|
906
|
+
*
|
|
907
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#hostname Documentation}
|
|
908
|
+
*/
|
|
909
|
+
declare const hostname: RegleRuleDefinition<string, [], false, boolean, unknown, string>;
|
|
910
|
+
interface UrlOptions {
|
|
911
|
+
protocol?: RegExp;
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Validates URLs.
|
|
915
|
+
*
|
|
916
|
+
* @param options - Optional configuration for url validation
|
|
917
|
+
* @param options.protocol - Optional regex for validating the protocol
|
|
918
|
+
*
|
|
919
|
+
* @example
|
|
920
|
+
* ```ts
|
|
921
|
+
* import { url } from '@regle/rules';
|
|
922
|
+
*
|
|
923
|
+
* const { r$ } = useRegle({ bestUrl: '' }, {
|
|
924
|
+
* bestUrl: { url },
|
|
925
|
+
* // or with custom protocol validation
|
|
926
|
+
* bestUrl: { url: url({ protocol: /^https?$/ }) },
|
|
927
|
+
* })
|
|
928
|
+
* ```
|
|
929
|
+
*
|
|
930
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#url Documentation}
|
|
931
|
+
*/
|
|
932
|
+
declare const url: RegleRuleWithParamsDefinition<string, [options?: UrlOptions | undefined], false, boolean, unknown, string>;
|
|
933
|
+
/**
|
|
934
|
+
* Validates HTTP URLs.
|
|
935
|
+
*
|
|
936
|
+
* @param options - Optional configuration for http url validation
|
|
937
|
+
* @param options.protocol - Optional regex for validating the protocol
|
|
938
|
+
*
|
|
939
|
+
* @example
|
|
940
|
+
* ```ts
|
|
941
|
+
* import { httpUrl } from '@regle/rules';
|
|
942
|
+
*
|
|
943
|
+
* const { r$ } = useRegle({ bestUrl: '' }, {
|
|
944
|
+
* bestUrl: { httpUrl },
|
|
945
|
+
* // or to force https protocol
|
|
946
|
+
* bestUrl: { httpUrl: httpUrl({ protocol: /^https$/ }) },
|
|
947
|
+
* })
|
|
948
|
+
* ```
|
|
949
|
+
*
|
|
950
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#httpurl Documentation}
|
|
951
|
+
*/
|
|
952
|
+
declare const httpUrl: RegleRuleWithParamsDefinition<string, [options?: UrlOptions | undefined], false, boolean, unknown, string>;
|
|
841
953
|
/**
|
|
842
954
|
* Allows only integers (positive and negative).
|
|
843
955
|
*
|
|
@@ -885,6 +997,21 @@ declare const ipv4Address: RegleRuleDefinition<string, [], false, boolean, Maybe
|
|
|
885
997
|
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#literal Documentation}
|
|
886
998
|
*/
|
|
887
999
|
declare function literal<const TValue$1 extends string | number>(literal: MaybeRefOrGetter<TValue$1>): RegleRuleDefinition<TValue$1, [literal: TValue$1], false, boolean, MaybeInput<TValue$1>, string | number>;
|
|
1000
|
+
/**
|
|
1001
|
+
* Validates lowercase strings.
|
|
1002
|
+
*
|
|
1003
|
+
* @example
|
|
1004
|
+
* ```ts
|
|
1005
|
+
* import { lowercase } from '@regle/rules';
|
|
1006
|
+
*
|
|
1007
|
+
* const { r$ } = useRegle({ name: '' }, {
|
|
1008
|
+
* name: { lowercase },
|
|
1009
|
+
* })
|
|
1010
|
+
* ```
|
|
1011
|
+
*
|
|
1012
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#lowercase Documentation}
|
|
1013
|
+
*/
|
|
1014
|
+
declare const lowercase: RegleRuleDefinition<string, [], false, boolean, unknown, string>;
|
|
888
1015
|
/**
|
|
889
1016
|
* Validates MAC addresses. Call as a function to specify a custom separator (e.g., `':'` or an empty string for `00ff1122334455`).
|
|
890
1017
|
*
|
|
@@ -909,6 +1036,8 @@ declare const macAddress: RegleRuleWithParamsDefinition<string, [separator?: str
|
|
|
909
1036
|
/**
|
|
910
1037
|
* Requires a value to be a file with a maximum size.
|
|
911
1038
|
*
|
|
1039
|
+
* @param maxSize - The maximum size of the file
|
|
1040
|
+
*
|
|
912
1041
|
* @example
|
|
913
1042
|
* ```ts
|
|
914
1043
|
* import { type InferInput } from '@regle/core';
|
|
@@ -931,6 +1060,7 @@ declare const maxFileSize: RegleRuleWithParamsDefinition<File, [maxSize: number]
|
|
|
931
1060
|
*
|
|
932
1061
|
* @param max - The maximum length
|
|
933
1062
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
1063
|
+
* @param options.allowEqual - Optional flag to allow equal length
|
|
934
1064
|
*
|
|
935
1065
|
* @example
|
|
936
1066
|
* ```ts
|
|
@@ -957,6 +1087,7 @@ declare const maxLength: RegleRuleWithParamsDefinition<string | any[] | Record<P
|
|
|
957
1087
|
*
|
|
958
1088
|
* @param max - The maximum value
|
|
959
1089
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
1090
|
+
* @param options.allowEqual - Optional flag to allow equal value
|
|
960
1091
|
*
|
|
961
1092
|
* @example
|
|
962
1093
|
* ```ts
|
|
@@ -981,6 +1112,8 @@ declare const maxValue: RegleRuleWithParamsDefinition<number | string, [max: num
|
|
|
981
1112
|
/**
|
|
982
1113
|
* Requires a value to be a file with a minimum size.
|
|
983
1114
|
*
|
|
1115
|
+
* @param minSize - The minimum size of the file
|
|
1116
|
+
*
|
|
984
1117
|
* @example
|
|
985
1118
|
* ```ts
|
|
986
1119
|
* import { type InferInput } from '@regle/core';
|
|
@@ -1003,6 +1136,7 @@ declare const minFileSize: RegleRuleWithParamsDefinition<File, [minSize: number]
|
|
|
1003
1136
|
*
|
|
1004
1137
|
* @param min - The minimum length
|
|
1005
1138
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
1139
|
+
* @param options.allowEqual - Optional flag to allow equal length
|
|
1006
1140
|
*
|
|
1007
1141
|
* @example
|
|
1008
1142
|
* ```ts
|
|
@@ -1029,6 +1163,7 @@ declare const minLength: RegleRuleWithParamsDefinition<string | any[] | Record<P
|
|
|
1029
1163
|
*
|
|
1030
1164
|
* @param min - The minimum value
|
|
1031
1165
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
1166
|
+
* @param options.allowEqual - Optional flag to allow equal value
|
|
1032
1167
|
*
|
|
1033
1168
|
* @example
|
|
1034
1169
|
* ```ts
|
|
@@ -1309,18 +1444,18 @@ declare const string: RegleRuleDefinition<unknown, [], false, boolean, MaybeInpu
|
|
|
1309
1444
|
*/
|
|
1310
1445
|
declare function type<T>(): RegleRuleDefinition<unknown, [], false, boolean, MaybeInput<T>>;
|
|
1311
1446
|
/**
|
|
1312
|
-
* Validates
|
|
1447
|
+
* Validates uppercase strings.
|
|
1313
1448
|
*
|
|
1314
1449
|
* @example
|
|
1315
1450
|
* ```ts
|
|
1316
|
-
* import {
|
|
1451
|
+
* import { uppercase } from '@regle/rules';
|
|
1317
1452
|
*
|
|
1318
|
-
* const { r$ } = useRegle({
|
|
1319
|
-
*
|
|
1453
|
+
* const { r$ } = useRegle({ name: '' }, {
|
|
1454
|
+
* name: { uppercase },
|
|
1320
1455
|
* })
|
|
1321
1456
|
* ```
|
|
1322
1457
|
*
|
|
1323
|
-
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#
|
|
1458
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#uppercase Documentation}
|
|
1324
1459
|
*/
|
|
1325
|
-
declare const
|
|
1326
|
-
export { EnumLike, alpha, alphaNum, and, applyIf, assignIf, between, boolean, checked, contains, date, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, file, fileType, getSize, hexadecimal, integer, ipv4Address, isDate, isEmpty, isFilled, isNumber, literal, macAddress, matchRegex, maxFileSize, maxLength, maxValue, minFileSize, minLength, minValue, nativeEnum, not, number, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, string, toDate, toNumber, type, url, withAsync, withMessage, withParams, withTooltip };
|
|
1460
|
+
declare const uppercase: RegleRuleDefinition<string, [], false, boolean, unknown, string>;
|
|
1461
|
+
export { EnumLike, UrlOptions, alpha, alphaNum, and, applyIf, assignIf, between, boolean, checked, contains, date, dateAfter, dateBefore, dateBetween, decimal, email, emoji, endsWith, exactDigits, exactLength, exactValue, file, fileType, getSize, hexadecimal, hostname, httpUrl, integer, ipv4Address, isDate, isEmpty, isFilled, isNumber, literal, lowercase, macAddress, matchRegex, maxFileSize, maxLength, maxValue, minFileSize, minLength, minValue, nativeEnum, not, number, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, string, toDate, toNumber, type, uppercase, url, withAsync, withMessage, withParams, withTooltip };
|
package/dist/regle-rules.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @regle/rules v1.16.0
|
|
3
|
+
* (c) 2025 Victor Garcia
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
1
7
|
import { InternalRuleType, createRule, unwrapRuleParameters } from "@regle/core";
|
|
2
8
|
import { computed, toValue, unref } from "vue";
|
|
3
9
|
|
|
@@ -246,18 +252,14 @@ function isObject(obj) {
|
|
|
246
252
|
*/
|
|
247
253
|
function isDate(value) {
|
|
248
254
|
if (isEmpty(value)) return false;
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
possibleDate = date$1;
|
|
256
|
-
}
|
|
257
|
-
return !!possibleDate;
|
|
258
|
-
} catch {
|
|
259
|
-
return false;
|
|
255
|
+
let possibleDate = null;
|
|
256
|
+
if (value instanceof Date) possibleDate = value;
|
|
257
|
+
else if (typeof value === "string") {
|
|
258
|
+
const date$1 = new Date(value);
|
|
259
|
+
if (date$1.toString() === "Invalid Date") return false;
|
|
260
|
+
possibleDate = date$1;
|
|
260
261
|
}
|
|
262
|
+
return !!possibleDate;
|
|
261
263
|
}
|
|
262
264
|
|
|
263
265
|
/**
|
|
@@ -736,12 +738,47 @@ function assignIf(_condition, rules, otherwiseRules) {
|
|
|
736
738
|
});
|
|
737
739
|
}
|
|
738
740
|
|
|
739
|
-
|
|
740
|
-
const
|
|
741
|
+
/** Regex for validating url protocols */
|
|
742
|
+
const DEFAULT_PROTOCOL_REGEX = /^https?$/;
|
|
743
|
+
/** Regex for validating url hostnames */
|
|
744
|
+
const HOSTNAME_REGEX = /^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/;
|
|
745
|
+
/**
|
|
746
|
+
* Regex taken from {@link https://gist.github.com/dperini/729294}
|
|
747
|
+
*/
|
|
748
|
+
const URL_REGEX = /^(?:(?:(?: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;
|
|
749
|
+
/** Regex for validating emoji */
|
|
750
|
+
const _EMOJI_REGEX = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
751
|
+
function EMOJI_REGEX() {
|
|
752
|
+
return new RegExp(_EMOJI_REGEX, "u");
|
|
753
|
+
}
|
|
754
|
+
/** Regex for validating lowercase strings */
|
|
755
|
+
const LOWERCASE_REGEX = /^[^A-Z]*$/;
|
|
756
|
+
/** Regex for validating uppercase strings */
|
|
757
|
+
const UPPERCASE_REGEX = /^[^a-z]*$/;
|
|
758
|
+
/** Regex for validating hexadecimal strings */
|
|
759
|
+
const HEX_REGEX = /^[0-9a-fA-F]*$/;
|
|
760
|
+
/** Regex for validating alpha strings */
|
|
761
|
+
const ALPHA_REGEX = /^[a-zA-Z]*$/;
|
|
762
|
+
/** Regex for validating alpha-symbol strings */
|
|
763
|
+
const ALPHA_SYMBOL_REGEX = /^[\w.]+$/;
|
|
764
|
+
/** Regex for validating alpha-numeric strings */
|
|
765
|
+
const ALPHA_NUM_REGEX = /^[a-zA-Z0-9]*$/;
|
|
766
|
+
/** Regex for validating alpha-numeric-symbol strings */
|
|
767
|
+
const ALPHA_NUM_SYMBOL_REGEX = /^[a-zA-Z0-9_]*$/;
|
|
768
|
+
/** Regex for validating decimal strings */
|
|
769
|
+
const DECIMAL_REGEX = /^[-]?\d*(\.\d+)?$/;
|
|
770
|
+
/** Regex for validating email addresses */
|
|
771
|
+
const EMAIL_REGEX = /^(?:[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;
|
|
772
|
+
/** Regex for validating integer strings */
|
|
773
|
+
const INTEGER_REGEX = /(^[0-9]*$)|(^-[0-9]+$)/;
|
|
774
|
+
/** Regex for validating numeric strings */
|
|
775
|
+
const NUMERIC_REGEX = /^\d*(\.\d+)?$/;
|
|
776
|
+
|
|
741
777
|
/**
|
|
742
778
|
* Allows only alphabetic characters.
|
|
743
779
|
*
|
|
744
780
|
* @param options - Optional configuration for alpha validation
|
|
781
|
+
* @param options.allowSymbols - Optional flag to allow symbols (_) in the alpha string
|
|
745
782
|
*
|
|
746
783
|
* @example
|
|
747
784
|
* ```ts
|
|
@@ -762,18 +799,17 @@ const alpha = createRule({
|
|
|
762
799
|
type: "alpha",
|
|
763
800
|
validator(value, options) {
|
|
764
801
|
if (isEmpty(value)) return true;
|
|
765
|
-
if (options?.allowSymbols) return matchRegex(value,
|
|
766
|
-
return matchRegex(value,
|
|
802
|
+
if (options?.allowSymbols) return matchRegex(value, ALPHA_SYMBOL_REGEX);
|
|
803
|
+
return matchRegex(value, ALPHA_REGEX);
|
|
767
804
|
},
|
|
768
805
|
message: "The value is not alphabetical"
|
|
769
806
|
});
|
|
770
807
|
|
|
771
|
-
const alphaNumRegex = /^[a-zA-Z0-9]*$/;
|
|
772
|
-
const alphaNumSymbolRegex = /^[a-zA-Z0-9_]*$/;
|
|
773
808
|
/**
|
|
774
809
|
* Allows only alphanumeric characters.
|
|
775
810
|
*
|
|
776
811
|
* @param options - Optional configuration for alphanumeric validation
|
|
812
|
+
* @param options.allowSymbols - Optional flag to allow symbols (_) in the alphanumeric string
|
|
777
813
|
*
|
|
778
814
|
* @example
|
|
779
815
|
* ```ts
|
|
@@ -794,8 +830,8 @@ const alphaNum = createRule({
|
|
|
794
830
|
type: "alphaNum",
|
|
795
831
|
validator(value, options) {
|
|
796
832
|
if (isEmpty(value)) return true;
|
|
797
|
-
if (options?.allowSymbols) return matchRegex(value,
|
|
798
|
-
return matchRegex(value,
|
|
833
|
+
if (options?.allowSymbols) return matchRegex(value, ALPHA_NUM_SYMBOL_REGEX);
|
|
834
|
+
return matchRegex(value, ALPHA_NUM_REGEX);
|
|
799
835
|
},
|
|
800
836
|
message: "The value must be alpha-numeric"
|
|
801
837
|
});
|
|
@@ -806,6 +842,7 @@ const alphaNum = createRule({
|
|
|
806
842
|
* @param min - The minimum limit
|
|
807
843
|
* @param max - The maximum limit
|
|
808
844
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }` for exclusive bounds)
|
|
845
|
+
* @param options.allowEqual - Optional flag to allow equal bounds
|
|
809
846
|
*
|
|
810
847
|
* @example
|
|
811
848
|
* ```ts
|
|
@@ -968,6 +1005,7 @@ function formatLocaleDate(date$1) {
|
|
|
968
1005
|
*
|
|
969
1006
|
* @param after - The date to compare to (can be a `Date`, string, ref, or getter)
|
|
970
1007
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
1008
|
+
* @param options.allowEqual - Optional flag to allow equal dates
|
|
971
1009
|
*
|
|
972
1010
|
* @example
|
|
973
1011
|
* ```ts
|
|
@@ -1014,6 +1052,7 @@ const dateAfter = createRule({
|
|
|
1014
1052
|
*
|
|
1015
1053
|
* @param before - The date to compare to (can be a `Date`, string, ref, or getter)
|
|
1016
1054
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
1055
|
+
* @param options.allowEqual - Optional flag to allow equal dates
|
|
1017
1056
|
*
|
|
1018
1057
|
* @example
|
|
1019
1058
|
* ```ts
|
|
@@ -1061,6 +1100,7 @@ const dateBefore = createRule({
|
|
|
1061
1100
|
* @param before - The minimum date limit
|
|
1062
1101
|
* @param after - The maximum date limit
|
|
1063
1102
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
1103
|
+
* @param options.allowEqual - Optional flag to allow equal dates
|
|
1064
1104
|
*
|
|
1065
1105
|
* @example
|
|
1066
1106
|
* ```ts
|
|
@@ -1090,7 +1130,6 @@ const dateBetween = createRule({
|
|
|
1090
1130
|
}
|
|
1091
1131
|
});
|
|
1092
1132
|
|
|
1093
|
-
const decimalRegex = /^[-]?\d*(\.\d+)?$/;
|
|
1094
1133
|
/**
|
|
1095
1134
|
* Allows positive and negative decimal numbers.
|
|
1096
1135
|
*
|
|
@@ -1109,12 +1148,11 @@ const decimal = createRule({
|
|
|
1109
1148
|
type: "decimal",
|
|
1110
1149
|
validator(value) {
|
|
1111
1150
|
if (isEmpty(value)) return true;
|
|
1112
|
-
return matchRegex(value,
|
|
1151
|
+
return matchRegex(value, DECIMAL_REGEX);
|
|
1113
1152
|
},
|
|
1114
1153
|
message: "The value must be decimal"
|
|
1115
1154
|
});
|
|
1116
1155
|
|
|
1117
|
-
const 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;
|
|
1118
1156
|
/**
|
|
1119
1157
|
* Validates email addresses. Always verify on the server to ensure the address is real and not already in use.
|
|
1120
1158
|
*
|
|
@@ -1133,11 +1171,34 @@ const email = createRule({
|
|
|
1133
1171
|
type: "email",
|
|
1134
1172
|
validator(value) {
|
|
1135
1173
|
if (isEmpty(value)) return true;
|
|
1136
|
-
return matchRegex(value,
|
|
1174
|
+
return matchRegex(value, EMAIL_REGEX);
|
|
1137
1175
|
},
|
|
1138
1176
|
message: "The value must be an valid email address"
|
|
1139
1177
|
});
|
|
1140
1178
|
|
|
1179
|
+
/**
|
|
1180
|
+
* Validates if the value is a valid emoji.
|
|
1181
|
+
*
|
|
1182
|
+
* @example
|
|
1183
|
+
* ```ts
|
|
1184
|
+
* import { emoji } from '@regle/rules';
|
|
1185
|
+
*
|
|
1186
|
+
* const { r$ } = useRegle({ emoji: '' }, {
|
|
1187
|
+
* emoji: { emoji },
|
|
1188
|
+
* })
|
|
1189
|
+
* ```
|
|
1190
|
+
*
|
|
1191
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#emoji Documentation}
|
|
1192
|
+
*/
|
|
1193
|
+
const emoji = createRule({
|
|
1194
|
+
type: "emoji",
|
|
1195
|
+
validator(value) {
|
|
1196
|
+
if (isEmpty(value)) return true;
|
|
1197
|
+
return EMOJI_REGEX().test(value);
|
|
1198
|
+
},
|
|
1199
|
+
message: "The value should be a valid emoji"
|
|
1200
|
+
});
|
|
1201
|
+
|
|
1141
1202
|
/**
|
|
1142
1203
|
* Checks if the string ends with the specified substring.
|
|
1143
1204
|
*
|
|
@@ -1165,6 +1226,48 @@ const endsWith = createRule({
|
|
|
1165
1226
|
}
|
|
1166
1227
|
});
|
|
1167
1228
|
|
|
1229
|
+
/**
|
|
1230
|
+
* Requires the input value to have a strict specified length. Works with arrays, objects and strings.
|
|
1231
|
+
*
|
|
1232
|
+
* @param count - The exact required length
|
|
1233
|
+
*
|
|
1234
|
+
* @example
|
|
1235
|
+
* ```ts
|
|
1236
|
+
* import { exactDigits } from '@regle/rules';
|
|
1237
|
+
*
|
|
1238
|
+
* const exactValue = ref(6);
|
|
1239
|
+
*
|
|
1240
|
+
* const { r$ } = useRegle({ digits: '' }, {
|
|
1241
|
+
* digits: {
|
|
1242
|
+
* exactDigits: exactDigits(6),
|
|
1243
|
+
* // or with reactive value
|
|
1244
|
+
* exactDigits: exactDigits(exactValue),
|
|
1245
|
+
* // or with getter
|
|
1246
|
+
* exactDigits: exactDigits(() => exactValue.value)
|
|
1247
|
+
* },
|
|
1248
|
+
* })
|
|
1249
|
+
* ```
|
|
1250
|
+
*
|
|
1251
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#exactdigits Documentation}
|
|
1252
|
+
*/
|
|
1253
|
+
const exactDigits = createRule({
|
|
1254
|
+
type: "exactDigits",
|
|
1255
|
+
validator: (value, count) => {
|
|
1256
|
+
if (isFilled(value, false) && isFilled(count)) {
|
|
1257
|
+
if (isNumber(count)) {
|
|
1258
|
+
const digitsRegex = /* @__PURE__ */ new RegExp(`^\\d{${count}}$`);
|
|
1259
|
+
return matchRegex(value.toString(), digitsRegex);
|
|
1260
|
+
}
|
|
1261
|
+
console.warn(`[exactDigits] Parameter isn't a number, got parameter: ${count}`);
|
|
1262
|
+
return false;
|
|
1263
|
+
}
|
|
1264
|
+
return true;
|
|
1265
|
+
},
|
|
1266
|
+
message: ({ $params: [count] }) => {
|
|
1267
|
+
return `The value should have exactly ${count} digits`;
|
|
1268
|
+
}
|
|
1269
|
+
});
|
|
1270
|
+
|
|
1168
1271
|
/**
|
|
1169
1272
|
* Requires the input value to have a strict specified length. Works with arrays, objects and strings.
|
|
1170
1273
|
*
|
|
@@ -1194,7 +1297,7 @@ const exactLength = createRule({
|
|
|
1194
1297
|
validator: (value, count) => {
|
|
1195
1298
|
if (isFilled(value, false) && isFilled(count)) {
|
|
1196
1299
|
if (isNumber(count)) return getSize(value) === count;
|
|
1197
|
-
console.warn(`[
|
|
1300
|
+
console.warn(`[exactLength] Parameter isn't a number, got parameter: ${count}`);
|
|
1198
1301
|
return false;
|
|
1199
1302
|
}
|
|
1200
1303
|
return true;
|
|
@@ -1274,6 +1377,8 @@ const file = createRule({
|
|
|
1274
1377
|
/**
|
|
1275
1378
|
* Requires a value to be a file with a specific type.
|
|
1276
1379
|
*
|
|
1380
|
+
* @param accept - The allowed file types
|
|
1381
|
+
*
|
|
1277
1382
|
* @example
|
|
1278
1383
|
* ```ts
|
|
1279
1384
|
* import { type InferInput } from '@regle/core';
|
|
@@ -1301,7 +1406,6 @@ const fileType = createRule({
|
|
|
1301
1406
|
}
|
|
1302
1407
|
});
|
|
1303
1408
|
|
|
1304
|
-
const hexadecimalRegex = /^[a-fA-F0-9]*$/;
|
|
1305
1409
|
/**
|
|
1306
1410
|
* Validates hexadecimal values.
|
|
1307
1411
|
*
|
|
@@ -1320,12 +1424,101 @@ const hexadecimal = createRule({
|
|
|
1320
1424
|
type: "hexadecimal",
|
|
1321
1425
|
validator(value) {
|
|
1322
1426
|
if (isEmpty(value)) return true;
|
|
1323
|
-
return matchRegex(value,
|
|
1427
|
+
return matchRegex(value, HEX_REGEX);
|
|
1324
1428
|
},
|
|
1325
1429
|
message: "The value must be hexadecimal"
|
|
1326
1430
|
});
|
|
1327
1431
|
|
|
1328
|
-
|
|
1432
|
+
/**
|
|
1433
|
+
* Validates hostnames.
|
|
1434
|
+
*
|
|
1435
|
+
* @example
|
|
1436
|
+
* ```ts
|
|
1437
|
+
* import { hostname } from '@regle/rules';
|
|
1438
|
+
*
|
|
1439
|
+
* const { r$ } = useRegle({ siteHost: '' }, {
|
|
1440
|
+
* siteHost: { hostname },
|
|
1441
|
+
* })
|
|
1442
|
+
* ```
|
|
1443
|
+
*
|
|
1444
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#hostname Documentation}
|
|
1445
|
+
*/
|
|
1446
|
+
const hostname = createRule({
|
|
1447
|
+
type: "hostname",
|
|
1448
|
+
validator(value) {
|
|
1449
|
+
if (isEmpty(value)) return true;
|
|
1450
|
+
return matchRegex(value, HOSTNAME_REGEX);
|
|
1451
|
+
},
|
|
1452
|
+
message: "The value is not a valid hostname"
|
|
1453
|
+
});
|
|
1454
|
+
|
|
1455
|
+
/**
|
|
1456
|
+
* Validates URLs.
|
|
1457
|
+
*
|
|
1458
|
+
* @param options - Optional configuration for url validation
|
|
1459
|
+
* @param options.protocol - Optional regex for validating the protocol
|
|
1460
|
+
*
|
|
1461
|
+
* @example
|
|
1462
|
+
* ```ts
|
|
1463
|
+
* import { url } from '@regle/rules';
|
|
1464
|
+
*
|
|
1465
|
+
* const { r$ } = useRegle({ bestUrl: '' }, {
|
|
1466
|
+
* bestUrl: { url },
|
|
1467
|
+
* // or with custom protocol validation
|
|
1468
|
+
* bestUrl: { url: url({ protocol: /^https?$/ }) },
|
|
1469
|
+
* })
|
|
1470
|
+
* ```
|
|
1471
|
+
*
|
|
1472
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#url Documentation}
|
|
1473
|
+
*/
|
|
1474
|
+
const url = createRule({
|
|
1475
|
+
type: "url",
|
|
1476
|
+
validator(value, options = {}) {
|
|
1477
|
+
try {
|
|
1478
|
+
if (isEmpty(value)) return true;
|
|
1479
|
+
const { protocol } = options || {};
|
|
1480
|
+
const urlInput = new URL(value);
|
|
1481
|
+
if (!HOSTNAME_REGEX.test(urlInput.hostname)) return false;
|
|
1482
|
+
if (protocol) {
|
|
1483
|
+
if (!protocol.test(urlInput.protocol.endsWith(":") ? urlInput.protocol.slice(0, -1) : urlInput.protocol)) return false;
|
|
1484
|
+
}
|
|
1485
|
+
return matchRegex(value, URL_REGEX);
|
|
1486
|
+
} catch {
|
|
1487
|
+
return false;
|
|
1488
|
+
}
|
|
1489
|
+
},
|
|
1490
|
+
message: "The value is not a valid URL address"
|
|
1491
|
+
});
|
|
1492
|
+
|
|
1493
|
+
/**
|
|
1494
|
+
* Validates HTTP URLs.
|
|
1495
|
+
*
|
|
1496
|
+
* @param options - Optional configuration for http url validation
|
|
1497
|
+
* @param options.protocol - Optional regex for validating the protocol
|
|
1498
|
+
*
|
|
1499
|
+
* @example
|
|
1500
|
+
* ```ts
|
|
1501
|
+
* import { httpUrl } from '@regle/rules';
|
|
1502
|
+
*
|
|
1503
|
+
* const { r$ } = useRegle({ bestUrl: '' }, {
|
|
1504
|
+
* bestUrl: { httpUrl },
|
|
1505
|
+
* // or to force https protocol
|
|
1506
|
+
* bestUrl: { httpUrl: httpUrl({ protocol: /^https$/ }) },
|
|
1507
|
+
* })
|
|
1508
|
+
* ```
|
|
1509
|
+
*
|
|
1510
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#httpurl Documentation}
|
|
1511
|
+
*/
|
|
1512
|
+
const httpUrl = createRule({
|
|
1513
|
+
type: "httpUrl",
|
|
1514
|
+
validator(value, options = {}) {
|
|
1515
|
+
if (isEmpty(value)) return true;
|
|
1516
|
+
const { protocol = DEFAULT_PROTOCOL_REGEX } = options || {};
|
|
1517
|
+
return url({ protocol }).exec(value);
|
|
1518
|
+
},
|
|
1519
|
+
message: "The value is not a valid http URL address"
|
|
1520
|
+
});
|
|
1521
|
+
|
|
1329
1522
|
/**
|
|
1330
1523
|
* Allows only integers (positive and negative).
|
|
1331
1524
|
*
|
|
@@ -1344,7 +1537,7 @@ const integer = createRule({
|
|
|
1344
1537
|
type: "integer",
|
|
1345
1538
|
validator(value) {
|
|
1346
1539
|
if (isEmpty(value)) return true;
|
|
1347
|
-
return matchRegex(value,
|
|
1540
|
+
return matchRegex(value, INTEGER_REGEX);
|
|
1348
1541
|
},
|
|
1349
1542
|
message: "The value must be an integer"
|
|
1350
1543
|
});
|
|
@@ -1404,6 +1597,29 @@ function literal(literal$1) {
|
|
|
1404
1597
|
}, [computed(() => toValue(literal$1))]), ({ $params: [literal$2] }) => `Value should be ${literal$2}.`);
|
|
1405
1598
|
}
|
|
1406
1599
|
|
|
1600
|
+
/**
|
|
1601
|
+
* Validates lowercase strings.
|
|
1602
|
+
*
|
|
1603
|
+
* @example
|
|
1604
|
+
* ```ts
|
|
1605
|
+
* import { lowercase } from '@regle/rules';
|
|
1606
|
+
*
|
|
1607
|
+
* const { r$ } = useRegle({ name: '' }, {
|
|
1608
|
+
* name: { lowercase },
|
|
1609
|
+
* })
|
|
1610
|
+
* ```
|
|
1611
|
+
*
|
|
1612
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#lowercase Documentation}
|
|
1613
|
+
*/
|
|
1614
|
+
const lowercase = createRule({
|
|
1615
|
+
type: "lowercase",
|
|
1616
|
+
validator(value) {
|
|
1617
|
+
if (isEmpty(value)) return true;
|
|
1618
|
+
return matchRegex(value, LOWERCASE_REGEX);
|
|
1619
|
+
},
|
|
1620
|
+
message: "The value is not a valid lowercase string"
|
|
1621
|
+
});
|
|
1622
|
+
|
|
1407
1623
|
/**
|
|
1408
1624
|
* Validates MAC addresses. Call as a function to specify a custom separator (e.g., `':'` or an empty string for `00ff1122334455`).
|
|
1409
1625
|
*
|
|
@@ -1439,6 +1655,8 @@ const hexValid = (hex) => hex.toLowerCase().match(/^[0-9a-f]{2}$/);
|
|
|
1439
1655
|
/**
|
|
1440
1656
|
* Requires a value to be a file with a maximum size.
|
|
1441
1657
|
*
|
|
1658
|
+
* @param maxSize - The maximum size of the file
|
|
1659
|
+
*
|
|
1442
1660
|
* @example
|
|
1443
1661
|
* ```ts
|
|
1444
1662
|
* import { type InferInput } from '@regle/core';
|
|
@@ -1474,6 +1692,7 @@ const maxFileSize = createRule({
|
|
|
1474
1692
|
*
|
|
1475
1693
|
* @param max - The maximum length
|
|
1476
1694
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
1695
|
+
* @param options.allowEqual - Optional flag to allow equal length
|
|
1477
1696
|
*
|
|
1478
1697
|
* @example
|
|
1479
1698
|
* ```ts
|
|
@@ -1517,6 +1736,7 @@ const maxLength = createRule({
|
|
|
1517
1736
|
*
|
|
1518
1737
|
* @param max - The maximum value
|
|
1519
1738
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
1739
|
+
* @param options.allowEqual - Optional flag to allow equal value
|
|
1520
1740
|
*
|
|
1521
1741
|
* @example
|
|
1522
1742
|
* ```ts
|
|
@@ -1559,6 +1779,8 @@ const maxValue = createRule({
|
|
|
1559
1779
|
/**
|
|
1560
1780
|
* Requires a value to be a file with a minimum size.
|
|
1561
1781
|
*
|
|
1782
|
+
* @param minSize - The minimum size of the file
|
|
1783
|
+
*
|
|
1562
1784
|
* @example
|
|
1563
1785
|
* ```ts
|
|
1564
1786
|
* import { type InferInput } from '@regle/core';
|
|
@@ -1594,6 +1816,7 @@ const minFileSize = createRule({
|
|
|
1594
1816
|
*
|
|
1595
1817
|
* @param min - The minimum length
|
|
1596
1818
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
1819
|
+
* @param options.allowEqual - Optional flag to allow equal length
|
|
1597
1820
|
*
|
|
1598
1821
|
* @example
|
|
1599
1822
|
* ```ts
|
|
@@ -1637,6 +1860,7 @@ const minLength = createRule({
|
|
|
1637
1860
|
*
|
|
1638
1861
|
* @param min - The minimum value
|
|
1639
1862
|
* @param options - Optional configuration (e.g., `{ allowEqual: false }`)
|
|
1863
|
+
* @param options.allowEqual - Optional flag to allow equal value
|
|
1640
1864
|
*
|
|
1641
1865
|
* @example
|
|
1642
1866
|
* ```ts
|
|
@@ -1737,7 +1961,6 @@ const number = createRule({
|
|
|
1737
1961
|
message: "The value must be a native number"
|
|
1738
1962
|
});
|
|
1739
1963
|
|
|
1740
|
-
const numericRegex = /^\d*(\.\d+)?$/;
|
|
1741
1964
|
/**
|
|
1742
1965
|
* Allows only numeric values (including numeric strings).
|
|
1743
1966
|
*
|
|
@@ -1756,7 +1979,7 @@ const numeric = createRule({
|
|
|
1756
1979
|
type: "numeric",
|
|
1757
1980
|
validator(value) {
|
|
1758
1981
|
if (isEmpty(value)) return true;
|
|
1759
|
-
return matchRegex(value,
|
|
1982
|
+
return matchRegex(value, NUMERIC_REGEX);
|
|
1760
1983
|
},
|
|
1761
1984
|
message: "The value must be numeric"
|
|
1762
1985
|
});
|
|
@@ -2026,30 +2249,26 @@ function type() {
|
|
|
2026
2249
|
}
|
|
2027
2250
|
|
|
2028
2251
|
/**
|
|
2029
|
-
*
|
|
2030
|
-
*/
|
|
2031
|
-
const 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;
|
|
2032
|
-
/**
|
|
2033
|
-
* Validates URLs.
|
|
2252
|
+
* Validates uppercase strings.
|
|
2034
2253
|
*
|
|
2035
2254
|
* @example
|
|
2036
2255
|
* ```ts
|
|
2037
|
-
* import {
|
|
2256
|
+
* import { uppercase } from '@regle/rules';
|
|
2038
2257
|
*
|
|
2039
|
-
* const { r$ } = useRegle({
|
|
2040
|
-
*
|
|
2258
|
+
* const { r$ } = useRegle({ name: '' }, {
|
|
2259
|
+
* name: { uppercase },
|
|
2041
2260
|
* })
|
|
2042
2261
|
* ```
|
|
2043
2262
|
*
|
|
2044
|
-
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#
|
|
2263
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#uppercase Documentation}
|
|
2045
2264
|
*/
|
|
2046
|
-
const
|
|
2047
|
-
type: "
|
|
2265
|
+
const uppercase = createRule({
|
|
2266
|
+
type: "uppercase",
|
|
2048
2267
|
validator(value) {
|
|
2049
2268
|
if (isEmpty(value)) return true;
|
|
2050
|
-
return matchRegex(value,
|
|
2269
|
+
return matchRegex(value, UPPERCASE_REGEX);
|
|
2051
2270
|
},
|
|
2052
|
-
message: "The value is not a valid
|
|
2271
|
+
message: "The value is not a valid uppercase string"
|
|
2053
2272
|
});
|
|
2054
2273
|
|
|
2055
|
-
export { alpha, alphaNum, and, applyIf, assignIf, between, boolean, checked, contains, date, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, file, fileType, getSize, hexadecimal, integer, ipv4Address, isDate, isEmpty, isFilled, isNumber, literal, macAddress, matchRegex, maxFileSize, maxLength, maxValue, minFileSize, minLength, minValue, nativeEnum, not, number, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, string, toDate, toNumber, type, url, withAsync, withMessage, withParams, withTooltip };
|
|
2274
|
+
export { alpha, alphaNum, and, applyIf, assignIf, between, boolean, checked, contains, date, dateAfter, dateBefore, dateBetween, decimal, email, emoji, endsWith, exactDigits, exactLength, exactValue, file, fileType, getSize, hexadecimal, hostname, httpUrl, integer, ipv4Address, isDate, isEmpty, isFilled, isNumber, literal, lowercase, macAddress, matchRegex, maxFileSize, maxLength, maxValue, minFileSize, minLength, minValue, nativeEnum, not, number, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, string, toDate, toNumber, type, uppercase, url, withAsync, withMessage, withParams, withTooltip };
|
package/dist/regle-rules.min.js
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
import{InternalRuleType as e,createRule as t,unwrapRuleParameters as n}from"@regle/core";import{computed as r,toValue as i,unref as a}from"vue";function o(n,r){let i,a,o,s,c=!1;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s,_async:c}=n;let l=t({type:i,validator:a,active:o,message:r,async:c}),u=[...s??[]];if(l._params=u,l._message_patched=!0,typeof l==`function`){if(s!=null){let e=l(...u);return e._message_patched=!0,e}return l}else return l}function s(n,r){let i,a,o,s,c,l=!1;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s,_message:c,_async:l}=n;let u=t({type:i,validator:a,active:o,message:c,tooltip:r,async:l}),d=[...s??[]];if(u._params=d,u._tooltip_patched=!0,typeof u==`function`){let e=u(...d);return u._tooltip_patched=!0,e}else return u}function c(n,r){let i,a,o=[],s=``;typeof n==`function`?(a=async(e,...t)=>n(e,...t),o=[r]):({_type:i,_message:s}=n,o=o=n._params?.concat(r),a=async(...e)=>n.validator(e));let c=t({type:i??e.Async,validator:a,message:s,async:!0});return c._params=c._params?.concat(o),c(...r??[])}function l(n,r){let i,a,o=[],s=``;typeof n==`function`?(e.Inline,a=(e,...t)=>n(e,...t),o=[r]):({_type:i,validator:a,_message:s}=n,o=o=n._params?.concat(r));let c=t({type:e.Inline,validator:a,message:s});return c._params=c._params?.concat(o),c(...r)}function u(r,i){let a,o,s=[],c=``;typeof i==`function`?(a=e.Inline,o=i,s=[r]):({_type:a,validator:o,_message:c}=i,s=i._params?.concat([r]));function l(e,...t){let[i]=n([r]);return i?o(e,...t):!0}function u(){let[e]=n([r]);return e}let d=t({type:a,validator:l,active:u,message:c}),f=[...s??[]];return d._params=f,typeof d==`function`?d(...f):d}function d(e){return e?.constructor?.name==`File`}function f(e,t=!0){return e==null?!0:e instanceof Date?isNaN(e.getTime()):d(e)?e.size<=0:Array.isArray(e)?t?e.length===0:!1:typeof e==`object`&&e?Object.keys(e).length===0:!String(e).length}function p(e){return e&&(e instanceof Date||e.constructor.name==`File`||e.constructor.name==`FileList`)?!1:typeof e==`object`&&!!e&&!Array.isArray(e)}function m(e){if(f(e))return!1;try{let t=null;if(e instanceof Date)t=e;else if(typeof e==`string`){let n=new Date(e);if(n.toString()===`Invalid Date`)return!1;t=n}return!!t}catch{return!1}}function h(e){let t=Object.prototype.toString.call(e);return e==null?new Date(NaN):e instanceof Date||typeof e==`object`&&t===`[object Date]`?new Date(e.getTime()):typeof e==`number`||t===`[object Number]`||typeof e==`string`||t===`[object String]`?new Date(e):new Date(NaN)}function g(e){return e===void 0?`0 bytes`:e<1024?`${e} bytes`:e<1024*1024?`${(e/1024).toFixed(2)} kb`:e<1024*1024*1024?`${(e/1024/1024).toFixed(2)} mb`:`${(e/1024/1024/1024).toFixed(2)} gb`}function _(e,t=!0){return!f(typeof e==`string`?e.trim():e,t)}function v(e){return e==null?!1:typeof e==`number`?!isNaN(e):!1}function y(e,...t){if(f(e))return!0;let n=typeof e==`number`?e.toString():e;return t.every(e=>(e.lastIndex=0,e.test(n)))}function b(e){let t=a(e);return Array.isArray(t)?t.length:typeof t==`object`?Object.keys(t).length:typeof t==`number`?isNaN(t)?0:t:String(t).length}function x(e){return typeof e==`number`?e:e==null?NaN:typeof e==`string`&&e.trim()===e?+e:NaN}function S(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>{if(typeof e==`function`)return null;{let t=e._params;return t?.length?t:[]}}).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e?.some(e=>typeof e!=`boolean`)?{$valid:e.every(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.every(e=>!!e)}let o;o=e.length?n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)):e=>!1;let s=t({type:`and`,validator:o,message:`The value does not match all of the provided validators`}),c=[...r??[]];return s._params=c,typeof s==`function`?s(...c):s}function ee(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>typeof e==`function`?null:e._params).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e.some(e=>typeof e!=`boolean`)?{$valid:e.some(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.some(e=>!!e)}let o=t({type:`or`,validator:n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)),message:`The value does not match any of the provided validators`}),s=[...r??[]];return o._params=s,typeof o==`function`?o(...s):o}function C(e,n){let r,i,a,o,s;typeof e==`function`?(i=e,s=e.constructor.name===`AsyncFunction`):({_type:r,validator:i,_params:o}=e,s=e._async),a=s?async(e,...t)=>_(e)?!await i(e,...t):!0:(e,...t)=>_(e)?!i(e,...t):!0;let c=t({type:`not`,validator:a,message:n??`Error`}),l=[...o??[]];return c._params=l,typeof c==`function`?c(...l):c}function w(e,t,n){return Object.entries(i(t)).map(([t,r])=>typeof r==`function`||p(r)&&`_validator`in r?[t,u(n?e:()=>!i(e),r)]:[t,r])}function T(e,t,n){return r(()=>{let r=w(e,t,!0),i=n?w(e,n,!1):[];return Object.fromEntries([...r,...i])})}const E=/^[a-zA-Z]*$/,D=/^[\w.]+$/,O=t({type:`alpha`,validator(e,t){return f(e)?!0:t?.allowSymbols?y(e,D):y(e,E)},message:`The value is not alphabetical`}),k=/^[a-zA-Z0-9]*$/,A=/^[a-zA-Z0-9_]*$/,j=t({type:`alphaNum`,validator(e,t){return f(e)?!0:t?.allowSymbols?y(e,A):y(e,k)},message:`The value must be alpha-numeric`}),M=t({type:`between`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};if(_(e)&&_(t)&&_(n)){let r=x(e),a=x(t),o=x(n);return v(r)&&v(a)&&v(o)?i?r>=a&&r<=o:r>a&&r<o:(console.warn(`[between] Value or parameters aren't numbers, got value: ${e}, min: ${t}, max: ${n}`),!1)}return!0},message:({$params:[e,t]})=>`The value must be between ${e} and ${t}`}),N=t({type:`boolean`,validator:e=>_(e)?typeof e==`boolean`:!0,message:`The value must be a native boolean`}),P=t({type:`checked`,validator:e=>_(e)?e===!0:!0,message:`The field must be checked`}),F=t({type:`contains`,validator(e,t){return _(e)&&_(t)?e.includes(t):!0},message({$params:[e]}){return`The value must contain ${e}`}}),I=t({type:`date`,validator:e=>_(e)?e instanceof Date:!0,message:`The value must be a native Date`});function L(){return navigator.languages==null?navigator.language:navigator.languages[0]}function R(e){return e?new Intl.DateTimeFormat(L(),{dateStyle:`short`}).format(new Date(e)):`?`}const z=t({type:`dateAfter`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e)&&_(t)?m(e)&&m(t)?(r?h(e).getTime()>=h(t).getTime():h(e).getTime()>h(t).getTime())?!0:{$valid:!1,error:`date-not-after`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be after ${R(e)}`}),B=t({type:`dateBefore`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e)&&_(t)?m(e)&&m(t)?(r?h(e).getTime()<=h(t).getTime():h(e).getTime()<h(t).getTime())?!0:{$valid:!1,error:`date-not-before`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be before ${R(e)}`}),V=t({type:`dateBetween`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};return m(e)&&m(t)&&m(n)?i?h(e).getTime()>=h(t).getTime()&&h(e).getTime()<=h(n).getTime():h(e).getTime()>h(t).getTime()&&h(e).getTime()<h(n).getTime():!0},message:({$params:[e,t]})=>`The date must be between ${R(e)} and ${R(t)}`}),H=/^[-]?\d*(\.\d+)?$/,U=t({type:`decimal`,validator(e){return f(e)?!0:y(e,H)},message:`The value must be decimal`}),W=/^(?:[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,G=t({type:`email`,validator(e){return f(e)?!0:y(e,W)},message:`The value must be an valid email address`}),K=t({type:`endsWith`,validator(e,t){return _(e)&&_(t)?e.endsWith(t):!0},message({$params:[e]}){return`The value must end with ${e}`}}),q=t({type:`exactLength`,validator:(e,t)=>_(e,!1)&&_(t)?v(t)?b(e)===t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$params:[e]})=>`The value should be exactly ${e} characters long`}),J=t({type:`exactValue`,validator:(e,t)=>_(e)&&_(t)?v(t)&&!isNaN(x(e))?x(e)===t:(console.warn(`[exactValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The value must be equal to ${e}`}),Y=t({type:`file`,validator:e=>_(e)?d(e):!0,message:`The value must be a native File`}),X=t({type:`fileType`,validator:(e,t)=>_(e)?d(e)?t.includes(e.type):!1:!0,message({$params:[e]}){return`File type is not allowed. Allowed types are: ${e.map(e=>e.split(`/`)[1]).join(`, `)}.`}}),te=/^[a-fA-F0-9]*$/,Z=t({type:`hexadecimal`,validator(e){return f(e)?!0:y(e,te)},message:`The value must be hexadecimal`}),Q=/(^[0-9]*$)|(^-[0-9]+$)/,ne=t({type:`integer`,validator(e){return f(e)?!0:y(e,Q)},message:`The value must be an integer`});function re(e){if(e.length>3||e.length===0||e[0]===`0`&&e!==`0`||!e.match(/^\d+$/))return!1;let t=e|0;return t>=0&&t<=255}const ie=t({type:`ipv4Address`,validator(e){if(f(e))return!0;if(typeof e!=`string`)return!1;let t=e.split(`.`);return t.length===4&&t.every(re)},message:`The value is not a valid IPv4 address`});function ae(e){return o(l((e,t)=>_(e)&&_(t)?t===e:!0,[r(()=>i(e))]),({$params:[e]})=>`Value should be ${e}.`)}const oe=t({type:`macAddress`,validator(e,t=`:`){if(f(e))return!0;if(typeof e!=`string`)return!1;let n=typeof t==`string`&&t!==``?e.split(t):e.length===12||e.length===16?e.match(/.{2}/g):null;return n!==null&&(n.length===6||n.length===8)&&n.every(se)},message:`The value is not a valid MAC Address`}),se=e=>e.toLowerCase().match(/^[0-9a-f]{2}$/),ce=t({type:`maxFileSize`,validator:(e,t)=>_(e)&&d(e)?{$valid:e.size<=t,fileSize:e.size}:!0,message({$params:[e],fileSize:t}){return`File size (${g(t)}) cannot exceed ${g(e)}`}}),le=t({type:`maxLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e,!1)&&_(t)?v(t)?r?b(e)<=t:b(e)<t:(console.warn(`[maxLength] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have maximum ${t} items`:`The value length should not exceed ${t}`}),ue=t({type:`maxValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e)&&_(t)?!isNaN(x(e))&&!isNaN(x(t))?r?x(e)<=x(t):x(e)<x(t):(console.warn(`[maxValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be less than or equal to ${e}`:`The value must be less than ${e}`}}),de=t({type:`minFileSize`,validator:(e,t)=>_(e)&&d(e)?{$valid:e.size>=t,fileSize:e.size}:!0,message({$params:[e],fileSize:t}){return`File size (${g(t)}) must be at least ${g(e)}`}}),fe=t({type:`minLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e,!1)&&_(t)?v(t)?r?b(e)>=t:b(e)>t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0},message:({$value:e,$params:[t]})=>Array.isArray(e)?`The list should have at least ${t} items`:`The value length should be at least ${t}`}),pe=t({type:`minValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e)&&_(t)?!isNaN(x(e))&&!isNaN(x(t))?r?x(e)>=x(t):x(e)>x(t):(console.warn(`[minValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be greater than or equal to ${e}`:`The value must be greater than ${e}`}});function me(e){let t=Object.keys(e).filter(t=>typeof e[e[t]]!=`number`),n={};for(let r of t)n[r]=e[r];return Object.values(n)}function he(e){return o(l((e,t)=>_(e)&&!f(t)?me(t).includes(e):!0,[r(()=>i(e))]),({$params:[e]})=>`The value should be one of those options: ${Object.values(e).join(`, `)}.`)}const ge=t({type:`number`,validator:e=>_(e)?v(e):!0,message:`The value must be a native number`}),_e=/^\d*(\.\d+)?$/,ve=t({type:`numeric`,validator(e){return f(e)?!0:y(e,_e)},message:`The value must be numeric`}),ye=t({type:`oneOf`,validator(e,t){return _(e)&&_(t,!1)?t.includes(e):!0},message:({$params:[e]})=>`The value should be one of those options: ${e.join(`, `)}.`}),be=t({type:`regex`,validator(e,t){return _(e)?y(e,...Array.isArray(t)?t:[t]):!0},message:`The value does not match the required pattern`}),$=t({type:`required`,validator:e=>_(e),message:`This field is required`}),xe=t({type:`required`,validator(e,t){return t?_(e):!0},message:`This field is required`,active({$params:[e]}){return e}}),Se=t({type:`required`,validator(e,t){return t?!0:_(e)},message:`This field is required`,active({$params:[e]}){return!e}}),Ce=t({type:`sameAs`,validator(e,t,n){return f(e)?!0:e===t},message({$params:[e,t=`other`]}){return`The value must be equal to the ${t} value`}}),we=t({type:`startsWith`,validator(e,t){return _(e)&&_(t)?e.startsWith(t):!0},message({$params:[e]}){return`The value must start with ${e}`}}),Te=t({type:`string`,validator:e=>_(e)?typeof e==`string`:!0,message:`The value must be a string`});function Ee(){return(()=>!0)}const De=/^(?:(?:(?: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,Oe=t({type:`url`,validator(e){return f(e)?!0:y(e,De)},message:`The value is not a valid URL address`});export{O as alpha,j as alphaNum,S as and,u as applyIf,T as assignIf,M as between,N as boolean,P as checked,F as contains,I as date,z as dateAfter,B as dateBefore,V as dateBetween,U as decimal,G as email,K as endsWith,q as exactLength,J as exactValue,Y as file,X as fileType,b as getSize,Z as hexadecimal,ne as integer,ie as ipv4Address,m as isDate,f as isEmpty,_ as isFilled,v as isNumber,ae as literal,oe as macAddress,y as matchRegex,ce as maxFileSize,le as maxLength,ue as maxValue,de as minFileSize,fe as minLength,pe as minValue,he as nativeEnum,C as not,ge as number,ve as numeric,ye as oneOf,ee as or,be as regex,$ as required,xe as requiredIf,Se as requiredUnless,Ce as sameAs,we as startsWith,Te as string,h as toDate,x as toNumber,Ee as type,Oe as url,c as withAsync,o as withMessage,l as withParams,s as withTooltip};
|
|
1
|
+
/**
|
|
2
|
+
* @regle/rules v1.16.0
|
|
3
|
+
* (c) 2025 Victor Garcia
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import{InternalRuleType as e,createRule as t,unwrapRuleParameters as n}from"@regle/core";import{computed as r,toValue as i,unref as a}from"vue";function o(n,r){let i,a,o,s,c=!1;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s,_async:c}=n;let l=t({type:i,validator:a,active:o,message:r,async:c}),u=[...s??[]];if(l._params=u,l._message_patched=!0,typeof l==`function`){if(s!=null){let e=l(...u);return e._message_patched=!0,e}return l}else return l}function s(n,r){let i,a,o,s,c,l=!1;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s,_message:c,_async:l}=n;let u=t({type:i,validator:a,active:o,message:c,tooltip:r,async:l}),d=[...s??[]];if(u._params=d,u._tooltip_patched=!0,typeof u==`function`){let e=u(...d);return u._tooltip_patched=!0,e}else return u}function c(n,r){let i,a,o=[],s=``;typeof n==`function`?(a=async(e,...t)=>n(e,...t),o=[r]):({_type:i,_message:s}=n,o=o=n._params?.concat(r),a=async(...e)=>n.validator(e));let c=t({type:i??e.Async,validator:a,message:s,async:!0});return c._params=c._params?.concat(o),c(...r??[])}function l(n,r){let i,a,o=[],s=``;typeof n==`function`?(e.Inline,a=(e,...t)=>n(e,...t),o=[r]):({_type:i,validator:a,_message:s}=n,o=o=n._params?.concat(r));let c=t({type:e.Inline,validator:a,message:s});return c._params=c._params?.concat(o),c(...r)}function u(r,i){let a,o,s=[],c=``;typeof i==`function`?(a=e.Inline,o=i,s=[r]):({_type:a,validator:o,_message:c}=i,s=i._params?.concat([r]));function l(e,...t){let[i]=n([r]);return i?o(e,...t):!0}function u(){let[e]=n([r]);return e}let d=t({type:a,validator:l,active:u,message:c}),f=[...s??[]];return d._params=f,typeof d==`function`?d(...f):d}function d(e){return e?.constructor?.name==`File`}function f(e,t=!0){return e==null?!0:e instanceof Date?isNaN(e.getTime()):d(e)?e.size<=0:Array.isArray(e)?t?e.length===0:!1:typeof e==`object`&&e?Object.keys(e).length===0:!String(e).length}function ee(e){return e&&(e instanceof Date||e.constructor.name==`File`||e.constructor.name==`FileList`)?!1:typeof e==`object`&&!!e&&!Array.isArray(e)}function p(e){if(f(e))return!1;let t=null;if(e instanceof Date)t=e;else if(typeof e==`string`){let n=new Date(e);if(n.toString()===`Invalid Date`)return!1;t=n}return!!t}function m(e){let t=Object.prototype.toString.call(e);return e==null?new Date(NaN):e instanceof Date||typeof e==`object`&&t===`[object Date]`?new Date(e.getTime()):typeof e==`number`||t===`[object Number]`||typeof e==`string`||t===`[object String]`?new Date(e):new Date(NaN)}function h(e){return e===void 0?`0 bytes`:e<1024?`${e} bytes`:e<1024*1024?`${(e/1024).toFixed(2)} kb`:e<1024*1024*1024?`${(e/1024/1024).toFixed(2)} mb`:`${(e/1024/1024/1024).toFixed(2)} gb`}function g(e,t=!0){return!f(typeof e==`string`?e.trim():e,t)}function _(e){return e==null?!1:typeof e==`number`?!isNaN(e):!1}function v(e,...t){if(f(e))return!0;let n=typeof e==`number`?e.toString():e;return t.every(e=>(e.lastIndex=0,e.test(n)))}function y(e){let t=a(e);return Array.isArray(t)?t.length:typeof t==`object`?Object.keys(t).length:typeof t==`number`?isNaN(t)?0:t:String(t).length}function b(e){return typeof e==`number`?e:e==null?NaN:typeof e==`string`&&e.trim()===e?+e:NaN}function x(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>{if(typeof e==`function`)return null;{let t=e._params;return t?.length?t:[]}}).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e?.some(e=>typeof e!=`boolean`)?{$valid:e.every(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.every(e=>!!e)}let o;o=e.length?n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)):e=>!1;let s=t({type:`and`,validator:o,message:`The value does not match all of the provided validators`}),c=[...r??[]];return s._params=c,typeof s==`function`?s(...c):s}function S(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>typeof e==`function`?null:e._params).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e.some(e=>typeof e!=`boolean`)?{$valid:e.some(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.some(e=>!!e)}let o=t({type:`or`,validator:n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)),message:`The value does not match any of the provided validators`}),s=[...r??[]];return o._params=s,typeof o==`function`?o(...s):o}function te(e,n){let r,i,a,o,s;typeof e==`function`?(i=e,s=e.constructor.name===`AsyncFunction`):({_type:r,validator:i,_params:o}=e,s=e._async),a=s?async(e,...t)=>g(e)?!await i(e,...t):!0:(e,...t)=>g(e)?!i(e,...t):!0;let c=t({type:`not`,validator:a,message:n??`Error`}),l=[...o??[]];return c._params=l,typeof c==`function`?c(...l):c}function C(e,t,n){return Object.entries(i(t)).map(([t,r])=>typeof r==`function`||ee(r)&&`_validator`in r?[t,u(n?e:()=>!i(e),r)]:[t,r])}function ne(e,t,n){return r(()=>{let r=C(e,t,!0),i=n?C(e,n,!1):[];return Object.fromEntries([...r,...i])})}const w=/^https?$/,T=/^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/,E=/^(?:(?:(?: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;function D(){return RegExp(`^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`,`u`)}const O=/^[^A-Z]*$/,k=/^[^a-z]*$/,A=/^[0-9a-fA-F]*$/,j=/^[a-zA-Z]*$/,M=/^[\w.]+$/,N=/^[a-zA-Z0-9]*$/,P=/^[a-zA-Z0-9_]*$/,F=/^[-]?\d*(\.\d+)?$/,I=/^(?:[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,L=/(^[0-9]*$)|(^-[0-9]+$)/,R=/^\d*(\.\d+)?$/,z=t({type:`alpha`,validator(e,t){return f(e)?!0:t?.allowSymbols?v(e,M):v(e,j)},message:`The value is not alphabetical`}),B=t({type:`alphaNum`,validator(e,t){return f(e)?!0:t?.allowSymbols?v(e,P):v(e,N)},message:`The value must be alpha-numeric`}),V=t({type:`between`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};if(g(e)&&g(t)&&g(n)){let r=b(e),a=b(t),o=b(n);return _(r)&&_(a)&&_(o)?i?r>=a&&r<=o:r>a&&r<o:!1}return!0},message:({$params:[e,t]})=>`The value must be between ${e} and ${t}`}),H=t({type:`boolean`,validator:e=>g(e)?typeof e==`boolean`:!0,message:`The value must be a native boolean`}),U=t({type:`checked`,validator:e=>g(e)?e===!0:!0,message:`The field must be checked`}),W=t({type:`contains`,validator(e,t){return g(e)&&g(t)?e.includes(t):!0},message({$params:[e]}){return`The value must contain ${e}`}}),G=t({type:`date`,validator:e=>g(e)?e instanceof Date:!0,message:`The value must be a native Date`});function K(){return navigator.languages==null?navigator.language:navigator.languages[0]}function q(e){return e?new Intl.DateTimeFormat(K(),{dateStyle:`short`}).format(new Date(e)):`?`}const J=t({type:`dateAfter`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?p(e)&&p(t)?(r?m(e).getTime()>=m(t).getTime():m(e).getTime()>m(t).getTime())?!0:{$valid:!1,error:`date-not-after`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be after ${q(e)}`}),Y=t({type:`dateBefore`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?p(e)&&p(t)?(r?m(e).getTime()<=m(t).getTime():m(e).getTime()<m(t).getTime())?!0:{$valid:!1,error:`date-not-before`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be before ${q(e)}`}),X=t({type:`dateBetween`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};return p(e)&&p(t)&&p(n)?i?m(e).getTime()>=m(t).getTime()&&m(e).getTime()<=m(n).getTime():m(e).getTime()>m(t).getTime()&&m(e).getTime()<m(n).getTime():!0},message:({$params:[e,t]})=>`The date must be between ${q(e)} and ${q(t)}`}),Z=t({type:`decimal`,validator(e){return f(e)?!0:v(e,F)},message:`The value must be decimal`}),re=t({type:`email`,validator(e){return f(e)?!0:v(e,I)},message:`The value must be an valid email address`}),ie=t({type:`emoji`,validator(e){return f(e)?!0:D().test(e)},message:`The value should be a valid emoji`}),ae=t({type:`endsWith`,validator(e,t){return g(e)&&g(t)?e.endsWith(t):!0},message({$params:[e]}){return`The value must end with ${e}`}}),oe=t({type:`exactDigits`,validator:(e,t)=>{if(g(e,!1)&&g(t)){if(_(t)){let n=RegExp(`^\\d{${t}}$`);return v(e.toString(),n)}return!1}return!0},message:({$params:[e]})=>`The value should have exactly ${e} digits`}),se=t({type:`exactLength`,validator:(e,t)=>g(e,!1)&&g(t)?_(t)?y(e)===t:!1:!0,message:({$params:[e]})=>`The value should be exactly ${e} characters long`}),ce=t({type:`exactValue`,validator:(e,t)=>g(e)&&g(t)&&_(t)&&!isNaN(b(e))?b(e)===t:!0,message:({$params:[e]})=>`The value must be equal to ${e}`}),le=t({type:`file`,validator:e=>g(e)?d(e):!0,message:`The value must be a native File`}),ue=t({type:`fileType`,validator:(e,t)=>g(e)?d(e)?t.includes(e.type):!1:!0,message({$params:[e]}){return`File type is not allowed. Allowed types are: ${e.map(e=>e.split(`/`)[1]).join(`, `)}.`}}),de=t({type:`hexadecimal`,validator(e){return f(e)?!0:v(e,A)},message:`The value must be hexadecimal`}),fe=t({type:`hostname`,validator(e){return f(e)?!0:v(e,T)},message:`The value is not a valid hostname`}),Q=t({type:`url`,validator(e,t={}){try{if(f(e))return!0;let{protocol:n}=t||{},r=new URL(e);return!T.test(r.hostname)||n&&!n.test(r.protocol.endsWith(`:`)?r.protocol.slice(0,-1):r.protocol)?!1:v(e,E)}catch{return!1}},message:`The value is not a valid URL address`}),pe=t({type:`httpUrl`,validator(e,t={}){if(f(e))return!0;let{protocol:n=w}=t||{};return Q({protocol:n}).exec(e)},message:`The value is not a valid http URL address`}),me=t({type:`integer`,validator(e){return f(e)?!0:v(e,L)},message:`The value must be an integer`});function he(e){if(e.length>3||e.length===0||e[0]===`0`&&e!==`0`||!e.match(/^\d+$/))return!1;let t=e|0;return t>=0&&t<=255}const ge=t({type:`ipv4Address`,validator(e){if(f(e))return!0;if(typeof e!=`string`)return!1;let t=e.split(`.`);return t.length===4&&t.every(he)},message:`The value is not a valid IPv4 address`});function _e(e){return o(l((e,t)=>g(e)&&g(t)?t===e:!0,[r(()=>i(e))]),({$params:[e]})=>`Value should be ${e}.`)}const ve=t({type:`lowercase`,validator(e){return f(e)?!0:v(e,O)},message:`The value is not a valid lowercase string`}),ye=t({type:`macAddress`,validator(e,t=`:`){if(f(e))return!0;if(typeof e!=`string`)return!1;let n=typeof t==`string`&&t!==``?e.split(t):e.length===12||e.length===16?e.match(/.{2}/g):null;return n!==null&&(n.length===6||n.length===8)&&n.every(be)},message:`The value is not a valid MAC Address`}),be=e=>e.toLowerCase().match(/^[0-9a-f]{2}$/),xe=t({type:`maxFileSize`,validator:(e,t)=>g(e)&&d(e)?{$valid:e.size<=t,fileSize:e.size}:!0,message({$params:[e],fileSize:t}){return`File size (${h(t)}) cannot exceed ${h(e)}`}}),Se=t({type:`maxLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e,!1)&&g(t)?_(t)?r?y(e)<=t:y(e)<t:!1:!0},message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have maximum ${t} items`:`The value length should not exceed ${t}`}),Ce=t({type:`maxValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?!isNaN(b(e))&&!isNaN(b(t))?r?b(e)<=b(t):b(e)<b(t):!1:!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be less than or equal to ${e}`:`The value must be less than ${e}`}}),we=t({type:`minFileSize`,validator:(e,t)=>g(e)&&d(e)?{$valid:e.size>=t,fileSize:e.size}:!0,message({$params:[e],fileSize:t}){return`File size (${h(t)}) must be at least ${h(e)}`}}),Te=t({type:`minLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e,!1)&&g(t)?_(t)?r?y(e)>=t:y(e)>t:!1:!0},message:({$value:e,$params:[t]})=>Array.isArray(e)?`The list should have at least ${t} items`:`The value length should be at least ${t}`}),Ee=t({type:`minValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?!isNaN(b(e))&&!isNaN(b(t))?r?b(e)>=b(t):b(e)>b(t):!1:!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be greater than or equal to ${e}`:`The value must be greater than ${e}`}});function De(e){let t=Object.keys(e).filter(t=>typeof e[e[t]]!=`number`),n={};for(let r of t)n[r]=e[r];return Object.values(n)}function $(e){return o(l((e,t)=>g(e)&&!f(t)?De(t).includes(e):!0,[r(()=>i(e))]),({$params:[e]})=>`The value should be one of those options: ${Object.values(e).join(`, `)}.`)}const Oe=t({type:`number`,validator:e=>g(e)?_(e):!0,message:`The value must be a native number`}),ke=t({type:`numeric`,validator(e){return f(e)?!0:v(e,R)},message:`The value must be numeric`}),Ae=t({type:`oneOf`,validator(e,t){return g(e)&&g(t,!1)?t.includes(e):!0},message:({$params:[e]})=>`The value should be one of those options: ${e.join(`, `)}.`}),je=t({type:`regex`,validator(e,t){return g(e)?v(e,...Array.isArray(t)?t:[t]):!0},message:`The value does not match the required pattern`}),Me=t({type:`required`,validator:e=>g(e),message:`This field is required`}),Ne=t({type:`required`,validator(e,t){return t?g(e):!0},message:`This field is required`,active({$params:[e]}){return e}}),Pe=t({type:`required`,validator(e,t){return t?!0:g(e)},message:`This field is required`,active({$params:[e]}){return!e}}),Fe=t({type:`sameAs`,validator(e,t,n){return f(e)?!0:e===t},message({$params:[e,t=`other`]}){return`The value must be equal to the ${t} value`}}),Ie=t({type:`startsWith`,validator(e,t){return g(e)&&g(t)?e.startsWith(t):!0},message({$params:[e]}){return`The value must start with ${e}`}}),Le=t({type:`string`,validator:e=>g(e)?typeof e==`string`:!0,message:`The value must be a string`});function Re(){return(()=>!0)}const ze=t({type:`uppercase`,validator(e){return f(e)?!0:v(e,k)},message:`The value is not a valid uppercase string`});export{z as alpha,B as alphaNum,x as and,u as applyIf,ne as assignIf,V as between,H as boolean,U as checked,W as contains,G as date,J as dateAfter,Y as dateBefore,X as dateBetween,Z as decimal,re as email,ie as emoji,ae as endsWith,oe as exactDigits,se as exactLength,ce as exactValue,le as file,ue as fileType,y as getSize,de as hexadecimal,fe as hostname,pe as httpUrl,me as integer,ge as ipv4Address,p as isDate,f as isEmpty,g as isFilled,_ as isNumber,_e as literal,ve as lowercase,ye as macAddress,v as matchRegex,xe as maxFileSize,Se as maxLength,Ce as maxValue,we as minFileSize,Te as minLength,Ee as minValue,$ as nativeEnum,te as not,Oe as number,ke as numeric,Ae as oneOf,S as or,je as regex,Me as required,Ne as requiredIf,Pe as requiredUnless,Fe as sameAs,Ie as startsWith,Le as string,m as toDate,b as toNumber,Re as type,ze as uppercase,Q as url,c as withAsync,o as withMessage,l as withParams,s as withTooltip};
|
package/package.json
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@regle/rules",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.0",
|
|
4
4
|
"description": "Collection of rules and helpers for Regle",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"type-fest": "5.
|
|
7
|
-
"@regle/core": "1.
|
|
6
|
+
"type-fest": "5.3.1",
|
|
7
|
+
"@regle/core": "1.16.0"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
|
-
"@types/node": "22.19.
|
|
11
|
-
"@vue/reactivity": "3.5.
|
|
10
|
+
"@types/node": "22.19.3",
|
|
11
|
+
"@vue/reactivity": "3.5.26",
|
|
12
12
|
"@vue/test-utils": "2.4.6",
|
|
13
|
-
"prettier": "3.7.
|
|
14
|
-
"tsdown": "0.
|
|
15
|
-
"type-fest": "5.
|
|
13
|
+
"prettier": "3.7.4",
|
|
14
|
+
"tsdown": "0.18.3",
|
|
15
|
+
"type-fest": "5.3.1",
|
|
16
16
|
"typescript": "5.9.3",
|
|
17
|
-
"vitest": "4.0.
|
|
18
|
-
"vue": "3.5.
|
|
19
|
-
"vue-tsc": "3.1
|
|
17
|
+
"vitest": "4.0.16",
|
|
18
|
+
"vue": "3.5.26",
|
|
19
|
+
"vue-tsc": "3.2.1"
|
|
20
20
|
},
|
|
21
21
|
"type": "module",
|
|
22
22
|
"exports": {
|