@regle/core 0.0.3 → 0.0.5-beta.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/index.d.cts CHANGED
@@ -1,19 +1,26 @@
1
1
  import * as vue from 'vue';
2
- import { MaybeRef } from 'vue';
2
+ import { MaybeRef, Ref } from 'vue';
3
+ import { PartialDeep } from 'type-fest';
3
4
 
4
5
  type ArrayElement<T> = T extends Array<infer U> ? U : never;
6
+ type ExcludeFromTuple<T extends readonly any[], E> = T extends [infer F, ...infer R] ? [NonNullable<F>] extends [E] ? ExcludeFromTuple<R, E> : [Exclude<F, E>, ...ExcludeFromTuple<R, E>] : [];
7
+ type UnrefTuple<T extends readonly any[]> = T extends [infer F, ...infer R] ? [NonNullable<F>] extends [MaybeRef<infer U>] ? [U, ...UnrefTuple<R>] : [F, ...UnrefTuple<R>] : [];
5
8
 
6
9
  type Maybe<T> = T | null | undefined;
10
+ type MaybeNull<T> = T | null;
11
+ type DeepMaybeRef<T extends Record<string, any>> = {
12
+ [K in keyof T]: MaybeRef<T[K]>;
13
+ };
7
14
 
8
15
  /**
9
16
  * @argument
10
17
  * createRule arguments options
11
18
  */
12
- interface RegleRuleInit<TValue extends any, TParams extends any[] = []> {
19
+ interface RegleRuleInit<TValue extends any, TParams extends any[] = [], TType extends string = string> {
13
20
  validator: (value: Maybe<TValue>, ...args: TParams) => boolean | Promise<boolean>;
14
21
  message: string | ((value: Maybe<TValue>, ...args: TParams) => string);
15
22
  active?: boolean | ((value: Maybe<TValue>, ...args: TParams) => boolean);
16
- type: string;
23
+ type: TType;
17
24
  }
18
25
 
19
26
  type DefaultValidators = {
@@ -49,6 +56,7 @@ interface RegleInternalRuleDefs<TValue extends any = any, TParams extends any[]
49
56
  _type: string;
50
57
  _patched: boolean;
51
58
  _params?: RegleUniversalParams<TParams>;
59
+ _async: boolean;
52
60
  }
53
61
  declare enum InternalRuleType {
54
62
  Inline = "__inline",
@@ -83,35 +91,73 @@ type InferRegleRule<TValue extends any = any, TParams extends any[] = []> = [
83
91
  TParams
84
92
  ] extends [[]] ? RegleRuleDefinition<TValue, TParams> : RegleRuleWithParamsDefinition<TValue, TParams>;
85
93
  type RegleRuleDefinitionProcessor<TValue extends any = any, TParams extends any[] = [], TReturn = any> = (value: Maybe<TValue>, ...args: TParams) => TReturn;
86
- type RegleCollectionRuleDefinition<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
94
+ type RegleCollectionRuleDefinition<TValue = any[], TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
87
95
  $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
88
96
  }) | {
89
97
  $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
90
98
  };
91
99
 
92
- type CustomRulesDeclarationTree = Record<string, RegleRuleRaw<any, any>>;
100
+ type CustomRulesDeclarationTree = {
101
+ [x: string]: RegleRuleRaw<any, any> | undefined;
102
+ };
93
103
  type AllRulesDeclarations = CustomRulesDeclarationTree & DefaultValidators;
94
104
 
95
- type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
105
+ /**
106
+ * @public
107
+ */
108
+ type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
96
109
  [TKey in keyof TForm]?: RegleFormPropertyType<TForm[TKey], TCustomRules>;
97
110
  };
98
- 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>;
99
111
  /**
112
+ * @internal
113
+ * @reference {@link ReglePartialValidationTree}
114
+ */
115
+ type $InternalReglePartialValidationTree = {
116
+ [x: string]: $InternalFormPropertyTypes;
117
+ };
118
+ /**
119
+ * @public
120
+ */
121
+ type RegleFormPropertyType<TValue = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<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>;
122
+ /**
123
+ * @internal
124
+ * @reference {@link RegleFormPropertyType}
125
+ */
126
+ type $InternalFormPropertyTypes = $InternalRegleRuleDecl | $InternalRegleCollectionRuleDecl | $InternalReglePartialValidationTree;
127
+ /**
128
+ * @public
100
129
  * Rule tree for a form property
101
130
  */
