@regle/core 0.0.4-beta.0 → 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,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
@@ -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,19 +91,21 @@ 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
105
  /**
96
106
  * @public
97
107
  */
98
- type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
108
+ type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
99
109
  [TKey in keyof TForm]?: RegleFormPropertyType<TForm[TKey], TCustomRules>;
100
110
  };
101
111
  /**
@@ -108,7 +118,7 @@ type $InternalReglePartialValidationTree = {
108
118
  /**
109
119
  * @public
110
120
  */
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>;
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>;
112
122
  /**
113
123
  * @internal
114
124
  * @reference {@link RegleFormPropertyType}
@@ -118,7 +128,7 @@ type $InternalFormPropertyTypes = $InternalRegleRuleDecl | $InternalRegleCollect
118
128
  * @public
119
129
  * Rule tree for a form property
120
130
  */
121
- type RegleRuleDecl<TValue extends any = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
131
+ type RegleRuleDecl<TValue extends any = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
122
132
  [TKey in keyof TCustomRules]?: TCustomRules[TKey] extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, TParams> : FormRuleDeclaration<TValue, any>;
123
133
  };
124
134
  /**
@@ -129,7 +139,7 @@ type $InternalRegleRuleDecl = Record<string, FormRuleDeclaration<any, any>>;
129
139
  /**
130
140
  * @public
131
141
  */
