@regle/core 0.0.4-beta.0 → 0.0.5

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,9 +1,16 @@
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
@@ -20,6 +27,21 @@ type DefaultValidators = {
20
27
  maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
21
28
  required: RegleRuleDefinition<unknown, []>;
22
29
  requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
30
+ alpha: RegleRuleDefinition<string>;
31
+ alphaNum: RegleRuleDefinition<string | number>;
32
+ between: RegleRuleWithParamsDefinition<number, [min: number, max: number]>;
33
+ decimal: RegleRuleDefinition<number | string>;
34
+ email: RegleRuleDefinition<string>;
35
+ integer: RegleRuleDefinition<number | string>;
36
+ maxValue: RegleRuleWithParamsDefinition<number, [count: number]>;
37
+ minLength: RegleRuleWithParamsDefinition<string | Record<PropertyKey, any> | any[], [
38
+ count: number
39
+ ]>;
40
+ minValue: RegleRuleWithParamsDefinition<number, [count: number]>;
41
+ numeric: RegleRuleDefinition<number | string>;
42
+ requireUnless: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
43
+ sameAs: RegleRuleWithParamsDefinition<unknown, [target: unknown]>;
44
+ url: RegleRuleDefinition<string>;
23
45
  };
24
46
 
25
47
  type ParamDecl<T = any> = MaybeRef<T> | (() => T);
@@ -49,6 +71,7 @@ interface RegleInternalRuleDefs<TValue extends any = any, TParams extends any[]
49
71
  _type: string;
50
72
  _patched: boolean;
51
73
  _params?: RegleUniversalParams<TParams>;
74
+ _async: boolean;
52
75
  }
53
76
  declare enum InternalRuleType {
54
77
  Inline = "__inline",
@@ -83,19 +106,21 @@ type InferRegleRule<TValue extends any = any, TParams extends any[] = []> = [
83
106
  TParams
84
107
  ] extends [[]] ? RegleRuleDefinition<TValue, TParams> : RegleRuleWithParamsDefinition<TValue, TParams>;
85
108
  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> & {
109
+ type RegleCollectionRuleDefinition<TValue = any[], TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
87
110
  $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
88
111
  }) | {
89
112
  $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
90
113
  };
91
114
 
92
- type CustomRulesDeclarationTree = Record<string, RegleRuleRaw<any, any>>;
115
+ type CustomRulesDeclarationTree = {
116
+ [x: string]: RegleRuleRaw<any, any> | undefined;
117
+ };
93
118
  type AllRulesDeclarations = CustomRulesDeclarationTree & DefaultValidators;
94
119
 
95
120
  /**
96
121
  * @public
97
122
  */
98
- type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
123
+ type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
99
124
  [TKey in keyof TForm]?: RegleFormPropertyType<TForm[TKey], TCustomRules>;
100
125
  };
101
126
  /**
@@ -108,7 +133,7 @@ type $InternalReglePartialValidationTree = {
108
133
  /**
109
134
  * @public
110
135
  */
111
- 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>;
136
+ 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>;
112
137
  /**
113
138
  * @internal
114
139
  * @reference {@link RegleFormPropertyType}
@@ -118,7 +143,7 @@ type $InternalFormPropertyTypes = $InternalRegleRuleDecl | $InternalRegleCollect
118
143
  * @public
119
144
  * Rule tree for a form property
120
145
  */
121
- type RegleRuleDecl<TValue extends any = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
146
+ type RegleRuleDecl<TValue extends any = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
122
147
  [TKey in keyof TCustomRules]?: TCustomRules[TKey] extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, TParams> : FormRuleDeclaration<TValue, any>;
123
148
  };
124
149
  /**
@@ -129,7 +154,7 @@ type $InternalRegleRuleDecl = Record<string, FormRuleDeclaration<any, any>>;
129
154
  /**
130
155
  * @public
131
156
  */