102
- type RegleRuleDecl<TValue extends any = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
131
+ type RegleRuleDecl<TValue extends any = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
103
132
  [TKey in keyof TCustomRules]?: TCustomRules[TKey] extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, TParams> : FormRuleDeclaration<TValue, any>;
104
133
  };
105
- type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
134
+ /**
135
+ * @internal
136
+ * @reference {@link RegleRuleDecl}
137
+ */
138
+ type $InternalRegleRuleDecl = Record<string, FormRuleDeclaration<any, any>>;
139
+ /**
140
+ * @public
141
+ */
142
+ type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
106
143
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
107
144
  }) | {
108
145
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
109
146
  };
110
147
  /**
111
- * TODO async
148
+ * @internal
149
+ * @reference {@link RegleCollectionRuleDecl}
150
+ *
112
151
  */
113
- type InlineRuleDeclaration<TValue extends any> = (value: Maybe<TValue>, ...args: any[]) => boolean | Promise<boolean>;
152
+ type $InternalRegleCollectionRuleDecl = $InternalRegleRuleDecl & {
153
+ $each?: $InternalFormPropertyTypes;
154
+ };
114
155
  /**
156
+ * @public
157
+ */
158
+ type InlineRuleDeclaration<TValue extends any = any> = (value: Maybe<TValue>, ...args: any[]) => boolean | Promise<boolean>;
159
+ /**
160
+ * @public
115
161
  * Regroup inline and registered rules
116
162
  * */
117
163
  type FormRuleDeclaration<TValue extends any, TParams extends any[] = []> = InlineRuleDeclaration<TValue> | RegleRuleDefinition<TValue, TParams>;
@@ -119,7 +165,7 @@ type FormRuleDeclaration<TValue extends any, TParams extends any[] = []> = Inlin
119
165
  type RegleErrorTree<TRules extends ReglePartialValidationTree<any, any>> = {
120
166
  readonly [K in keyof TRules]: RegleValidationErrors<TRules[K]>;
121
167
  };