132
- type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
142
+ type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
133
143
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
134
144
  }) | {
135
145
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
@@ -166,7 +176,7 @@ type DataType = string | number | Record<string, any> | File | Array<any> | Date
166
176
  /**
167
177
  * @public
168
178
  */
169
- interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus {
179
+ interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus<TState> {
170
180
  readonly $fields: {
171
181
  readonly [TKey in keyof TRules]: InferRegleStatusType<NonNullable<TRules[TKey]>, TState, TKey>;
172
182
  };
@@ -183,7 +193,7 @@ interface $InternalRegleStatus extends RegleCommonStatus {
183
193
  /**
184
194
  * @public
185
195
  */
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>;
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>;
187
197
  /**
188
198
  * @internal
189
199
  * @reference {@link InferRegleStatusType}
@@ -192,7 +202,7 @@ type $InternalRegleStatusType = $InternalRegleCollectionStatus | RegleCommonStat
192
202
  /**
193
203
  * @public
194
204
  */
195
- interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, AllRulesDeclarations> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus {
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> {
196
206
  $value: TState[TKey];
197
207
  readonly $rules: {
198
208
  readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
@@ -209,13 +219,15 @@ interface $InternalRegleFieldStatus extends RegleCommonStatus {
209
219
  /**
210
220
  * @public
211
221
  */
212
- interface RegleCommonStatus {
222
+ interface RegleCommonStatus<TValue = any> {
213
223
  readonly $valid: boolean;
214
224
  readonly $invalid: boolean;
215
225
  readonly $dirty: boolean;
216
226
  readonly $anyDirty: boolean;
217
227
  readonly $pending: boolean;
218
228
  readonly $error: boolean;
229
+ $id?: string;
230
+ $value: TValue;
219
231
  $touch(): void;
220
232
  $reset(): void;
221
233
  $validate(): Promise<boolean>;
@@ -267,6 +279,7 @@ interface RegleCollectionStatus<TRules extends RegleRuleDecl | ReglePartialValid
267
279
  interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fields'> {
268
280
  $each: Array<$InternalRegleStatusType>;
269
281
  $rules?: Record<string, $InternalRegleRuleStatus>;
282
+ /** Track each array state */
270
283
  $unwatch(): void;
271
284
  $watch(): void;
272
285
  $fields?: {
@@ -274,8 +287,48 @@ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fi
274
287
  };
275
288
  }
276
289
 
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
+
277
324
  declare function createRule<TValue extends any, TParams extends any[] = [], TType extends string = string>(definition: RegleRuleInit<TValue, TParams, TType>): InferRegleRule<TValue, TParams>;
278
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;
331
+
279
332
  /**
280
333
  * Root function that allows you to define project-wise all your custom validators or overwrite default ones
281
334
  *
@@ -283,14 +336,9 @@ declare function createRule<TValue extends any, TParams extends any[] = [], TTyp
283
336
  *
284
337
  * @param customRules
285
338
  */
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
- };
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>;
295
343
 
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 };
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,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
@@ -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,19 +91,21 @@ 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
105
  /**
96
106
  * @public
97
107
  */
98
- type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
108
+ type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
99
109
  [TKey in keyof TForm]?: RegleFormPropertyType<TForm[TKey], TCustomRules>;
100
110
  };
101
111
  /**
@@ -108,7 +118,7 @@ type $InternalReglePartialValidationTree = {
108
118
  /**
109
119
  * @public
110
120
  */
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>;
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>;
112
122
  /**
113
123
  * @internal
114
124
  * @reference {@link RegleFormPropertyType}
@@ -118,7 +128,7 @@ type $InternalFormPropertyTypes = $InternalRegleRuleDecl | $InternalRegleCollect
118
128
  * @public
119
129
  * Rule tree for a form property
120
130
  */
121
- type RegleRuleDecl<TValue extends any = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
131
+ type RegleRuleDecl<TValue extends any = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = {
122
132
  [TKey in keyof TCustomRules]?: TCustomRules[TKey] extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, TParams> : FormRuleDeclaration<TValue, any>;
123
133
  };
124
134
  /**
@@ -129,7 +139,7 @@ type $InternalRegleRuleDecl = Record<string, FormRuleDeclaration<any, any>>;
129
139
  /**
130
140
  * @public
131
141
  */
132
- type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
142
+ type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
133
143
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
134
144
  }) | {
135
145
  $each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
@@ -166,7 +176,7 @@ type DataType = string | number | Record<string, any> | File | Array<any> | Date
166
176
  /**
167
177
  * @public
168
178
  */
169
- interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus {
179
+ interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus<TState> {
170
180
  readonly $fields: {
171
181
  readonly [TKey in keyof TRules]: InferRegleStatusType<NonNullable<TRules[TKey]>, TState, TKey>;
172
182
  };
@@ -183,7 +193,7 @@ interface $InternalRegleStatus extends RegleCommonStatus {
183
193
  /**
184
194
  * @public
185
195
  */
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>;
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>;
187
197
  /**
188
198
  * @internal
189
199
  * @reference {@link InferRegleStatusType}
@@ -192,7 +202,7 @@ type $InternalRegleStatusType = $InternalRegleCollectionStatus | RegleCommonStat
192
202
  /**
193
203
  * @public
194
204
  */
195
- interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, AllRulesDeclarations> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus {
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> {
196
206
  $value: TState[TKey];
197
207
  readonly $rules: {
198
208
  readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
@@ -209,13 +219,15 @@ interface $InternalRegleFieldStatus extends RegleCommonStatus {
209
219
  /**
210
220
  * @public
211
221
  */
212
- interface RegleCommonStatus {
222
+ interface RegleCommonStatus<TValue = any> {
213
223
  readonly $valid: boolean;
214
224
  readonly $invalid: boolean;
215
225
  readonly $dirty: boolean;
216
226
  readonly $anyDirty: boolean;
217
227
  readonly $pending: boolean;
218
228
  readonly $error: boolean;
229
+ $id?: string;
230
+ $value: TValue;
219
231
  $touch(): void;
220
232
  $reset(): void;
221
233
  $validate(): Promise<boolean>;
@@ -267,6 +279,7 @@ interface RegleCollectionStatus<TRules extends RegleRuleDecl | ReglePartialValid
267
279
  interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fields'> {
268
280
  $each: Array<$InternalRegleStatusType>;
269
281
  $rules?: Record<string, $InternalRegleRuleStatus>;
282
+ /** Track each array state */
270
283
  $unwatch(): void;
271
284
  $watch(): void;
272
285
  $fields?: {
@@ -274,8 +287,48 @@ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fi
274
287
  };
275
288
  }
276
289
 
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
+
277
324
  declare function createRule<TValue extends any, TParams extends any[] = [], TType extends string = string>(definition: RegleRuleInit<TValue, TParams, TType>): InferRegleRule<TValue, TParams>;
278
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;
331
+
279
332
  /**
280
333
  * Root function that allows you to define project-wise all your custom validators or overwrite default ones
281
334
  *
@@ -283,14 +336,9 @@ declare function createRule<TValue extends any, TParams extends any[] = [], TTyp
283
336
  *
284
337
  * @param customRules
285
338
  */
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
- };
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>;
295
343
 
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 };
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 };