132
- type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
157
+ type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
133
158
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
134
159
  }) | {
135
160
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
@@ -166,7 +191,7 @@ type DataType = string | number | Record<string, any> | File | Array<any> | Date
166
191
  /**
167
192
  * @public
168
193
  */
169
- interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus {
194
+ interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus<TState> {
170
195
  readonly $fields: {
171
196
  readonly [TKey in keyof TRules]: InferRegleStatusType<NonNullable<TRules[TKey]>, TState, TKey>;
172
197
  };
@@ -183,7 +208,7 @@ interface $InternalRegleStatus extends RegleCommonStatus {
183
208
  /**
184
209
  * @public
185
210
  */
186
- 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 : RegleStatus<TState[TKey], TRule> : RegleFieldStatus<TRule, TState, TKey>;
211
+ 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>;
187
212
  /**
188
213
  * @internal
189
214
  * @reference {@link InferRegleStatusType}
@@ -192,7 +217,7 @@ type $InternalRegleStatusType = $InternalRegleCollectionStatus | RegleCommonStat
192
217
  /**
193
218
  * @public
194
219
  */
195
- interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, AllRulesDeclarations> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus {
220
+ interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, Partial<AllRulesDeclarations>> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus<TState> {
196
221
  $value: TState[TKey];
197
222
  readonly $rules: {
198
223
  readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
@@ -209,13 +234,15 @@ interface $InternalRegleFieldStatus extends RegleCommonStatus {
209
234
  /**
210
235
  * @public
211
236
  */
212
- interface RegleCommonStatus {
237
+ interface RegleCommonStatus<TValue = any> {
213
238
  readonly $valid: boolean;
214
239
  readonly $invalid: boolean;
215
240
  readonly $dirty: boolean;
216
241
  readonly $anyDirty: boolean;
217
242
  readonly $pending: boolean;
218
243
  readonly $error: boolean;
244
+ $id?: string;
245
+ $value: TValue;
219
246
  $touch(): void;
220
247
  $reset(): void;
221
248
  $validate(): Promise<boolean>;
@@ -267,6 +294,7 @@ interface RegleCollectionStatus<TRules extends RegleRuleDecl | ReglePartialValid
267
294
  interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fields'> {
268
295
  $each: Array<$InternalRegleStatusType>;
269
296
  $rules?: Record<string, $InternalRegleRuleStatus>;
297
+ /** Track each array state */
270
298
  $unwatch(): void;
271
299
  $watch(): void;
272
300
  $fields?: {
@@ -274,8 +302,48 @@ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fi
274
302
  };
275
303
  }
276
304
 
305
+ interface Regle<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> {
306
+ state: Ref<PartialDeep<TState>>;
307
+ $regle: RegleStatus<TState, TRules>;
308
+ errors: RegleErrorTree<TRules>;
309
+ resetForm: () => void;
310
+ validateForm: () => Promise<false | DeepSafeFormState<TState, TRules>>;
311
+ }
312
+ type DeepSafeFormState<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> = {
313
+ [K in keyof TState as [SafeProperty<TState[K], TRules[K]>] extends [never] ? K : never]?: [
314
+ SafeProperty<TState[K], TRules[K]>
315
+ ] extends [never] ? TState[K] : SafeProperty<TState[K], TRules[K]>;
316
+ } & {
317
+ [K in keyof TState as [SafeProperty<TState[K], TRules[K]>] extends [never] ? never : K]-?: SafeProperty<TState[K], TRules[K]>;
318
+ };
319
+ 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;
320
+
321
+ interface RegleBehaviourOptions {
322
+ /**
323
+ * Only display error when calling `validateForm()`
324
+ * @default false
325
+ */
326
+ lazy?: boolean;
327
+ /**
328
+ * Automaticaly set the dirty set without the need of `$value` or `$touch`
329
+ * @default true
330
+ */
331
+ autoDirty?: boolean;
332
+ /**
333
+ * The fields will turn valid when they are, but not invalid unless calling `validateForm()`
334
+ * @default false
335
+ */
336
+ rewardEarly?: boolean;
337
+ }
338
+
277
339
  declare function createRule<TValue extends any, TParams extends any[] = [], TType extends string = string>(definition: RegleRuleInit<TValue, TParams, TType>): InferRegleRule<TValue, TParams>;
278
340
 
341
+ /**
342
+ * Returns a clean list of parameters
343
+ * Removing Ref and executing function to return the unwraped value
344
+ */
345
+ declare function unwrapRuleParameters<TParams extends any[]>(params: ParamDecl[]): TParams;
346
+
279
347
  /**
280
348
  * Root function that allows you to define project-wise all your custom validators or overwrite default ones
281
349
  *
@@ -283,14 +351,9 @@ declare function createRule<TValue extends any, TParams extends any[] = [], TTyp
283
351
  *
284
352
  * @param customRules
285
353
  */
286
- declare function defineCustomValidators<TCustomRules extends Partial<AllRulesDeclarations>>(customRules: () => TCustomRules): {
287
- useRegle: <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree & DefaultValidators & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules)) => {
288
- state: vue.Ref<TState>;
289
- $regle: RegleStatus<TState, TRules>;
290
- errors: RegleErrorTree<TRules>;
291
- resetForm: () => void;
292
- validateForm: () => Promise<boolean>;
293
- };
294
- };
354
+ declare function defineRegleOptions<TCustomRules extends Partial<AllRulesDeclarations>>({ rules, options, }: {
355
+ rules?: () => TCustomRules;
356
+ options?: RegleBehaviourOptions;
357
+ }): <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>;
295
358
 
296
- export { $InternalFormPropertyTypes, $InternalRegleCollectionRuleDecl, $InternalRegleCollectionStatus, $InternalRegleFieldStatus, $InternalReglePartialValidationTree, $InternalRegleRuleDecl, $InternalRegleRuleStatus, $InternalRegleStatus, $InternalRegleStatusType, AllRulesDeclarations, CustomRulesDeclarationTree, DataType, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, ParamDecl, PossibleRegleErrors, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleStatus, RegleUniversalParams, RegleValidationErrors, UnwrapRegleUniversalParams, createRule, defineCustomValidators };
359
+ 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,9 +1,16 @@
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
@@ -20,6 +27,21 @@ type DefaultValidators = {
20
27
  maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
21
28
  required: RegleRuleDefinition<unknown, []>;
22
29
  requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
30
+ alpha: RegleRuleDefinition<string>;
31
+ alphaNum: RegleRuleDefinition<string | number>;
32
+ between: RegleRuleWithParamsDefinition<number, [min: number, max: number]>;
33
+ decimal: RegleRuleDefinition<number | string>;
34
+ email: RegleRuleDefinition<string>;
35
+ integer: RegleRuleDefinition<number | string>;
36
+ maxValue: RegleRuleWithParamsDefinition<number, [count: number]>;
37
+ minLength: RegleRuleWithParamsDefinition<string | Record<PropertyKey, any> | any[], [
38
+ count: number
39
+ ]>;
40
+ minValue: RegleRuleWithParamsDefinition<number, [count: number]>;
41
+ numeric: RegleRuleDefinition<number | string>;
42
+ requireUnless: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
43
+ sameAs: RegleRuleWithParamsDefinition<unknown, [target: unknown]>;
44
+ url: RegleRuleDefinition<string>;
23
45
  };
24
46
 
25
47
  type ParamDecl<T = any> = MaybeRef<T> | (() => T);
@@ -49,6 +71,7 @@ interface RegleInternalRuleDefs<TValue extends any = any, TParams extends any[]
49
71
  _type: string;
50
72
  _patched: boolean;
51
73
  _params?: RegleUniversalParams<TParams>;
74
+ _async: boolean;
52
75
  }
53
76
  declare enum InternalRuleType {
54
77
  Inline = "__inline",
@@ -83,19 +106,21 @@ type InferRegleRule<TValue extends any = any, TParams extends any[] = []> = [
83
106
  TParams
84
107
  ] extends [[]] ? RegleRuleDefinition<TValue, TParams> : RegleRuleWithParamsDefinition<TValue, TParams>;
85
108
  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> & {
109
+ type RegleCollectionRuleDefinition<TValue = any[], TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
87
110
  $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
88
111
  }) | {
89
112
  $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
90
113
  };
91
114
 
92
- type CustomRulesDeclarationTree = Record<string, RegleRuleRaw<any, any>>;
115
+ type CustomRulesDeclarationTree = {
116
+ [x: string]: RegleRuleRaw<any, any> | undefined;
117
+ };
93
118
  type AllRulesDeclarations = CustomRulesDeclarationTree & DefaultValidators;
94
119
 
95
120
  /**
96
121
  * @public
97
122
  */
98
- type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
123
+ type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
99
124
  [TKey in keyof TForm]?: RegleFormPropertyType<TForm[TKey], TCustomRules>;
100
125
  };
101
126
  /**
@@ -108,7 +133,7 @@ type $InternalReglePartialValidationTree = {
108
133
  /**
109
134
  * @public
110
135
  */
111
- 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>;
136
+ 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>;
112
137
  /**
113
138
  * @internal
114
139
  * @reference {@link RegleFormPropertyType}
@@ -118,7 +143,7 @@ type $InternalFormPropertyTypes = $InternalRegleRuleDecl | $InternalRegleCollect
118
143
  * @public
119
144
  * Rule tree for a form property
120
145
  */
121
- type RegleRuleDecl<TValue extends any = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
146
+ type RegleRuleDecl<TValue extends any = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
122
147
  [TKey in keyof TCustomRules]?: TCustomRules[TKey] extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, TParams> : FormRuleDeclaration<TValue, any>;
123
148
  };
124
149
  /**
@@ -129,7 +154,7 @@ type $InternalRegleRuleDecl = Record<string, FormRuleDeclaration<any, any>>;
129
154
  /**
130
155
  * @public
131
156
  */
132
- type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
157
+ type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
133
158
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
134
159
  }) | {
135
160
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
@@ -166,7 +191,7 @@ type DataType = string | number | Record<string, any> | File | Array<any> | Date
166
191
  /**
167
192
  * @public
168
193
  */
169
- interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus {
194
+ interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus<TState> {
170
195
  readonly $fields: {
171
196
  readonly [TKey in keyof TRules]: InferRegleStatusType<NonNullable<TRules[TKey]>, TState, TKey>;
172
197
  };
@@ -183,7 +208,7 @@ interface $InternalRegleStatus extends RegleCommonStatus {
183
208
  /**
184
209
  * @public
185
210
  */
186
- 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 : RegleStatus<TState[TKey], TRule> : RegleFieldStatus<TRule, TState, TKey>;
211
+ 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>;
187
212
  /**
188
213
  * @internal
189
214
  * @reference {@link InferRegleStatusType}
@@ -192,7 +217,7 @@ type $InternalRegleStatusType = $InternalRegleCollectionStatus | RegleCommonStat
192
217
  /**
193
218
  * @public
194
219
  */
195
- interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, AllRulesDeclarations> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus {
220
+ interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, Partial<AllRulesDeclarations>> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus<TState> {
196
221
  $value: TState[TKey];
197
222
  readonly $rules: {
198
223
  readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
@@ -209,13 +234,15 @@ interface $InternalRegleFieldStatus extends RegleCommonStatus {
209
234
  /**
210
235
  * @public
211
236
  */
212
- interface RegleCommonStatus {
237
+ interface RegleCommonStatus<TValue = any> {
213
238
  readonly $valid: boolean;
214
239
  readonly $invalid: boolean;
215
240
  readonly $dirty: boolean;
216
241
  readonly $anyDirty: boolean;
217
242
  readonly $pending: boolean;
218
243
  readonly $error: boolean;
244
+ $id?: string;
245
+ $value: TValue;
219
246
  $touch(): void;
220
247
  $reset(): void;
221
248
  $validate(): Promise<boolean>;
@@ -267,6 +294,7 @@ interface RegleCollectionStatus<TRules extends RegleRuleDecl | ReglePartialValid
267
294
  interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fields'> {
268
295
  $each: Array<$InternalRegleStatusType>;
269
296
  $rules?: Record<string, $InternalRegleRuleStatus>;
297
+ /** Track each array state */
270
298
  $unwatch(): void;
271
299
  $watch(): void;
272
300
  $fields?: {
@@ -274,8 +302,48 @@ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fi
274
302
  };
275
303
  }
276
304
 
305
+ interface Regle<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> {
306
+ state: Ref<PartialDeep<TState>>;
307
+ $regle: RegleStatus<TState, TRules>;
308
+ errors: RegleErrorTree<TRules>;
309
+ resetForm: () => void;
310
+ validateForm: () => Promise<false | DeepSafeFormState<TState, TRules>>;
311
+ }
312
+ type DeepSafeFormState<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> = {
313
+ [K in keyof TState as [SafeProperty<TState[K], TRules[K]>] extends [never] ? K : never]?: [
314
+ SafeProperty<TState[K], TRules[K]>
315
+ ] extends [never] ? TState[K] : SafeProperty<TState[K], TRules[K]>;
316
+ } & {
317
+ [K in keyof TState as [SafeProperty<TState[K], TRules[K]>] extends [never] ? never : K]-?: SafeProperty<TState[K], TRules[K]>;
318
+ };
319
+ 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;
320
+
321
+ interface RegleBehaviourOptions {
322
+ /**
323
+ * Only display error when calling `validateForm()`
324
+ * @default false
325
+ */
326
+ lazy?: boolean;
327
+ /**
328
+ * Automaticaly set the dirty set without the need of `$value` or `$touch`
329
+ * @default true
330
+ */
331
+ autoDirty?: boolean;
332
+ /**
333
+ * The fields will turn valid when they are, but not invalid unless calling `validateForm()`
334
+ * @default false
335
+ */
336
+ rewardEarly?: boolean;
337
+ }
338
+
277
339
  declare function createRule<TValue extends any, TParams extends any[] = [], TType extends string = string>(definition: RegleRuleInit<TValue, TParams, TType>): InferRegleRule<TValue, TParams>;
278
340
 
341
+ /**
342
+ * Returns a clean list of parameters
343
+ * Removing Ref and executing function to return the unwraped value
344
+ */
345
+ declare function unwrapRuleParameters<TParams extends any[]>(params: ParamDecl[]): TParams;
346
+
279
347
  /**
280
348
  * Root function that allows you to define project-wise all your custom validators or overwrite default ones
281
349
  *
@@ -283,14 +351,9 @@ declare function createRule<TValue extends any, TParams extends any[] = [], TTyp
283
351
  *
284
352
  * @param customRules
285
353
  */
286
- declare function defineCustomValidators<TCustomRules extends Partial<AllRulesDeclarations>>(customRules: () => TCustomRules): {
287
- useRegle: <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree & DefaultValidators & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules)) => {
288
- state: vue.Ref<TState>;
289
- $regle: RegleStatus<TState, TRules>;
290
- errors: RegleErrorTree<TRules>;
291
- resetForm: () => void;
292
- validateForm: () => Promise<boolean>;
293
- };
294
- };
354
+ declare function defineRegleOptions<TCustomRules extends Partial<AllRulesDeclarations>>({ rules, options, }: {
355
+ rules?: () => TCustomRules;
356
+ options?: RegleBehaviourOptions;
357
+ }): <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>;
295
358
 
296
- export { $InternalFormPropertyTypes, $InternalRegleCollectionRuleDecl, $InternalRegleCollectionStatus, $InternalRegleFieldStatus, $InternalReglePartialValidationTree, $InternalRegleRuleDecl, $InternalRegleRuleStatus, $InternalRegleStatus, $InternalRegleStatusType, AllRulesDeclarations, CustomRulesDeclarationTree, DataType, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, ParamDecl, PossibleRegleErrors, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleStatus, RegleUniversalParams, RegleValidationErrors, UnwrapRegleUniversalParams, createRule, defineCustomValidators };
359
+ 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 };