122
- 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[];
168
+ type RegleValidationErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleCollectionRuleDefinition ? RegleCollectionErrors<TRule['$each']> : TRule extends ReglePartialValidationTree<any, any> ? RegleErrorTree<TRule> : TRule extends RegleRuleDecl<any, any> ? string[] : string[];
123
169
  type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = {
124
170
  readonly $errors: string[];
125
171
  readonly $each: RegleValidationErrors<TRule>[];
@@ -127,54 +173,161 @@ type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undef
127
173
  type PossibleRegleErrors = RegleCollectionErrors<any> | string[] | RegleErrorTree<any>;
128
174
  type DataType = string | number | Record<string, any> | File | Array<any> | Date | null | undefined;
129
175
 
130
- interface RegleStatus<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState>> extends RegleCommonStatus<TState> {
176
+ /**
177
+ * @public
178
+ */
179
+ interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus<TState> {
131
180
  readonly $fields: {
132
181
  readonly [TKey in keyof TRules]: InferRegleStatusType<NonNullable<TRules[TKey]>, TState, TKey>;
133
182
  };
134
183
  }
184
+ /**
185
+ * @internal
186
+ * @reference {@link RegleStatus}
187
+ */
188
+ interface $InternalRegleStatus extends RegleCommonStatus {
189
+ $fields: {
190
+ [x: string]: $InternalRegleStatusType;
191
+ };
192
+ }
193
+ /**
194
+ * @public
195
+ */
135
196
  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>;
136
- interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, AllRulesDeclarations>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus<TState[TKey]> {
197
+ /**
198
+ * @internal
199
+ * @reference {@link InferRegleStatusType}
200
+ */
201
+ type $InternalRegleStatusType = $InternalRegleCollectionStatus | RegleCommonStatus | $InternalRegleStatus | $InternalRegleFieldStatus;
202
+ /**
203
+ * @public
204
+ */
205
+ interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, Partial<AllRulesDeclarations>> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus<TState> {
206
+ $value: TState[TKey];
137
207
  readonly $rules: {
138
208
  readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
139
209
  };
140
210
  }
211
+ /**
212
+ * @internal
213
+ * @reference {@link RegleFieldStatus}
214
+ */
215
+ interface $InternalRegleFieldStatus extends RegleCommonStatus {
216
+ $value: any;
217
+ $rules: Record<string, $InternalRegleRuleStatus>;
218
+ }
219
+ /**
220
+ * @public
221
+ */
141
222
  interface RegleCommonStatus<TValue = any> {
142
223
  readonly $valid: boolean;
143
224
  readonly $invalid: boolean;
144
225
  readonly $dirty: boolean;
145
226
  readonly $anyDirty: boolean;
146
227
  readonly $pending: boolean;
147
- $value: TValue;
148
228
  readonly $error: boolean;
149
- $touch: () => void;
150
- $reset: () => void;
229
+ $id?: string;
230
+ $value: TValue;
231
+ $touch(): void;
232
+ $reset(): void;
233
+ $validate(): Promise<boolean>;
234
+ $unwatch(): void;
235
+ $watch(): void;
151
236
  }
152
- interface RegleSoftRuleStatus<TValue = any, TParams extends any[] = any[]> {
237
+ /**
238
+ * @public
239
+ */
240
+ type RegleRuleStatus<TValue = any, TParams extends any[] = any[]> = {
153
241
  readonly $type: string;
154
242
  readonly $message: string;
155
- readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
156
243
  readonly $active: boolean;
157
244
  readonly $valid: boolean;
158
245
  readonly $pending: boolean;
159
- readonly $params?: TParams;
160
- }
161
- type RegleRuleStatus<TValue = any, TParams extends any[] = any[]> = {
246
+ readonly $path: string;
247
+ $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
248
+ $validate(): Promise<boolean>;
249
+ } & ([TParams] extends [[]] ? {} : {
250
+ readonly $params: TParams;
251
+ });
252
+ /**
253
+ * @internal
254
+ * @reference {@link RegleRuleStatus}
255
+ */
256
+ interface $InternalRegleRuleStatus {
162
257
  readonly $type: string;
163
258
  readonly $message: string;
164
- readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
165
259
  readonly $active: boolean;
166
260
  readonly $valid: boolean;
167
261
  readonly $pending: boolean;
168
- } & ([TParams] extends [[]] ? {} : {
169
- readonly $params: TParams;
170
- });
262
+ readonly $path: string;
263
+ $validator(value: any, ...args: any[]): boolean | Promise<boolean>;
264
+ $validate(): Promise<boolean>;
265
+ $unwatch(): void;
266
+ $watch(): void;
267
+ readonly $params?: any[];
268
+ }
269
+ /**
270
+ * @public
271
+ */
171
272
  interface RegleCollectionStatus<TRules extends RegleRuleDecl | ReglePartialValidationTree<any>, TState extends any[]> extends RegleFieldStatus<TRules, TState> {
172
273
  readonly $each: Array<InferRegleStatusType<NonNullable<TRules>, TState, number>>;
173
274
  }
174
- type PossibleRegleStatus = RegleStatus<any, any> | RegleFieldStatus<any> | RegleSoftRuleStatus | RegleCollectionStatus<any, any> | RegleCommonStatus<any>;
175
- type PossibleRegleFieldStatus = RegleFieldStatus<any, any> | RegleStatus<any, any>;
275
+ /**
276
+ * @internal
277
+ * @reference {@link RegleCollectionStatus}
278
+ */
279
+ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fields'> {
280
+ $each: Array<$InternalRegleStatusType>;
281
+ $rules?: Record<string, $InternalRegleRuleStatus>;
282
+ /** Track each array state */
283
+ $unwatch(): void;
284
+ $watch(): void;
285
+ $fields?: {
286
+ [x: string]: $InternalRegleStatusType;
287
+ };
288
+ }
176
289
 
177
- declare function createRule<TValue extends any, TParams extends any[] = []>(definition: RegleRuleInit<TValue, TParams>): InferRegleRule<TValue, TParams>;
290
+ interface Regle<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> {
291
+ state: Ref<PartialDeep<TState>>;
292
+ $regle: RegleStatus<TState, TRules>;
293
+ errors: RegleErrorTree<TRules>;
294
+ resetForm: () => void;
295
+ validateForm: () => Promise<false | DeepSafeFormState<TState, TRules>>;
296
+ }
297
+ type DeepSafeFormState<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> = {
298
+ [K in keyof TState as [SafeProperty<TState[K], TRules[K]>] extends [never] ? K : never]?: [
299
+ SafeProperty<TState[K], TRules[K]>
300
+ ] extends [never] ? TState[K] : SafeProperty<TState[K], TRules[K]>;
301
+ } & {
302
+ [K in keyof TState as [SafeProperty<TState[K], TRules[K]>] extends [never] ? never : K]-?: SafeProperty<TState[K], TRules[K]>;
303
+ };
304
+ type SafeProperty<TState, TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleCollectionRuleDefinition ? TState extends Array<any> ? SafeProperty<TState[number], TRule['$each']>[] : never : TRule extends ReglePartialValidationTree<any, any> ? TState extends Record<string, any> ? DeepSafeFormState<TState, TRule> : never : TRule extends RegleRuleDecl<any, any> ? unknown extends TRule['required'] ? never : TRule['required'] extends undefined ? never : TRule['required'] extends RegleRuleDefinition<any, infer Params> ? Params extends never[] ? TState : never : never : never;
305
+
306
+ interface RegleBehaviourOptions {
307
+ /**
308
+ * Only display error when calling `validateForm()`
309
+ * @default false
310
+ */
311
+ lazy?: boolean;
312
+ /**
313
+ * Automaticaly set the dirty set without the need of `$value` or `$touch`
314
+ * @default true
315
+ */
316
+ autoDirty?: boolean;
317
+ /**
318
+ * The fields will turn valid when they are, but not invalid unless calling `validateForm()`
319
+ * @default false
320
+ */
321
+ rewardEarly?: boolean;
322
+ }
323
+
324
+ declare function createRule<TValue extends any, TParams extends any[] = [], TType extends string = string>(definition: RegleRuleInit<TValue, TParams, TType>): InferRegleRule<TValue, TParams>;
325
+
326
+ /**
327
+ * Returns a clean list of parameters
328
+ * Removing Ref and executing function to return the unwraped value
329
+ */
330
+ declare function unwrapRuleParameters<TParams extends any[]>(params: ParamDecl[]): TParams;
178
331
 
179
332
  /**
180
333
  * Root function that allows you to define project-wise all your custom validators or overwrite default ones
@@ -183,12 +336,9 @@ declare function createRule<TValue extends any, TParams extends any[] = []>(defi
183
336
  *
184
337
  * @param customRules
185
338
  */
186
- declare function defineCustomValidators<TCustomRules extends Partial<AllRulesDeclarations>>(customRules: () => TCustomRules): {
187
- useRegle: <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree & DefaultValidators & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules)) => {
188
- state: vue.Ref<TState>;
189
- $regle: vue.Ref<RegleStatus<TState, TRules>>;
190
- errors: RegleErrorTree<TRules>;
191
- };
192
- };
339
+ declare function defineRegleOptions<TCustomRules extends Partial<AllRulesDeclarations>>({ rules, options, }: {
340
+ rules?: () => TCustomRules;
341
+ options?: RegleBehaviourOptions;
342
+ }): <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, Partial<AllRulesDeclarations> & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules), options?: Partial<DeepMaybeRef<Required<RegleBehaviourOptions>>> | undefined) => Regle<TState, TRules>;
193
343
 
