@regle/core 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,213 @@
1
+ import * as vue from 'vue';
2
+ import { MaybeRef, Ref } from 'vue';
3
+
4
+ type ArrayElement<T> = T extends Array<infer U> ? U : never;
5
+
6
+ type Maybe<T> = T | null | undefined;
7
+
8
+ /**
9
+ * @argument
10
+ * createRule arguments options
11
+ */
12
+ interface RegleRuleInit<TValue extends any, TParams extends any[] = []> {
13
+ validator: (value: Maybe<TValue>, ...args: TParams) => boolean | Promise<boolean>;
14
+ message: string | ((value: Maybe<TValue>, ...args: TParams) => string);
15
+ active?: boolean | ((value: Maybe<TValue>, ...args: TParams) => boolean);
16
+ type: string;
17
+ }
18
+
19
+ declare const defaultValidators: {
20
+ maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
21
+ required: RegleRuleDefinition<unknown, []>;
22
+ requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
23
+ };
24
+ type DefaultValidators = typeof defaultValidators;
25
+
26
+ type ParamDecl<T = any> = MaybeRef<T> | (() => T);
27
+ type CreateFn<T extends any[]> = (...args: T) => any;
28
+ /**
29
+ * Transform normal parameters tuple declaration to a rich tuple declaration
30
+ *
31
+ * [foo: string, bar?: number] => [foo: MaybeRef<string> | (() => string), bar?: MaybeRef<number | undefined> | (() => number) | undefined]
32
+ */
33
+ type RegleUniversalParams<T extends any[] = [], F = CreateFn<T>> = [T] extends [[]] ? [] : Parameters<F extends (...args: infer Args) => any ? (...args: {
34
+ [K in keyof Args]: ParamDecl<Args[K]>;
35
+ }) => any : never>;
36
+ type UnwrapRegleUniversalParams<T extends ParamDecl[] = [], F = CreateFn<T>> = [T] extends [
37
+ [
38
+ ]
39
+ ] ? [] : Parameters<F extends (...args: infer Args) => any ? (...args: {
40
+ [K in keyof Args]: Args[K] extends ParamDecl<infer U> ? U : Args[K];
41
+ }) => any : never>;
42
+
43
+ /**
44
+ * Internal definition of the rule, can be used to reset or patch the rule
45
+ */
46
+ interface RegleInternalRuleDefs<TValue extends any = any, TParams extends any[] = []> {
47
+ _validator: (value: Maybe<TValue>, ...args: TParams) => boolean | Promise<boolean>;
48
+ _message: string | ((value: Maybe<TValue>, ...args: TParams) => string);
49
+ _active?: boolean | ((value: Maybe<TValue>, ...args: TParams) => boolean);
50
+ _type: string;
51
+ _patched: boolean;
52
+ _params?: RegleUniversalParams<TParams>;
53
+ }
54
+ declare enum InternalRuleType {
55
+ Inline = "__inline",
56
+ Async = "__async"
57
+ }
58
+
59
+ /**
60
+ * Returned typed of rules created with `createRule`
61
+ * */
62
+ interface RegleRuleDefinition<TValue extends any = any, TParams extends any[] = []> extends RegleInternalRuleDefs<TValue, TParams> {
63
+ validator: RegleRuleDefinitionProcessor<TValue, TParams, boolean | Promise<boolean>>;
64
+ message: RegleRuleDefinitionProcessor<TValue, TParams, string>;
65
+ active: RegleRuleDefinitionProcessor<TValue, TParams, boolean>;
66
+ type: string;
67
+ }
68
+ /**
69
+ * Rules with params created with `createRules` are callable while being customizable
70
+ */
71
+ interface RegleRuleWithParamsDefinition<TValue extends any = any, TParams extends any[] = []> extends RegleRuleInit<TValue, TParams>, RegleInternalRuleDefs<TValue, TParams> {
72
+ (...params: RegleUniversalParams<TParams>): RegleRuleDefinition<TValue, TParams>;
73
+ }
74
+ /**
75
+ * Generic types for a created RegleRule
76
+ */
77
+ type RegleRuleRaw<TValue extends any = any, TParams extends any[] = []> = RegleRuleDefinition<TValue, TParams> | RegleRuleWithParamsDefinition<TValue, TParams>;
78
+ /**
79
+ * Process the type of a created rule with `createRule`.
80
+ * For a rule with params it will return a function
81
+ * Otherwise it will return the rule definition
82
+ */
83
+ type InferRegleRule<TValue extends any = any, TParams extends any[] = []> = [
84
+ TParams
85
+ ] extends [[]] ? RegleRuleDefinition<TValue, TParams> : RegleRuleWithParamsDefinition<TValue, TParams>;
86
+ type RegleRuleDefinitionProcessor<TValue extends any = any, TParams extends any[] = [], TReturn = any> = (value: Maybe<TValue>, ...args: TParams) => TReturn;
87
+ type RegleCollectionRuleDefinition<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
88
+ $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
89
+ }) | {
90
+ $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
91
+ };
92
+
93
+ type CustomRulesDeclarationTree = Record<string, RegleRuleRaw<any, any>>;
94
+ type AllRulesDeclarations = CustomRulesDeclarationTree & DefaultValidators;
95
+
96
+ type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
97
+ [TKey in keyof TForm]?: RegleFormPropertyType<TForm[TKey], TCustomRules>;
98
+ };
99
+ type RegleFormPropertyType<TValue = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = [NonNullable<TValue>] extends [never] ? RegleRuleDecl<unknown, TCustomRules> : NonNullable<TValue> extends Array<any> ? RegleCollectionRuleDecl<TValue, TCustomRules> : NonNullable<TValue> extends Date ? RegleRuleDecl<NonNullable<TValue>, TCustomRules> : NonNullable<TValue> extends File ? RegleRuleDecl<NonNullable<TValue>, TCustomRules> : NonNullable<TValue> extends Record<string, any> ? ReglePartialValidationTree<NonNullable<TValue>, TCustomRules> : RegleRuleDecl<NonNullable<TValue>, TCustomRules>;
100
+ /**
101
+ * Rule tree for a form property
102
+ */
103
+ type RegleRuleDecl<TValue extends any = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
104
+ [TKey in keyof TCustomRules]?: TCustomRules[TKey] extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, TParams> : FormRuleDeclaration<TValue, any>;
105
+ };
106
+ type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
107
+ $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
108
+ }) | {
109
+ $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
110
+ };
111
+ /**
112
+ * TODO async
113
+ */
114
+ type InlineRuleDeclaration<TValue extends any> = (value: Maybe<TValue>, ...args: any[]) => boolean | Promise<boolean>;
115
+ /**
116
+ * Regroup inline and registered rules
117
+ * */
118
+ type FormRuleDeclaration<TValue extends any, TParams extends any[] = []> = InlineRuleDeclaration<TValue> | RegleRuleDefinition<TValue, TParams>;
119
+
120
+ type RegleErrorTree<TRules extends ReglePartialValidationTree<any, any>> = {
121
+ readonly [K in keyof TRules]: RegleValidationErrors<TRules[K]>;
122
+ };
123
+ type RegleValidationErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleRuleDecl<any, any> ? string[] : TRule extends ReglePartialValidationTree<any, any> ? RegleErrorTree<TRule> : TRule extends RegleCollectionRuleDefinition ? RegleCollectionErrors<TRule['$each']> : string[];
124
+ type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = {
125
+ readonly $errors: string[];
126
+ readonly $each: RegleValidationErrors<TRule>[];
127
+ };
128
+ type PossibleRegleErrors = RegleCollectionErrors<any> | string[] | RegleErrorTree<any>;
129
+ type DataType = string | number | Record<string, any> | File | Array<any> | Date | null | undefined;
130
+
131
+ interface RegleStatus<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState>> extends RegleCommonStatus<TState> {
132
+ readonly $fields: {
133
+ readonly [TKey in keyof TRules]: InferRegleStatusType<NonNullable<TRules[TKey]>, TState, TKey>;
134
+ };
135
+ }
136
+ type InferRegleStatusType<TRule extends RegleCollectionRuleDecl | RegleRuleDecl | ReglePartialValidationTree<any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> = TRule extends RegleCollectionRuleDefinition<any, any> ? RegleCollectionStatus<TRule['$each'], TState[TKey]> : TRule extends ReglePartialValidationTree<any> ? TState[TKey] extends Array<any> ? RegleCommonStatus<TState[TKey]> : RegleStatus<TState[TKey], TRule> : RegleFieldStatus<TRule, TState, TKey>;
137
+ interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, AllRulesDeclarations>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus<TState[TKey]> {
138
+ readonly $rules: {
139
+ readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
140
+ };
141
+ }
142
+ interface RegleCommonStatus<TValue = any> {
143
+ readonly $valid: boolean;
144
+ readonly $invalid: boolean;
145
+ readonly $dirty: boolean;
146
+ readonly $anyDirty: boolean;
147
+ readonly $pending: boolean;
148
+ $value: TValue;
149
+ readonly $error: boolean;
150
+ $touch: () => void;
151
+ $reset: () => void;
152
+ }
153
+ interface RegleSoftRuleStatus<TValue = any, TParams extends any[] = any[]> {
154
+ readonly $type: string;
155
+ readonly $message: string;
156
+ readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
157
+ readonly $active: boolean;
158
+ readonly $valid: boolean;
159
+ readonly $pending: boolean;
160
+ readonly $params?: TParams;
161
+ }
162
+ type RegleRuleStatus<TValue = any, TParams extends any[] = any[]> = {
163
+ readonly $type: string;
164
+ readonly $message: string;
165
+ readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
166
+ readonly $active: boolean;
167
+ readonly $valid: boolean;
168
+ readonly $pending: boolean;
169
+ } & ([TParams] extends [[]] ? {} : {
170
+ readonly $params: TParams;
171
+ });
172
+ interface RegleCollectionStatus<TRules extends RegleRuleDecl | ReglePartialValidationTree<any>, TState extends any[]> extends RegleFieldStatus<TRules, TState> {
173
+ readonly $each: Array<InferRegleStatusType<NonNullable<TRules>, TState, number>>;
174
+ }
175
+ type PossibleRegleStatus = RegleStatus<any, any> | RegleFieldStatus<any> | RegleSoftRuleStatus | RegleCollectionStatus<any, any> | RegleCommonStatus<any>;
176
+ type PossibleRegleFieldStatus = RegleFieldStatus<any, any> | RegleStatus<any, any>;
177
+
178
+ declare function createRule<TValue extends any, TParams extends any[] = []>(definition: RegleRuleInit<TValue, TParams>): InferRegleRule<TValue, TParams>;
179
+
180
+ /**
181
+ * Root function that allows you to define project-wise all your custom validators or overwrite default ones
182
+ *
183
+ * It will return utility functions that let you build type-safe forms
184
+ *
185
+ * @param customRules
186
+ */
187
+ declare function defineCustomValidators<TCustomRules extends Partial<AllRulesDeclarations>>(customRules: () => TCustomRules): {
188
+ useRegle: <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree & {
189
+ maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
190
+ required: RegleRuleDefinition<unknown, []>;
191
+ requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
192
+ } & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules)) => {
193
+ state: vue.Ref<TState>;
194
+ $regle: vue.Ref<RegleStatus<TState, TRules>>;
195
+ errors: RegleErrorTree<TRules>;
196
+ };
197
+ };
198
+
199
+ declare function withMessage<TValue extends any>(rule: InlineRuleDeclaration<TValue>, newMessage: string | ((value: TValue) => string)): RegleRuleDefinition<TValue>;
200
+ declare function withMessage<TValue extends any, TParams extends any[]>(rule: RegleRuleWithParamsDefinition<TValue, TParams>, newMessage: string | ((value: TValue, ...args: TParams) => string)): RegleRuleWithParamsDefinition<TValue, TParams>;
201
+ declare function withMessage<TValue extends any, TParams extends any[]>(rule: RegleRuleDefinition<TValue, TParams>, newMessage: string | ((value: TValue, ...args: TParams) => string)): RegleRuleDefinition<TValue, TParams>;
202
+
203
+ declare function withAsync<TParams extends (Ref<unknown> | (() => unknown))[]>(rule: InlineRuleDeclaration<any>, depsArray: [...TParams]): RegleRuleDefinition<any>;
204
+
205
+ declare function isEmpty(value: unknown): value is null | undefined;
206
+
207
+ declare const required: RegleRuleDefinition<unknown, []>;
208
+
209
+ declare const maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
210
+
211
+ declare const requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
212
+
213
+ export { AllRulesDeclarations, CustomRulesDeclarationTree, DataType, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, ParamDecl, PossibleRegleErrors, PossibleRegleFieldStatus, PossibleRegleStatus, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleSoftRuleStatus, RegleStatus, RegleUniversalParams, RegleValidationErrors, UnwrapRegleUniversalParams, createRule, defineCustomValidators, isEmpty, maxLength, required, requiredIf, withAsync, withMessage };
@@ -0,0 +1,213 @@
1
+ import * as vue from 'vue';
2
+ import { MaybeRef, Ref } from 'vue';
3
+
4
+ type ArrayElement<T> = T extends Array<infer U> ? U : never;
5
+
6
+ type Maybe<T> = T | null | undefined;
7
+
8
+ /**
9
+ * @argument
10
+ * createRule arguments options
11
+ */
12
+ interface RegleRuleInit<TValue extends any, TParams extends any[] = []> {
13
+ validator: (value: Maybe<TValue>, ...args: TParams) => boolean | Promise<boolean>;
14
+ message: string | ((value: Maybe<TValue>, ...args: TParams) => string);
15
+ active?: boolean | ((value: Maybe<TValue>, ...args: TParams) => boolean);
16
+ type: string;
17
+ }
18
+
19
+ declare const defaultValidators: {
20
+ maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
21
+ required: RegleRuleDefinition<unknown, []>;
22
+ requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
23
+ };
24
+ type DefaultValidators = typeof defaultValidators;
25
+
26
+ type ParamDecl<T = any> = MaybeRef<T> | (() => T);
27
+ type CreateFn<T extends any[]> = (...args: T) => any;
28
+ /**
29
+ * Transform normal parameters tuple declaration to a rich tuple declaration
30
+ *
31
+ * [foo: string, bar?: number] => [foo: MaybeRef<string> | (() => string), bar?: MaybeRef<number | undefined> | (() => number) | undefined]
32
+ */
33
+ type RegleUniversalParams<T extends any[] = [], F = CreateFn<T>> = [T] extends [[]] ? [] : Parameters<F extends (...args: infer Args) => any ? (...args: {
34
+ [K in keyof Args]: ParamDecl<Args[K]>;
35
+ }) => any : never>;
36
+ type UnwrapRegleUniversalParams<T extends ParamDecl[] = [], F = CreateFn<T>> = [T] extends [
37
+ [
38
+ ]
39
+ ] ? [] : Parameters<F extends (...args: infer Args) => any ? (...args: {
40
+ [K in keyof Args]: Args[K] extends ParamDecl<infer U> ? U : Args[K];
41
+ }) => any : never>;
42
+
43
+ /**
44
+ * Internal definition of the rule, can be used to reset or patch the rule
45
+ */
46
+ interface RegleInternalRuleDefs<TValue extends any = any, TParams extends any[] = []> {
47
+ _validator: (value: Maybe<TValue>, ...args: TParams) => boolean | Promise<boolean>;
48
+ _message: string | ((value: Maybe<TValue>, ...args: TParams) => string);
49
+ _active?: boolean | ((value: Maybe<TValue>, ...args: TParams) => boolean);
50
+ _type: string;
51
+ _patched: boolean;
52
+ _params?: RegleUniversalParams<TParams>;
53
+ }
54
+ declare enum InternalRuleType {
55
+ Inline = "__inline",
56
+ Async = "__async"
57
+ }
58
+
59
+ /**
60
+ * Returned typed of rules created with `createRule`
61
+ * */
62
+ interface RegleRuleDefinition<TValue extends any = any, TParams extends any[] = []> extends RegleInternalRuleDefs<TValue, TParams> {
63
+ validator: RegleRuleDefinitionProcessor<TValue, TParams, boolean | Promise<boolean>>;
64
+ message: RegleRuleDefinitionProcessor<TValue, TParams, string>;
65
+ active: RegleRuleDefinitionProcessor<TValue, TParams, boolean>;
66
+ type: string;
67
+ }
68
+ /**
69
+ * Rules with params created with `createRules` are callable while being customizable
70
+ */
71
+ interface RegleRuleWithParamsDefinition<TValue extends any = any, TParams extends any[] = []> extends RegleRuleInit<TValue, TParams>, RegleInternalRuleDefs<TValue, TParams> {
72
+ (...params: RegleUniversalParams<TParams>): RegleRuleDefinition<TValue, TParams>;
73
+ }
74
+ /**
75
+ * Generic types for a created RegleRule
76
+ */
77
+ type RegleRuleRaw<TValue extends any = any, TParams extends any[] = []> = RegleRuleDefinition<TValue, TParams> | RegleRuleWithParamsDefinition<TValue, TParams>;
78
+ /**
79
+ * Process the type of a created rule with `createRule`.
80
+ * For a rule with params it will return a function
81
+ * Otherwise it will return the rule definition
82
+ */
83
+ type InferRegleRule<TValue extends any = any, TParams extends any[] = []> = [
84
+ TParams
85
+ ] extends [[]] ? RegleRuleDefinition<TValue, TParams> : RegleRuleWithParamsDefinition<TValue, TParams>;
86
+ type RegleRuleDefinitionProcessor<TValue extends any = any, TParams extends any[] = [], TReturn = any> = (value: Maybe<TValue>, ...args: TParams) => TReturn;
87
+ type RegleCollectionRuleDefinition<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
88
+ $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
89
+ }) | {
90
+ $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
91
+ };
92
+
93
+ type CustomRulesDeclarationTree = Record<string, RegleRuleRaw<any, any>>;
94
+ type AllRulesDeclarations = CustomRulesDeclarationTree & DefaultValidators;
95
+
96
+ type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
97
+ [TKey in keyof TForm]?: RegleFormPropertyType<TForm[TKey], TCustomRules>;
98
+ };
99
+ type RegleFormPropertyType<TValue = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = [NonNullable<TValue>] extends [never] ? RegleRuleDecl<unknown, TCustomRules> : NonNullable<TValue> extends Array<any> ? RegleCollectionRuleDecl<TValue, TCustomRules> : NonNullable<TValue> extends Date ? RegleRuleDecl<NonNullable<TValue>, TCustomRules> : NonNullable<TValue> extends File ? RegleRuleDecl<NonNullable<TValue>, TCustomRules> : NonNullable<TValue> extends Record<string, any> ? ReglePartialValidationTree<NonNullable<TValue>, TCustomRules> : RegleRuleDecl<NonNullable<TValue>, TCustomRules>;
100
+ /**
101
+ * Rule tree for a form property
102
+ */
103
+ type RegleRuleDecl<TValue extends any = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
104
+ [TKey in keyof TCustomRules]?: TCustomRules[TKey] extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, TParams> : FormRuleDeclaration<TValue, any>;
105
+ };
106
+ type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
107
+ $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
108
+ }) | {
109
+ $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
110
+ };
111
+ /**
112
+ * TODO async
113
+ */
114
+ type InlineRuleDeclaration<TValue extends any> = (value: Maybe<TValue>, ...args: any[]) => boolean | Promise<boolean>;
115
+ /**
116
+ * Regroup inline and registered rules
117
+ * */
118
+ type FormRuleDeclaration<TValue extends any, TParams extends any[] = []> = InlineRuleDeclaration<TValue> | RegleRuleDefinition<TValue, TParams>;
119
+
120
+ type RegleErrorTree<TRules extends ReglePartialValidationTree<any, any>> = {
121
+ readonly [K in keyof TRules]: RegleValidationErrors<TRules[K]>;
122
+ };
123
+ type RegleValidationErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleRuleDecl<any, any> ? string[] : TRule extends ReglePartialValidationTree<any, any> ? RegleErrorTree<TRule> : TRule extends RegleCollectionRuleDefinition ? RegleCollectionErrors<TRule['$each']> : string[];
124
+ type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = {
125
+ readonly $errors: string[];
126
+ readonly $each: RegleValidationErrors<TRule>[];
127
+ };
128
+ type PossibleRegleErrors = RegleCollectionErrors<any> | string[] | RegleErrorTree<any>;
129
+ type DataType = string | number | Record<string, any> | File | Array<any> | Date | null | undefined;
130
+
131
+ interface RegleStatus<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState>> extends RegleCommonStatus<TState> {
132
+ readonly $fields: {
133
+ readonly [TKey in keyof TRules]: InferRegleStatusType<NonNullable<TRules[TKey]>, TState, TKey>;
134
+ };
135
+ }
136
+ type InferRegleStatusType<TRule extends RegleCollectionRuleDecl | RegleRuleDecl | ReglePartialValidationTree<any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> = TRule extends RegleCollectionRuleDefinition<any, any> ? RegleCollectionStatus<TRule['$each'], TState[TKey]> : TRule extends ReglePartialValidationTree<any> ? TState[TKey] extends Array<any> ? RegleCommonStatus<TState[TKey]> : RegleStatus<TState[TKey], TRule> : RegleFieldStatus<TRule, TState, TKey>;
137
+ interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, AllRulesDeclarations>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus<TState[TKey]> {
138
+ readonly $rules: {
139
+ readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
140
+ };
141
+ }
142
+ interface RegleCommonStatus<TValue = any> {
143
+ readonly $valid: boolean;
144
+ readonly $invalid: boolean;
145
+ readonly $dirty: boolean;
146
+ readonly $anyDirty: boolean;
147
+ readonly $pending: boolean;
148
+ $value: TValue;
149
+ readonly $error: boolean;
150
+ $touch: () => void;
151
+ $reset: () => void;
152
+ }
153
+ interface RegleSoftRuleStatus<TValue = any, TParams extends any[] = any[]> {
154
+ readonly $type: string;
155
+ readonly $message: string;
156
+ readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
157
+ readonly $active: boolean;
158
+ readonly $valid: boolean;
159
+ readonly $pending: boolean;
160
+ readonly $params?: TParams;
161
+ }
162
+ type RegleRuleStatus<TValue = any, TParams extends any[] = any[]> = {
163
+ readonly $type: string;
164
+ readonly $message: string;
165
+ readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
166
+ readonly $active: boolean;
167
+ readonly $valid: boolean;
168
+ readonly $pending: boolean;
169
+ } & ([TParams] extends [[]] ? {} : {
170
+ readonly $params: TParams;
171
+ });
172
+ interface RegleCollectionStatus<TRules extends RegleRuleDecl | ReglePartialValidationTree<any>, TState extends any[]> extends RegleFieldStatus<TRules, TState> {
173
+ readonly $each: Array<InferRegleStatusType<NonNullable<TRules>, TState, number>>;
174
+ }
175
+ type PossibleRegleStatus = RegleStatus<any, any> | RegleFieldStatus<any> | RegleSoftRuleStatus | RegleCollectionStatus<any, any> | RegleCommonStatus<any>;
176
+ type PossibleRegleFieldStatus = RegleFieldStatus<any, any> | RegleStatus<any, any>;
177
+
178
+ declare function createRule<TValue extends any, TParams extends any[] = []>(definition: RegleRuleInit<TValue, TParams>): InferRegleRule<TValue, TParams>;
179
+
180
+ /**
181
+ * Root function that allows you to define project-wise all your custom validators or overwrite default ones
182
+ *
183
+ * It will return utility functions that let you build type-safe forms
184
+ *
185
+ * @param customRules
186
+ */
187
+ declare function defineCustomValidators<TCustomRules extends Partial<AllRulesDeclarations>>(customRules: () => TCustomRules): {
188
+ useRegle: <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree & {
189
+ maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
190
+ required: RegleRuleDefinition<unknown, []>;
191
+ requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
192
+ } & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules)) => {
193
+ state: vue.Ref<TState>;
194
+ $regle: vue.Ref<RegleStatus<TState, TRules>>;
195
+ errors: RegleErrorTree<TRules>;
196
+ };
197
+ };
198
+
199
+ declare function withMessage<TValue extends any>(rule: InlineRuleDeclaration<TValue>, newMessage: string | ((value: TValue) => string)): RegleRuleDefinition<TValue>;
200
+ declare function withMessage<TValue extends any, TParams extends any[]>(rule: RegleRuleWithParamsDefinition<TValue, TParams>, newMessage: string | ((value: TValue, ...args: TParams) => string)): RegleRuleWithParamsDefinition<TValue, TParams>;
201
+ declare function withMessage<TValue extends any, TParams extends any[]>(rule: RegleRuleDefinition<TValue, TParams>, newMessage: string | ((value: TValue, ...args: TParams) => string)): RegleRuleDefinition<TValue, TParams>;
202
+
203
+ declare function withAsync<TParams extends (Ref<unknown> | (() => unknown))[]>(rule: InlineRuleDeclaration<any>, depsArray: [...TParams]): RegleRuleDefinition<any>;
204
+
205
+ declare function isEmpty(value: unknown): value is null | undefined;
206
+
207
+ declare const required: RegleRuleDefinition<unknown, []>;
208
+
209
+ declare const maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
210
+
211
+ declare const requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
212
+
213
+ export { AllRulesDeclarations, CustomRulesDeclarationTree, DataType, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, ParamDecl, PossibleRegleErrors, PossibleRegleFieldStatus, PossibleRegleStatus, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleSoftRuleStatus, RegleStatus, RegleUniversalParams, RegleValidationErrors, UnwrapRegleUniversalParams, createRule, defineCustomValidators, isEmpty, maxLength, required, requiredIf, withAsync, withMessage };