@regle/core 0.0.5 → 0.0.7

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.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as vue from 'vue';
2
- import { MaybeRef, Ref } from 'vue';
3
- import { PartialDeep } from 'type-fest';
2
+ import { MaybeRef, Ref, ComputedRef } from 'vue';
3
+ import { DeepMaybeRef as DeepMaybeRef$1 } from 'types/utils';
4
+ import { DeepReactiveState as DeepReactiveState$1 } from 'types/core/useRegle.types';
4
5
 
5
6
  type ArrayElement<T> = T extends Array<infer U> ? U : never;
6
7
  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>] : [];
@@ -11,40 +12,11 @@ type MaybeNull<T> = T | null;
11
12
  type DeepMaybeRef<T extends Record<string, any>> = {
12
13
  [K in keyof T]: MaybeRef<T[K]>;
13
14
  };
14
-
15
- /**
16
- * @argument
17
- * createRule arguments options
18
- */
19
- interface RegleRuleInit<TValue extends any, TParams extends any[] = [], TType extends string = string> {
20
- validator: (value: Maybe<TValue>, ...args: TParams) => boolean | Promise<boolean>;
21
- message: string | ((value: Maybe<TValue>, ...args: TParams) => string);
22
- active?: boolean | ((value: Maybe<TValue>, ...args: TParams) => boolean);
23
- type: TType;
24
- }
25
-
26
- type DefaultValidators = {
27
- maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
28
- required: RegleRuleDefinition<unknown, []>;
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>;
15
+ type ExcludeByType<T, U> = {
16
+ [K in keyof T as T[K] extends U ? never : K]: T[K] extends U ? never : T[K];
45
17
  };
46
18
 
47
- type ParamDecl<T = any> = MaybeRef<T> | (() => T);
19
+ type ParamDecl<T = any> = MaybeRef<Maybe<T>> | (() => Maybe<T>);
48
20
  type CreateFn<T extends any[]> = (...args: T) => any;
49
21
  /**
50
22
  * Transform normal parameters tuple declaration to a rich tuple declaration
@@ -64,14 +36,14 @@ type UnwrapRegleUniversalParams<T extends ParamDecl[] = [], F = CreateFn<T>> = [
64
36
  /**
65
37
  * Internal definition of the rule, can be used to reset or patch the rule
66
38
  */
67
- interface RegleInternalRuleDefs<TValue extends any = any, TParams extends any[] = []> {
68
- _validator: (value: Maybe<TValue>, ...args: TParams) => boolean | Promise<boolean>;
69
- _message: string | ((value: Maybe<TValue>, ...args: TParams) => string);
70
- _active?: boolean | ((value: Maybe<TValue>, ...args: TParams) => boolean);
71
- _type: string;
39
+ interface RegleInternalRuleDefs<TValue extends any = any, TParams extends any[] = [], TAsync extends boolean = false, TMetadata extends RegleRuleMetadataDefinition = boolean> {
40
+ _validator: (value: Maybe<TValue>, ...args: TParams) => TAsync extends false ? TMetadata : Promise<TMetadata>;
41
+ _message: string | ((value: Maybe<TValue>, metadata: RegleRuleMetadataConsumer<TParams, TMetadata>) => string);
42
+ _active?: boolean | ((value: Maybe<TValue>, metadata: RegleRuleMetadataConsumer<TParams, TMetadata>) => boolean);
43
+ _type?: string;
72
44
  _patched: boolean;
73
45
  _params?: RegleUniversalParams<TParams>;
74
- _async: boolean;
46
+ _async: TAsync;
75
47
  }
76
48
  declare enum InternalRuleType {
77
49
  Inline = "__inline",
@@ -81,39 +53,130 @@ declare enum InternalRuleType {
81
53
  /**
82
54
  * Returned typed of rules created with `createRule`
83
55
  * */
84
- interface RegleRuleDefinition<TValue extends any = any, TParams extends any[] = []> extends RegleInternalRuleDefs<TValue, TParams> {
85
- validator: RegleRuleDefinitionProcessor<TValue, TParams, boolean | Promise<boolean>>;
86
- message: RegleRuleDefinitionProcessor<TValue, TParams, string>;
87
- active: RegleRuleDefinitionProcessor<TValue, TParams, boolean>;
88
- type: string;
56
+ interface RegleRuleDefinition<TValue extends any = any, TParams extends any[] = [], TAsync extends boolean = false, TMetaData extends RegleRuleMetadataDefinition = boolean> extends RegleInternalRuleDefs<TValue, TParams, TAsync, TMetaData> {
57
+ validator: RegleRuleDefinitionProcessor<TValue, TParams, TAsync extends false ? TMetaData : Promise<TMetaData>>;
58
+ message: RegleRuleDefinitionWithMetadataProcessor<TValue, RegleRuleMetadataConsumer<TParams, TMetaData>, string>;
59
+ active: RegleRuleDefinitionWithMetadataProcessor<TValue, RegleRuleMetadataConsumer<TParams, TMetaData>, boolean>;
60
+ type?: string;
61
+ exec: (value: Maybe<TValue>) => TAsync extends false ? TMetaData : Promise<TMetaData>;
62
+ }
63
+ /**
64
+ * @internal
65
+ * */
66
+ interface $InternalRegleRuleDefinition extends RegleInternalRuleDefs<any, any, any> {
67
+ validator: RegleRuleDefinitionProcessor;
68
+ message: RegleRuleDefinitionWithMetadataProcessor<any, any, any>;
69
+ active: RegleRuleDefinitionWithMetadataProcessor<any, any, any>;
70
+ type?: string;
71
+ exec: (value: any) => RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>;
89
72
  }
90
73
  /**
91
74
  * Rules with params created with `createRules` are callable while being customizable
92
75
  */
93
- interface RegleRuleWithParamsDefinition<TValue extends any = any, TParams extends any[] = []> extends RegleRuleInit<TValue, TParams>, RegleInternalRuleDefs<TValue, TParams> {
94
- (...params: RegleUniversalParams<TParams>): RegleRuleDefinition<TValue, TParams>;
76
+ interface RegleRuleWithParamsDefinition<TValue extends any = any, TParams extends any[] = [], TAsync extends boolean = false, TMetadata extends RegleRuleMetadataDefinition = boolean> extends RegleRuleCore<TValue, TParams, TAsync, TMetadata>, RegleInternalRuleDefs<TValue, TParams, TAsync, TMetadata> {
77
+ (...params: RegleUniversalParams<TParams>): RegleRuleDefinition<TValue, TParams, TAsync, TMetadata>;
95
78
  }
79
+ /**
80
+ * Define a rule Metadata definition
81
+ */
82
+ type RegleRuleMetadataDefinition = {
83
+ $valid: boolean;
84
+ [x: string]: any;
85
+ } | boolean;
86
+ type DefaultMetadataProperties = Pick<ExcludeByType<RegleCommonStatus, Function>, '$invalid'>;
87
+ /**
88
+ * Will be used to consumme metadata on related helpers and rule status
89
+ */
90
+ type RegleRuleMetadataConsumer<TParams extends any[] = [], TMetadata extends RegleRuleMetadataDefinition = boolean> = DefaultMetadataProperties & (TParams extends never ? {} : {
91
+ $params: TParams;
92
+ }) & (TMetadata extends boolean ? {} : Omit<TMetadata, '$valid'>);
93
+ /**
94
+ * @internal
95
+ */
96
+ type $InternalRegleRuleMetadataConsumer = DefaultMetadataProperties & {
97
+ $params?: any[];
98
+ [x: string]: any;
99
+ };
96
100
  /**
97
101
  * Generic types for a created RegleRule
98
102
  */
99
- type RegleRuleRaw<TValue extends any = any, TParams extends any[] = []> = RegleRuleDefinition<TValue, TParams> | RegleRuleWithParamsDefinition<TValue, TParams>;
103
+ type RegleRuleRaw<TValue extends any = any, TParams extends any[] = [], TAsync extends boolean = false, TMetaData extends RegleRuleMetadataDefinition = boolean> = RegleRuleDefinition<TValue, TParams, TAsync, TMetaData> | RegleRuleWithParamsDefinition<TValue, TParams, TAsync, TMetaData>;
100
104
  /**
101
105
  * Process the type of a created rule with `createRule`.
102
106
  * For a rule with params it will return a function
103
107
  * Otherwise it will return the rule definition
104
108
  */
105
- type InferRegleRule<TValue extends any = any, TParams extends any[] = []> = [
106
- TParams
107
- ] extends [[]] ? RegleRuleDefinition<TValue, TParams> : RegleRuleWithParamsDefinition<TValue, TParams>;
108
- type RegleRuleDefinitionProcessor<TValue extends any = any, TParams extends any[] = [], TReturn = any> = (value: Maybe<TValue>, ...args: TParams) => TReturn;
109
+ type InferRegleRule<TValue extends any = any, TParams extends any[] = [], TAsync extends boolean = false, TMetaData extends RegleRuleMetadataDefinition = boolean> = [TParams] extends [[]] ? RegleRuleDefinition<TValue, TParams, TAsync, TMetaData> : RegleRuleWithParamsDefinition<TValue, TParams, TAsync, TMetaData>;
110
+ type RegleRuleDefinitionProcessor<TValue extends any = any, TParams extends any[] = [], TReturn = any> = (value: Maybe<TValue>, ...params: TParams) => TReturn;
111
+ type RegleRuleDefinitionWithMetadataProcessor<TValue extends any, TMetadata extends RegleRuleMetadataConsumer<any, any>, TReturn = any> = (value: Maybe<TValue>, metadata: TMetadata) => TReturn;
109
112
  type RegleCollectionRuleDefinition<TValue = any[], TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
110
113
  $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
111
114
  }) | {
112
115
  $each: RegleFormPropertyType<ArrayElement<TValue>, TCustomRules>;
113
116
  };
114
117
 
118
+ /**
119
+ * @argument
120
+ * createRule arguments options
121
+ */
122
+ interface RegleRuleInit<TValue extends any, TParams extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TMetadata extends RegleRuleMetadataDefinition, TAsync extends boolean = TReturn extends Promise<any> ? true : false> {
123
+ type: {
124
+ value: TValue;
125
+ params: TParams;
126
+ };
127
+ validator: (value: Maybe<TValue>, ...args: TParams) => TReturn;
128
+ message: string | ((value: Maybe<TValue>, metadata: RegleRuleMetadataConsumer<TParams, TMetadata>) => string);
129
+ active?: boolean | ((value: Maybe<TValue>, metadata: RegleRuleMetadataConsumer<TParams, TMetadata>) => boolean);
130
+ }
131
+ /**
132
+ * @argument
133
+ * Rule core
134
+ */
135
+ interface RegleRuleCore<TValue extends any, TParams extends any[] = [], TAsync extends boolean = false, TMetadata extends RegleRuleMetadataDefinition = boolean> {
136
+ validator: (value: Maybe<TValue>, ...args: TParams) => TAsync extends false ? TMetadata : Promise<TMetadata>;
137
+ message: string | ((value: Maybe<TValue>, metadata: RegleRuleMetadataConsumer<TParams, TMetadata>) => string);
138
+ active?: boolean | ((value: Maybe<TValue>, metadata: RegleRuleMetadataConsumer<TParams, TMetadata>) => boolean);
139
+ type?: string;
140
+ }
141
+ /**
142
+ * @internal
143
+ * createRule arguments options
144
+ */
145
+ interface $InternalRegleRuleInit {
146
+ validator: (value: any, ...args: any[]) => RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>;
147
+ message: string | ((value: any, metadata: $InternalRegleRuleMetadataConsumer) => string);
148
+ active?: boolean | ((value: any, metadata: $InternalRegleRuleMetadataConsumer) => boolean);
149
+ type?: string;
150
+ }
151
+
152
+ type DefaultValidators = {
153
+ maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
154
+ required: RegleRuleDefinition<unknown, []>;
155
+ requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
156
+ requiredUnless: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
157
+ alpha: RegleRuleDefinition<string>;
158
+ alphaNum: RegleRuleDefinition<string | number>;
159
+ between: RegleRuleWithParamsDefinition<number, [min: number, max: number]>;
160
+ decimal: RegleRuleDefinition<number | string>;
161
+ email: RegleRuleDefinition<string>;
162
+ integer: RegleRuleDefinition<number | string>;
163
+ maxValue: RegleRuleWithParamsDefinition<number, [count: number]>;
164
+ minLength: RegleRuleWithParamsDefinition<string | Record<PropertyKey, any> | any[], [
165
+ count: number
166
+ ]>;
167
+ minValue: RegleRuleWithParamsDefinition<number, [count: number]>;
168
+ numeric: RegleRuleDefinition<number | string>;
169
+ sameAs: RegleRuleWithParamsDefinition<unknown, [target: unknown]>;
170
+ url: RegleRuleDefinition<string>;
171
+ dateAfter: RegleRuleWithParamsDefinition<Date, [after: Date]>;
172
+ dateBefore: RegleRuleWithParamsDefinition<Date, [before: Date]>;
173
+ dateBetween: RegleRuleWithParamsDefinition<Date, [before: Date, after: Date]>;
174
+ ipAddress: RegleRuleDefinition<string, [], false>;
175
+ macAddress: RegleRuleWithParamsDefinition<string, [separator?: string | undefined], false>;
176
+ };
177
+
115
178
  type CustomRulesDeclarationTree = {
116
- [x: string]: RegleRuleRaw<any, any> | undefined;
179
+ [x: string]: RegleRuleRaw<any, any, boolean, any> | undefined;
117
180
  };
118
181
  type AllRulesDeclarations = CustomRulesDeclarationTree & DefaultValidators;
119
182
 
@@ -170,12 +233,16 @@ type $InternalRegleCollectionRuleDecl = $InternalRegleRuleDecl & {
170
233
  /**
171
234
  * @public
172
235
  */
173
- type InlineRuleDeclaration<TValue extends any = any> = (value: Maybe<TValue>, ...args: any[]) => boolean | Promise<boolean>;
236
+ type InlineRuleDeclaration<TValue extends any = any, TMetadata extends RegleRuleMetadataDefinition = boolean, TReturn extends TMetadata | Promise<TMetadata> = TMetadata, TAsync extends boolean = TReturn extends Promise<any> ? true : false> = (value: Maybe<TValue>, ...args: any[]) => TReturn;
237
+ /**
238
+ * @public
239
+ */
240
+ type InlinePromiseRuleDeclaration<TValue extends any = any, TMetadata extends RegleRuleMetadataDefinition = boolean> = (value: Maybe<TValue>, ...args: any[]) => Promise<TMetadata>;
174
241
  /**
175
242
  * @public
176
243
  * Regroup inline and registered rules
177
244
  * */
178
- type FormRuleDeclaration<TValue extends any, TParams extends any[] = []> = InlineRuleDeclaration<TValue> | RegleRuleDefinition<TValue, TParams>;
245
+ type FormRuleDeclaration<TValue extends any, TParams extends any[] = [], TMetadata extends RegleRuleMetadataDefinition = boolean> = InlineRuleDeclaration<TValue, TMetadata> | RegleRuleDefinition<TValue, TParams, boolean, TMetadata>;
179
246
 
180
247
  type RegleErrorTree<TRules extends ReglePartialValidationTree<any, any>> = {
181
248
  readonly [K in keyof TRules]: RegleValidationErrors<TRules[K]>;
@@ -185,7 +252,21 @@ type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undef
185
252
  readonly $errors: string[];
186
253
  readonly $each: RegleValidationErrors<TRule>[];
187
254
  };
188
- type PossibleRegleErrors = RegleCollectionErrors<any> | string[] | RegleErrorTree<any>;
255
+ /**
256
+ * @internal
257
+ */
258
+ type $InternalRegleErrors = RegleCollectionErrors<any> | string[] | RegleErrorTree<any>;
259
+ type RegleExternalErrorTree<TState extends Record<string, any> = Record<string, any>> = {
260
+ [K in keyof TState]?: RegleExternalValidationErrors<TState[K]>;
261
+ };
262
+ type RegleExternalValidationErrors<TValue extends any> = [NonNullable<TValue>] extends [
263
+ never
264
+ ] ? string[] : TValue extends Array<infer U> ? RegleExternalCollectionErrors<U> : NonNullable<TValue> extends Date ? string[] : NonNullable<TValue> extends File ? string[] : TValue extends Record<string, any> ? RegleExternalErrorTree<TValue> : string[];
265
+ type RegleExternalCollectionErrors<TValue extends any = any> = {
266
+ $errors?: string[];
267
+ $each?: RegleExternalValidationErrors<TValue>[];
268
+ };
269
+ type $InternalExternalRegleErrors = RegleExternalCollectionErrors<any> | string[] | RegleExternalErrorTree<any>;
189
270
  type DataType = string | number | Record<string, any> | File | Array<any> | Date | null | undefined;
190
271
 
191
272
  /**
@@ -219,6 +300,7 @@ type $InternalRegleStatusType = $InternalRegleCollectionStatus | RegleCommonStat
219
300
  */
220
301
  interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, Partial<AllRulesDeclarations>> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus<TState> {
221
302
  $value: TState[TKey];
303
+ readonly $externalErrors?: string[];
222
304
  readonly $rules: {
223
305
  readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
224
306
  };
@@ -230,6 +312,7 @@ interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, Partial<All
230
312
  interface $InternalRegleFieldStatus extends RegleCommonStatus {
231
313
  $value: any;
232
314
  $rules: Record<string, $InternalRegleRuleStatus>;
315
+ $externalErrors?: string[];
233
316
  }
234
317
  /**
235
318
  * @public
@@ -248,6 +331,7 @@ interface RegleCommonStatus<TValue = any> {
248
331
  $validate(): Promise<boolean>;
249
332
  $unwatch(): void;
250
333
  $watch(): void;
334
+ $clearExternalErrors(): void;
251
335
  }
252
336
  /**
253
337
  * @public
@@ -269,17 +353,18 @@ type RegleRuleStatus<TValue = any, TParams extends any[] = any[]> = {
269
353
  * @reference {@link RegleRuleStatus}
270
354
  */
271
355
  interface $InternalRegleRuleStatus {
272
- readonly $type: string;
273
- readonly $message: string;
274
- readonly $active: boolean;
275
- readonly $valid: boolean;
276
- readonly $pending: boolean;
277
- readonly $path: string;
278
- $validator(value: any, ...args: any[]): boolean | Promise<boolean>;
356
+ $type: string;
357
+ $message: string;
358
+ $active: boolean;
359
+ $valid: boolean;
360
+ $pending: boolean;
361
+ $path: string;
362
+ $externalErrors?: string[];
363
+ $params?: any[];
364
+ $validator(value: any, ...args: any[]): RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>;
279
365
  $validate(): Promise<boolean>;
280
366
  $unwatch(): void;
281
367
  $watch(): void;
282
- readonly $params?: any[];
283
368
  }
284
369
  /**
285
370
  * @public
@@ -302,13 +387,247 @@ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fi
302
387
  };
303
388
  }
304
389
 
390
+ /**
391
+ Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
392
+
393
+ @category Type
394
+ */
395
+ type Primitive =
396
+ | null
397
+ | undefined
398
+ | string
399
+ | number
400
+ | boolean
401
+ | symbol
402
+ | bigint;
403
+
404
+ declare global {
405
+ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged.
406
+ interface SymbolConstructor {
407
+ readonly observable: symbol;
408
+ }
409
+ }
410
+
411
+ /**
412
+ Matches any primitive, `void`, `Date`, or `RegExp` value.
413
+ */
414
+ type BuiltIns = Primitive | void | Date | RegExp;
415
+
416
+ /**
417
+ Test if the given function has multiple call signatures.
418
+
419
+ Needed to handle the case of a single call signature with properties.
420
+
421
+ Multiple call signatures cannot currently be supported due to a TypeScript limitation.
422
+ @see https://github.com/microsoft/TypeScript/issues/29732
423
+ */
424
+ type HasMultipleCallSignatures<T extends (...arguments_: any[]) => unknown> =
425
+ T extends {(...arguments_: infer A): unknown; (...arguments_: any[]): unknown}
426
+ ? unknown[] extends A
427
+ ? false
428
+ : true
429
+ : false;
430
+
431
+ /**
432
+ @see PartialDeep
433
+ */
434
+ type PartialDeepOptions = {
435
+ /**
436
+ Whether to affect the individual elements of arrays and tuples.
437
+
438
+ @default false
439
+ */
440
+ readonly recurseIntoArrays?: boolean;
441
+ };
442
+
443
+ /**
444
+ Create a type from another type with all keys and nested keys set to optional.
445
+
446
+ Use-cases:
447
+ - Merging a default settings/config object with another object, the second object would be a deep partial of the default object.
448
+ - Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.
449
+
450
+ @example
451
+ ```
452
+ import type {PartialDeep} from 'type-fest';
453
+
454
+ const settings: Settings = {
455
+ textEditor: {
456
+ fontSize: 14;
457
+ fontColor: '#000000';
458
+ fontWeight: 400;
459
+ }
460
+ autocomplete: false;
461
+ autosave: true;
462
+ };
463
+
464
+ const applySavedSettings = (savedSettings: PartialDeep<Settings>) => {
465
+ return {...settings, ...savedSettings};
466
+ }
467
+
468
+ settings = applySavedSettings({textEditor: {fontWeight: 500}});
469
+ ```
470
+
471
+ By default, this does not affect elements in array and tuple types. You can change this by passing `{recurseIntoArrays: true}` as the second type argument:
472
+
473
+ ```
474
+ import type {PartialDeep} from 'type-fest';
475
+
476
+ interface Settings {
477
+ languages: string[];
478
+ }
479
+
480
+ const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {
481
+ languages: [undefined]
482
+ };
483
+ ```
484
+
485
+ @category Object
486
+ @category Array
487
+ @category Set
488
+ @category Map
489
+ */
490
+ type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIns | (((...arguments_: any[]) => unknown)) | (new (...arguments_: any[]) => unknown)
491
+ ? T
492
+ : T extends Map<infer KeyType, infer ValueType>
493
+ ? PartialMapDeep<KeyType, ValueType, Options>
494
+ : T extends Set<infer ItemType>
495
+ ? PartialSetDeep<ItemType, Options>
496
+ : T extends ReadonlyMap<infer KeyType, infer ValueType>
497
+ ? PartialReadonlyMapDeep<KeyType, ValueType, Options>
498
+ : T extends ReadonlySet<infer ItemType>
499
+ ? PartialReadonlySetDeep<ItemType, Options>
500
+ : T extends object
501
+ ? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
502
+ ? Options['recurseIntoArrays'] extends true
503
+ ? ItemType[] extends T // Test for arrays (non-tuples) specifically
504
+ ? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
505
+ ? ReadonlyArray<PartialDeep<ItemType | undefined, Options>>
506
+ : Array<PartialDeep<ItemType | undefined, Options>>
507
+ : PartialObjectDeep<T, Options> // Tuples behave properly
508
+ : T // If they don't opt into array testing, just use the original type
509
+ : PartialObjectDeep<T, Options>
510
+ : unknown;
511
+
512
+ /**
513
+ Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
514
+ */
515
+ type PartialMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> = {} & Map<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>>;
516
+
517
+ /**
518
+ Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
519
+ */
520
+ type PartialSetDeep<T, Options extends PartialDeepOptions> = {} & Set<PartialDeep<T, Options>>;
521
+
522
+ /**
523
+ Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
524
+ */
525
+ type PartialReadonlyMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> = {} & ReadonlyMap<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>>;
526
+
527
+ /**
528
+ Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
529
+ */
530
+ type PartialReadonlySetDeep<T, Options extends PartialDeepOptions> = {} & ReadonlySet<PartialDeep<T, Options>>;
531
+
532
+ /**
533
+ Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
534
+ */
535
+ type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOptions> = {
536
+ [KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType], Options>
537
+ };
538
+
539
+ type ExcludeUndefined<T> = Exclude<T, undefined>;
540
+
541
+ /**
542
+ Create a type from another type with all keys and nested keys set to required.
543
+
544
+ Use-cases:
545
+ - Creating optional configuration interfaces where the underlying implementation still requires all options to be fully specified.
546
+ - Modeling the resulting type after a deep merge with a set of defaults.
547
+
548
+ @example
549
+ ```
550
+ import type {RequiredDeep} from 'type-fest';
551
+
552
+ type Settings = {
553
+ textEditor?: {
554
+ fontSize?: number | undefined;
555
+ fontColor?: string | undefined;
556
+ fontWeight?: number | undefined;
557
+ }
558
+ autocomplete?: boolean | undefined;
559
+ autosave?: boolean | undefined;
560
+ };
561
+
562
+ type RequiredSettings = RequiredDeep<Settings>;
563
+ // type RequiredSettings = {
564
+ // textEditor: {
565
+ // fontSize: number;
566
+ // fontColor: string;
567
+ // fontWeight: number;
568
+ // }
569
+ // autocomplete: boolean;
570
+ // autosave: boolean;
571
+ // }
572
+ ```
573
+
574
+ Note that types containing overloaded functions are not made deeply required due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
575
+
576
+ @category Utilities
577
+ @category Object
578
+ @category Array
579
+ @category Set
580
+ @category Map
581
+ */
582
+ type RequiredDeep<T, E extends ExcludeUndefined<T> = ExcludeUndefined<T>> = E extends BuiltIns
583
+ ? E
584
+ : E extends Map<infer KeyType, infer ValueType>
585
+ ? Map<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
586
+ : E extends Set<infer ItemType>
587
+ ? Set<RequiredDeep<ItemType>>
588
+ : E extends ReadonlyMap<infer KeyType, infer ValueType>
589
+ ? ReadonlyMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
590
+ : E extends ReadonlySet<infer ItemType>
591
+ ? ReadonlySet<RequiredDeep<ItemType>>
592
+ : E extends WeakMap<infer KeyType, infer ValueType>
593
+ ? WeakMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
594
+ : E extends WeakSet<infer ItemType>
595
+ ? WeakSet<RequiredDeep<ItemType>>
596
+ : E extends Promise<infer ValueType>
597
+ ? Promise<RequiredDeep<ValueType>>
598
+ : E extends (...arguments_: any[]) => unknown
599
+ ? {} extends RequiredObjectDeep<E>
600
+ ? E
601
+ : HasMultipleCallSignatures<E> extends true
602
+ ? E
603
+ : ((...arguments_: Parameters<E>) => ReturnType<E>) & RequiredObjectDeep<E>
604
+ : E extends object
605
+ ? E extends Array<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
606
+ ? ItemType[] extends E // Test for arrays (non-tuples) specifically
607
+ ? Array<RequiredDeep<ItemType>> // Recreate relevant array type to prevent eager evaluation of circular reference
608
+ : RequiredObjectDeep<E> // Tuples behave properly
609
+ : RequiredObjectDeep<E>
610
+ : unknown;
611
+
612
+ type RequiredObjectDeep<ObjectType extends object> = {
613
+ [KeyType in keyof ObjectType]-?: RequiredDeep<ObjectType[KeyType]>
614
+ };
615
+
305
616
  interface Regle<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> {
306
- state: Ref<PartialDeep<TState>>;
617
+ $state: Ref<PartialDeep<TState>>;
307
618
  $regle: RegleStatus<TState, TRules>;
308
- errors: RegleErrorTree<TRules>;
619
+ /** Show active errors based on your behaviour options (lazy, autoDirty)
620
+ * It allow you to skip scouting the `$regle` object
621
+ */
622
+ $errors: RegleErrorTree<TRules>;
623
+ $valid: ComputedRef<boolean>;
624
+ $invalid: ComputedRef<boolean>;
309
625
  resetForm: () => void;
310
626
  validateForm: () => Promise<false | DeepSafeFormState<TState, TRules>>;
311
627
  }
628
+ type DeepReactiveState<T extends Record<string, any>> = {
629
+ [K in keyof T]: MaybeRef<T[K]>;
630
+ };
312
631
  type DeepSafeFormState<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> = {
313
632
  [K in keyof TState as [SafeProperty<TState[K], TRules[K]>] extends [never] ? K : never]?: [
314
633
  SafeProperty<TState[K], TRules[K]>
@@ -335,8 +654,47 @@ interface RegleBehaviourOptions {
335
654
  */
336
655
  rewardEarly?: boolean;
337
656
  }
657
+ interface LocalRegleBehaviourOptions<TState extends Record<string, any>> {
658
+ $externalErrors?: MaybeRef<RegleExternalErrorTree<TState>>;
659
+ }
660
+ type ResolvedRegleBehaviourOptions = DeepMaybeRef$1<RequiredDeep<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<Record<string, any>>;
338
661
 
339
- declare function createRule<TValue extends any, TParams extends any[] = [], TType extends string = string>(definition: RegleRuleInit<TValue, TParams, TType>): InferRegleRule<TValue, TParams>;
662
+ /**
663
+ * @description
664
+ * Create a typed custom rule that can be used like default validators.
665
+ * It can also be declared in the global options
666
+ *
667
+ * It will automatically detect if the rule is async
668
+ *
669
+ * @typeParam TValue - The input value the rule should receive
670
+ * @typeParam TParams - Tuple declaration of the rule parameters (if any)
671
+ *
672
+ * @param definition - The rule processors object
673
+ *
674
+ * @returns A rule definition that can be callable depending on params presence
675
+ *
676
+ * @exemple
677
+ *
678
+ * ```ts
679
+ * // Create a simple rule with no params
680
+ * import {ruleHelpers} from '@regle/validators';
681
+ *
682
+ * export const isFoo = createRule<string>({
683
+ * validator(value) {
684
+ * if (ruleHelpers.isFilled(value)) {
685
+ * return value === 'foo';
686
+ * }
687
+ * return true
688
+ * },
689
+ * message: "The value should be 'foo'"
690
+ * })
691
+ * ```
692
+ */
693
+ declare function createRule<TValue extends any, TParams extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TMetaData extends RegleRuleMetadataDefinition = TReturn extends Promise<infer M> ? M : TReturn, TAsync extends boolean = TReturn extends Promise<any> ? true : false>(definition: RegleRuleInit<TValue, TParams, TReturn, TMetaData, TAsync>): InferRegleRule<TValue, TParams, TAsync, TMetaData>;
694
+ declare function defineType<TValue extends any = unknown, TParams extends any[] = []>(ruleName: string): {
695
+ value: TValue;
696
+ params: TParams;
697
+ };
340
698
 
341
699
  /**
342
700
  * Returns a clean list of parameters
@@ -344,6 +702,8 @@ declare function createRule<TValue extends any, TParams extends any[] = [], TTyp
344
702
  */
345
703
  declare function unwrapRuleParameters<TParams extends any[]>(params: ParamDecl[]): TParams;
346
704
 
705
+ declare const useRegle: <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, Partial<AllRulesDeclarations>>>(state: Ref<TState> | DeepReactiveState$1<TState>, rulesFactory: ComputedRef<TRules> | (() => TRules), options?: (Partial<DeepMaybeRef$1<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<TState>) | undefined) => Regle<TState, TRules>;
706
+
347
707
  /**
348
708
  * Root function that allows you to define project-wise all your custom validators or overwrite default ones
349
709
  *
@@ -351,9 +711,9 @@ declare function unwrapRuleParameters<TParams extends any[]>(params: ParamDecl[]
351
711
  *
352
712
  * @param customRules
353
713
  */
354
- declare function defineRegleOptions<TCustomRules extends Partial<AllRulesDeclarations>>({ rules, options, }: {
714
+ declare function defineRegleConfig<TCustomRules extends Partial<AllRulesDeclarations>>({ rules, options, }: {
355
715
  rules?: () => TCustomRules;
356
716
  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>;
717
+ }): <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, Partial<AllRulesDeclarations> & TCustomRules>>(state: vue.Ref<TState> | DeepReactiveState<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules), options?: (Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<TState>) | undefined) => Regle<TState, TRules>;
358
718
 
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 };
719
+ export { type $InternalExternalRegleErrors, type $InternalFormPropertyTypes, type $InternalRegleCollectionRuleDecl, type $InternalRegleCollectionStatus, type $InternalRegleErrors, type $InternalRegleFieldStatus, type $InternalReglePartialValidationTree, type $InternalRegleRuleDecl, type $InternalRegleRuleDefinition, type $InternalRegleRuleInit, type $InternalRegleRuleMetadataConsumer, type $InternalRegleRuleStatus, type $InternalRegleStatus, type $InternalRegleStatusType, type AllRulesDeclarations, type ArrayElement, type CustomRulesDeclarationTree, type DataType, type DeepMaybeRef, type ExcludeByType, type ExcludeFromTuple, type FormRuleDeclaration, type InferRegleRule, type InferRegleStatusType, type InlinePromiseRuleDeclaration, type InlineRuleDeclaration, InternalRuleType, type LocalRegleBehaviourOptions, type Maybe, type MaybeNull, type ParamDecl, type Regle, type RegleBehaviourOptions, type RegleCollectionErrors, type RegleCollectionRuleDecl, type RegleCollectionRuleDefinition, type RegleCollectionStatus, type RegleCommonStatus, type RegleErrorTree, type RegleExternalCollectionErrors, type RegleExternalErrorTree, type RegleExternalValidationErrors, type RegleFieldStatus, type RegleFormPropertyType, type RegleInternalRuleDefs, type ReglePartialValidationTree, type RegleRuleCore, type RegleRuleDecl, type RegleRuleDefinition, type RegleRuleDefinitionProcessor, type RegleRuleDefinitionWithMetadataProcessor, type RegleRuleInit, type RegleRuleMetadataConsumer, type RegleRuleMetadataDefinition, type RegleRuleRaw, type RegleRuleStatus, type RegleRuleWithParamsDefinition, type RegleStatus, type RegleUniversalParams, type RegleValidationErrors, type ResolvedRegleBehaviourOptions, type UnrefTuple, type UnwrapRegleUniversalParams, createRule, defineRegleConfig, defineType, unwrapRuleParameters, useRegle };