194
- 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 };
344
+ export { $InternalFormPropertyTypes, $InternalRegleCollectionRuleDecl, $InternalRegleCollectionStatus, $InternalRegleFieldStatus, $InternalReglePartialValidationTree, $InternalRegleRuleDecl, $InternalRegleRuleStatus, $InternalRegleStatus, $InternalRegleStatusType, AllRulesDeclarations, ArrayElement, CustomRulesDeclarationTree, DataType, DeepMaybeRef, ExcludeFromTuple, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, Maybe, MaybeNull, ParamDecl, PossibleRegleErrors, Regle, RegleBehaviourOptions, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleStatus, RegleUniversalParams, RegleValidationErrors, UnrefTuple, UnwrapRegleUniversalParams, createRule, defineRegleOptions, unwrapRuleParameters };
package/dist/index.d.ts CHANGED
@@ -1,19 +1,26 @@
1
1
  import * as vue from 'vue';
2
- import { MaybeRef } from 'vue';
2
+ import { MaybeRef, Ref } from 'vue';
3
+ import { PartialDeep } from 'type-fest';
3
4
 
4
5
  type ArrayElement<T> = T extends Array<infer U> ? U : never;
6
+ type ExcludeFromTuple<T extends readonly any[], E> = T extends [infer F, ...infer R] ? [NonNullable<F>] extends [E] ? ExcludeFromTuple<R, E> : [Exclude<F, E>, ...ExcludeFromTuple<R, E>] : [];
7
+ type UnrefTuple<T extends readonly any[]> = T extends [infer F, ...infer R] ? [NonNullable<F>] extends [MaybeRef<infer U>] ? [U, ...UnrefTuple<R>] : [F, ...UnrefTuple<R>] : [];
5
8
 
6
9
  type Maybe<T> = T | null | undefined;
10
+ type MaybeNull<T> = T | null;
11
+ type DeepMaybeRef<T extends Record<string, any>> = {
12
+ [K in keyof T]: MaybeRef<T[K]>;
13
+ };
7
14
 
8
15
  /**
9
16
  * @argument
10
17
  * createRule arguments options
11
18
  */
12
- interface RegleRuleInit<TValue extends any, TParams extends any[] = []> {
19
+ interface RegleRuleInit<TValue extends any, TParams extends any[] = [], TType extends string = string> {
13
20
  validator: (value: Maybe<TValue>, ...args: TParams) => boolean | Promise<boolean>;
14
21
  message: string | ((value: Maybe<TValue>, ...args: TParams) => string);
15
22
  active?: boolean | ((value: Maybe<TValue>, ...args: TParams) => boolean);
16
- type: string;
23
+ type: TType;
17
24
  }
18
25
 
19
26
  type DefaultValidators = {
@@ -49,6 +56,7 @@ interface RegleInternalRuleDefs<TValue extends any = any, TParams extends any[]
49
56
  _type: string;
50
57
  _patched: boolean;
51
58
  _params?: RegleUniversalParams<TParams>;
59
+ _async: boolean;
52
60
  }
53
61
  declare enum InternalRuleType {
54
62
  Inline = "__inline",
@@ -83,35 +91,73 @@ type InferRegleRule<TValue extends any = any, TParams extends any[] = []> = [
83
91
  TParams
84
92
  ] extends [[]] ? RegleRuleDefinition<TValue, TParams> : RegleRuleWithParamsDefinition<TValue, TParams>;
85
93
  type RegleRuleDefinitionProcessor<TValue extends any = any, TParams extends any[] = [], TReturn = any> = (value: Maybe<TValue>, ...args: TParams) => TReturn;
86
- type RegleCollectionRuleDefinition<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
94
+ type RegleCollectionRuleDefinition<TValue = any[], TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
87
95
  $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
88
96
  }) | {
89
97
  $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
90
98
  };
91
99
 
92
- type CustomRulesDeclarationTree = Record<string, RegleRuleRaw<any, any>>;
100
+ type CustomRulesDeclarationTree = {
101
+ [x: string]: RegleRuleRaw<any, any> | undefined;
102
+ };
93
103
  type AllRulesDeclarations = CustomRulesDeclarationTree & DefaultValidators;
94
104
 
95
- type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
105
+ /**
106
+ * @public
107
+ */
108
+ type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
96
109
  [TKey in keyof TForm]?: RegleFormPropertyType<TForm[TKey], TCustomRules>;
97
110
  };
98
- 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>;
99
111
  /**
112
+ * @internal
113
+ * @reference {@link ReglePartialValidationTree}
114
+ */
115
+ type $InternalReglePartialValidationTree = {
116
+ [x: string]: $InternalFormPropertyTypes;
117
+ };
118
+ /**
119
+ * @public
120
+ */
121
+ type RegleFormPropertyType<TValue = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<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>;
122
+ /**
123
+ * @internal
124
+ * @reference {@link RegleFormPropertyType}
125
+ */
126
+ type $InternalFormPropertyTypes = $InternalRegleRuleDecl | $InternalRegleCollectionRuleDecl | $InternalReglePartialValidationTree;
127
+ /**
128
+ * @public
100
129
  * Rule tree for a form property
101
130
  */
102
- type RegleRuleDecl<TValue extends any = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
131
+ type RegleRuleDecl<TValue extends any = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
103
132
  [TKey in keyof TCustomRules]?: TCustomRules[TKey] extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, TParams> : FormRuleDeclaration<TValue, any>;
104
133
  };
105
- type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
134
+ /**
135
+ * @internal
136
+ * @reference {@link RegleRuleDecl}
137
+ */
138
+ type $InternalRegleRuleDecl = Record<string, FormRuleDeclaration<any, any>>;
139
+ /**
140
+ * @public
141
+ */
142
+ type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
106
143
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
107
144
  }) | {
108
145
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
109
146
  };
110
147
  /**
111
- * TODO async
148
+ * @internal
149
+ * @reference {@link RegleCollectionRuleDecl}
150
+ *
112
151
  */
113
- type InlineRuleDeclaration<TValue extends any> = (value: Maybe<TValue>, ...args: any[]) => boolean | Promise<boolean>;
152
+ type $InternalRegleCollectionRuleDecl = $InternalRegleRuleDecl & {
153
+ $each?: $InternalFormPropertyTypes;
154
+ };
114
155
  /**
156
+ * @public
157
+ */
158
+ type InlineRuleDeclaration<TValue extends any = any> = (value: Maybe<TValue>, ...args: any[]) => boolean | Promise<boolean>;
159
+ /**
160
+ * @public
115
161
  * Regroup inline and registered rules
116
162
  * */
117
163
  type FormRuleDeclaration<TValue extends any, TParams extends any[] = []> = InlineRuleDeclaration<TValue> | RegleRuleDefinition<TValue, TParams>;
@@ -119,7 +165,7 @@ type FormRuleDeclaration<TValue extends any, TParams extends any[] = []> = Inlin
119
165
  type RegleErrorTree<TRules extends ReglePartialValidationTree<any, any>> = {
120
166
  readonly [K in keyof TRules]: RegleValidationErrors<TRules[K]>;
121
167
  };
122
- 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[];
168
+ type RegleValidationErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleCollectionRuleDefinition ? RegleCollectionErrors<TRule['$each']> : TRule extends ReglePartialValidationTree<any, any> ? RegleErrorTree<TRule> : TRule extends RegleRuleDecl<any, any> ? string[] : string[];
123
169
  type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = {
124
170
  readonly $errors: string[];
125
171
  readonly $each: RegleValidationErrors<TRule>[];
@@ -127,54 +173,161 @@ type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undef
127
173
  type PossibleRegleErrors = RegleCollectionErrors<any> | string[] | RegleErrorTree<any>;
128
174
  type DataType = string | number | Record<string, any> | File | Array<any> | Date | null | undefined;
129
175
 
130
- interface RegleStatus<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState>> extends RegleCommonStatus<TState> {
176
+ /**
177
+ * @public
178
+ */
179
+ interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus<TState> {
131
180
  readonly $fields: {
132
181
  readonly [TKey in keyof TRules]: InferRegleStatusType<NonNullable<TRules[TKey]>, TState, TKey>;
133
182
  };
134
183
  }
184
+ /**
185
+ * @internal
186
+ * @reference {@link RegleStatus}
187
+ */
188
+ interface $InternalRegleStatus extends RegleCommonStatus {
189
+ $fields: {
190
+ [x: string]: $InternalRegleStatusType;
191
+ };
192
+ }
193
+ /**
194
+ * @public
195
+ */
135
196
  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>;
136
- interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, AllRulesDeclarations>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus<TState[TKey]> {
197
+ /**
198
+ * @internal
199
+ * @reference {@link InferRegleStatusType}
200
+ */
201
+ type $InternalRegleStatusType = $InternalRegleCollectionStatus | RegleCommonStatus | $InternalRegleStatus | $InternalRegleFieldStatus;
202
+ /**
203
+ * @public
204
+ */
205
+ interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, Partial<AllRulesDeclarations>> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus<TState> {
206
+ $value: TState[TKey];
137
207
  readonly $rules: {
138
208
  readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
139
209
  };
140
210
  }
211
+ /**
212
+ * @internal
213
+ * @reference {@link RegleFieldStatus}
214
+ */
215
+ interface $InternalRegleFieldStatus extends RegleCommonStatus {
216
+ $value: any;
217
+ $rules: Record<string, $InternalRegleRuleStatus>;
218
+ }
219
+ /**
220
+ * @public
221
+ */
141
222
  interface RegleCommonStatus<TValue = any> {
142
223
  readonly $valid: boolean;
143
224
  readonly $invalid: boolean;
144
225
  readonly $dirty: boolean;
145
226
  readonly $anyDirty: boolean;
146
227
  readonly $pending: boolean;
147
- $value: TValue;
148
228
  readonly $error: boolean;
149
- $touch: () => void;
150
- $reset: () => void;
229
+ $id?: string;
230
+ $value: TValue;
231
+ $touch(): void;
232
+ $reset(): void;
233
+ $validate(): Promise<boolean>;
234
+ $unwatch(): void;
235
+ $watch(): void;
151
236
  }
152
- interface RegleSoftRuleStatus<TValue = any, TParams extends any[] = any[]> {
237
+ /**
238
+ * @public
239
+ */
240
+ type RegleRuleStatus<TValue = any, TParams extends any[] = any[]> = {
153
241
  readonly $type: string;
154
242
  readonly $message: string;
155
- readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
156
243
  readonly $active: boolean;
157
244
  readonly $valid: boolean;
158
245
  readonly $pending: boolean;
159
- readonly $params?: TParams;
160
- }
161
- type RegleRuleStatus<TValue = any, TParams extends any[] = any[]> = {
246
+ readonly $path: string;
247
+ $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
248
+ $validate(): Promise<boolean>;
249
+ } & ([TParams] extends [[]] ? {} : {
250
+ readonly $params: TParams;
251
+ });
252
+ /**
253
+ * @internal
254
+ * @reference {@link RegleRuleStatus}
255
+ */
256
+ interface $InternalRegleRuleStatus {
162
257
  readonly $type: string;
163
258
  readonly $message: string;
164
- readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
165
259
  readonly $active: boolean;
166
260
  readonly $valid: boolean;
167
261
  readonly $pending: boolean;
168
- } & ([TParams] extends [[]] ? {} : {
169
- readonly $params: TParams;
170
- });
262
+ readonly $path: string;
263
+ $validator(value: any, ...args: any[]): boolean | Promise<boolean>;
264
+ $validate(): Promise<boolean>;
265
+ $unwatch(): void;
266
+ $watch(): void;
267
+ readonly $params?: any[];
268
+ }
269
+ /**
270
+ * @public
271
+ */
171
272
  interface RegleCollectionStatus<TRules extends RegleRuleDecl | ReglePartialValidationTree<any>, TState extends any[]> extends RegleFieldStatus<TRules, TState> {
172
273
  readonly $each: Array<InferRegleStatusType<NonNullable<TRules>, TState, number>>;
173
274
  }
174
- type PossibleRegleStatus = RegleStatus<any, any> | RegleFieldStatus<any> | RegleSoftRuleStatus | RegleCollectionStatus<any, any> | RegleCommonStatus<any>;
175
- type PossibleRegleFieldStatus = RegleFieldStatus<any, any> | RegleStatus<any, any>;
275
+ /**
276
+ * @internal
277
+ * @reference {@link RegleCollectionStatus}
278
+ */
279
+ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fields'> {
280
+ $each: Array<$InternalRegleStatusType>;
281
+ $rules?: Record<string, $InternalRegleRuleStatus>;
282
+ /** Track each array state */
283
+ $unwatch(): void;
284
+ $watch(): void;
285
+ $fields?: {
286
+ [x: string]: $InternalRegleStatusType;
287
+ };
288
+ }
176
289
 
177
- declare function createRule<TValue extends any, TParams extends any[] = []>(definition: RegleRuleInit<TValue, TParams>): InferRegleRule<TValue, TParams>;
290
+ interface Regle<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> {
291
+ state: Ref<PartialDeep<TState>>;
292
+ $regle: RegleStatus<TState, TRules>;
293
+ errors: RegleErrorTree<TRules>;
294
+ resetForm: () => void;
295
+ validateForm: () => Promise<false | DeepSafeFormState<TState, TRules>>;
296
+ }
297
+ type DeepSafeFormState<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> = {
298
+ [K in keyof TState as [SafeProperty<TState[K], TRules[K]>] extends [never] ? K : never]?: [
299
+ SafeProperty<TState[K], TRules[K]>
300
+ ] extends [never] ? TState[K] : SafeProperty<TState[K], TRules[K]>;
301
+ } & {
302
+ [K in keyof TState as [SafeProperty<TState[K], TRules[K]>] extends [never] ? never : K]-?: SafeProperty<TState[K], TRules[K]>;
303
+ };
304
+ type SafeProperty<TState, TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleCollectionRuleDefinition ? TState extends Array<any> ? SafeProperty<TState[number], TRule['$each']>[] : never : TRule extends ReglePartialValidationTree<any, any> ? TState extends Record<string, any> ? DeepSafeFormState<TState, TRule> : never : TRule extends RegleRuleDecl<any, any> ? unknown extends TRule['required'] ? never : TRule['required'] extends undefined ? never : TRule['required'] extends RegleRuleDefinition<any, infer Params> ? Params extends never[] ? TState : never : never : never;
305
+
306
+ interface RegleBehaviourOptions {
307
+ /**
308
+ * Only display error when calling `validateForm()`
309
+ * @default false
310
+ */
311
+ lazy?: boolean;
312
+ /**
313
+ * Automaticaly set the dirty set without the need of `$value` or `$touch`
314
+ * @default true
315
+ */
316
+ autoDirty?: boolean;
317
+ /**
318
+ * The fields will turn valid when they are, but not invalid unless calling `validateForm()`
319
+ * @default false
320
+ */
321
+ rewardEarly?: boolean;
322
+ }
323
+
324
+ declare function createRule<TValue extends any, TParams extends any[] = [], TType extends string = string>(definition: RegleRuleInit<TValue, TParams, TType>): InferRegleRule<TValue, TParams>;
325
+
326
+ /**
327
+ * Returns a clean list of parameters
328
+ * Removing Ref and executing function to return the unwraped value
329
+ */
330
+ declare function unwrapRuleParameters<TParams extends any[]>(params: ParamDecl[]): TParams;
178
331
 
179
332
  /**
180
333
  * Root function that allows you to define project-wise all your custom validators or overwrite default ones
@@ -183,12 +336,9 @@ declare function createRule<TValue extends any, TParams extends any[] = []>(defi
183
336
  *
184
337
  * @param customRules
185
338
  */
186
- declare function defineCustomValidators<TCustomRules extends Partial<AllRulesDeclarations>>(customRules: () => TCustomRules): {
187
- useRegle: <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree & DefaultValidators & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules)) => {
188
- state: vue.Ref<TState>;
189
- $regle: vue.Ref<RegleStatus<TState, TRules>>;
190
- errors: RegleErrorTree<TRules>;
191
- };
192
- };
339
+ declare function defineRegleOptions<TCustomRules extends Partial<AllRulesDeclarations>>({ rules, options, }: {
340
+ rules?: () => TCustomRules;
341
+ options?: RegleBehaviourOptions;
342
+ }): <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, Partial<AllRulesDeclarations> & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules), options?: Partial<DeepMaybeRef<Required<RegleBehaviourOptions>>> | undefined) => Regle<TState, TRules>;
193
343
 
194
- 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 };
344
+ export { $InternalFormPropertyTypes, $InternalRegleCollectionRuleDecl, $InternalRegleCollectionStatus, $InternalRegleFieldStatus, $InternalReglePartialValidationTree, $InternalRegleRuleDecl, $InternalRegleRuleStatus, $InternalRegleStatus, $InternalRegleStatusType, AllRulesDeclarations, ArrayElement, CustomRulesDeclarationTree, DataType, DeepMaybeRef, ExcludeFromTuple, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, Maybe, MaybeNull, ParamDecl, PossibleRegleErrors, Regle, RegleBehaviourOptions, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleStatus, RegleUniversalParams, RegleValidationErrors, UnrefTuple, UnwrapRegleUniversalParams, createRule, defineRegleOptions, unwrapRuleParameters };