@regle/core 0.0.12 → 0.0.13
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.cjs +35 -1311
- package/dist/index.d.cts +54 -360
- package/dist/index.d.ts +54 -360
- package/dist/index.js +35 -1279
- package/package.json +21 -19
package/dist/index.d.cts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { MaybeRef, Ref, ComputedRef } from 'vue';
|
|
1
|
+
import { MaybeRef, ComputedRef, Ref, UnwrapNestedRefs } from 'vue';
|
|
3
2
|
|
|
4
|
-
type
|
|
5
|
-
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>] : [];
|
|
6
|
-
type UnrefTuple<T extends readonly any[]> = T extends [infer F, ...infer R] ? [NonNullable<F>] extends [MaybeRef<infer U>] ? [U, ...UnrefTuple<R>] : [F, ...UnrefTuple<R>] : [];
|
|
7
|
-
|
|
8
|
-
type Maybe<T> = T | null | undefined;
|
|
9
|
-
type MaybeNull<T> = T | null;
|
|
3
|
+
type Maybe<T = any> = T | null | undefined;
|
|
10
4
|
type DeepMaybeRef<T extends Record<string, any>> = {
|
|
11
5
|
[K in keyof T]: MaybeRef<T[K]>;
|
|
12
6
|
};
|
|
@@ -14,6 +8,8 @@ type ExcludeByType<T, U> = {
|
|
|
14
8
|
[K in keyof T as T[K] extends U ? never : K]: T[K] extends U ? never : T[K];
|
|
15
9
|
};
|
|
16
10
|
|
|
11
|
+
type ArrayElement<T> = T extends Array<infer U> ? U : never;
|
|
12
|
+
|
|
17
13
|
type ParamDecl<T = any> = MaybeRef<Maybe<T>> | (() => Maybe<T>);
|
|
18
14
|
type CreateFn<T extends any[]> = (...args: T) => any;
|
|
19
15
|
/**
|
|
@@ -51,22 +47,12 @@ declare enum InternalRuleType {
|
|
|
51
47
|
/**
|
|
52
48
|
* Returned typed of rules created with `createRule`
|
|
53
49
|
* */
|
|
54
|
-
interface RegleRuleDefinition<TValue extends any = any, TParams extends any[] = [], TAsync extends boolean = boolean, TMetaData extends RegleRuleMetadataDefinition = RegleRuleMetadataDefinition> extends RegleInternalRuleDefs<
|
|
55
|
-
validator: RegleRuleDefinitionProcessor<
|
|
56
|
-
message: RegleRuleDefinitionWithMetadataProcessor<
|
|
57
|
-
active: RegleRuleDefinitionWithMetadataProcessor<
|
|
58
|
-
type?: string;
|
|
59
|
-
exec: (value: Maybe<TValue>) => TAsync extends false ? TMetaData : Promise<TMetaData>;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* @internal
|
|
63
|
-
* */
|
|
64
|
-
interface $InternalRegleRuleDefinition extends RegleInternalRuleDefs<any, any, any> {
|
|
65
|
-
validator: RegleRuleDefinitionProcessor;
|
|
66
|
-
message: RegleRuleDefinitionWithMetadataProcessor<any, any, any>;
|
|
67
|
-
active: RegleRuleDefinitionWithMetadataProcessor<any, any, any>;
|
|
50
|
+
interface RegleRuleDefinition<TValue extends any = any, TParams extends any[] = [], TAsync extends boolean = boolean, TMetaData extends RegleRuleMetadataDefinition = RegleRuleMetadataDefinition, TFilteredValue extends any = TValue extends Date & File & infer M ? M : TValue> extends RegleInternalRuleDefs<TFilteredValue, TParams, TAsync, TMetaData> {
|
|
51
|
+
validator: RegleRuleDefinitionProcessor<TFilteredValue, TParams, TAsync extends false ? TMetaData : Promise<TMetaData>>;
|
|
52
|
+
message: RegleRuleDefinitionWithMetadataProcessor<TFilteredValue, PossibleRegleRuleMetadataConsumer, string | string[]>;
|
|
53
|
+
active: RegleRuleDefinitionWithMetadataProcessor<TFilteredValue, PossibleRegleRuleMetadataConsumer, boolean>;
|
|
68
54
|
type?: string;
|
|
69
|
-
exec: (value:
|
|
55
|
+
exec: (value: Maybe<TFilteredValue>) => TAsync extends false ? TMetaData : Promise<TMetaData>;
|
|
70
56
|
}
|
|
71
57
|
/**
|
|
72
58
|
* Rules with params created with `createRules` are callable while being customizable
|
|
@@ -74,33 +60,27 @@ interface $InternalRegleRuleDefinition extends RegleInternalRuleDefs<any, any, a
|
|
|
74
60
|
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> {
|
|
75
61
|
(...params: RegleUniversalParams<TParams>): RegleRuleDefinition<TValue, TParams, TAsync, TMetadata>;
|
|
76
62
|
}
|
|
63
|
+
type RegleRuleMetadataExtended = {
|
|
64
|
+
$valid: boolean;
|
|
65
|
+
[x: string]: any;
|
|
66
|
+
};
|
|
77
67
|
/**
|
|
78
68
|
* Define a rule Metadata definition
|
|
79
69
|
*/
|
|
80
|
-
type RegleRuleMetadataDefinition =
|
|
81
|
-
$valid: boolean;
|
|
82
|
-
[x: string]: any;
|
|
83
|
-
} | boolean;
|
|
70
|
+
type RegleRuleMetadataDefinition = RegleRuleMetadataExtended | boolean;
|
|
84
71
|
type DefaultMetadataProperties = Pick<ExcludeByType<RegleCommonStatus, Function>, '$invalid'>;
|
|
85
72
|
/**
|
|
86
73
|
* Will be used to consumme metadata on related helpers and rule status
|
|
87
74
|
*/
|
|
88
75
|
type RegleRuleMetadataConsumer<TParams extends any[] = [], TMetadata extends RegleRuleMetadataDefinition = boolean> = DefaultMetadataProperties & (TParams extends never ? {} : {
|
|
89
76
|
$params: TParams;
|
|
90
|
-
}) & (TMetadata extends boolean ?
|
|
77
|
+
}) & (Exclude<TMetadata, boolean> extends RegleRuleMetadataExtended ? TMetadata extends boolean ? Partial<Omit<Exclude<TMetadata, boolean>, '$valid'>> : Omit<Exclude<TMetadata, boolean>, '$valid'> : {});
|
|
91
78
|
/**
|
|
92
79
|
* Will be used to consumme metadata on related helpers and rule status
|
|
93
80
|
*/
|
|
94
81
|
type PossibleRegleRuleMetadataConsumer = DefaultMetadataProperties & {
|
|
95
82
|
$params?: any[];
|
|
96
83
|
};
|
|
97
|
-
/**
|
|
98
|
-
* @internal
|
|
99
|
-
*/
|
|
100
|
-
type $InternalRegleRuleMetadataConsumer = DefaultMetadataProperties & {
|
|
101
|
-
$params?: any[];
|
|
102
|
-
[x: string]: any;
|
|
103
|
-
};
|
|
104
84
|
/**
|
|
105
85
|
* Generic types for a created RegleRule
|
|
106
86
|
*/
|
|
@@ -142,16 +122,6 @@ interface RegleRuleCore<TValue extends any, TParams extends any[] = [], TAsync e
|
|
|
142
122
|
active?: boolean | ((value: Maybe<TValue>, metadata: RegleRuleMetadataConsumer<TParams, TMetadata>) => boolean);
|
|
143
123
|
type?: string;
|
|
144
124
|
}
|
|
145
|
-
/**
|
|
146
|
-
* @internal
|
|
147
|
-
* createRule arguments options
|
|
148
|
-
*/
|
|
149
|
-
interface $InternalRegleRuleInit {
|
|
150
|
-
validator: (value: any, ...args: any[]) => RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>;
|
|
151
|
-
message: string | ((value: any, metadata: $InternalRegleRuleMetadataConsumer) => string | string[]);
|
|
152
|
-
active?: boolean | ((value: any, metadata: $InternalRegleRuleMetadataConsumer) => boolean);
|
|
153
|
-
type?: string;
|
|
154
|
-
}
|
|
155
125
|
|
|
156
126
|
type DefaultValidators = {
|
|
157
127
|
maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
|
|
@@ -182,20 +152,6 @@ type CustomRulesDeclarationTree = {
|
|
|
182
152
|
};
|
|
183
153
|
type AllRulesDeclarations = CustomRulesDeclarationTree & DefaultValidators;
|
|
184
154
|
|
|
185
|
-
/**
|
|
186
|
-
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
|
|
187
|
-
|
|
188
|
-
@category Type
|
|
189
|
-
*/
|
|
190
|
-
type Primitive =
|
|
191
|
-
| null
|
|
192
|
-
| undefined
|
|
193
|
-
| string
|
|
194
|
-
| number
|
|
195
|
-
| boolean
|
|
196
|
-
| symbol
|
|
197
|
-
| bigint;
|
|
198
|
-
|
|
199
155
|
declare global {
|
|
200
156
|
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged.
|
|
201
157
|
interface SymbolConstructor {
|
|
@@ -203,227 +159,50 @@ declare global {
|
|
|
203
159
|
}
|
|
204
160
|
}
|
|
205
161
|
|
|
206
|
-
|
|
207
|
-
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
208
|
-
*/
|
|
209
|
-
type BuiltIns = Primitive | void | Date | RegExp;
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
Test if the given function has multiple call signatures.
|
|
213
|
-
|
|
214
|
-
Needed to handle the case of a single call signature with properties.
|
|
215
|
-
|
|
216
|
-
Multiple call signatures cannot currently be supported due to a TypeScript limitation.
|
|
217
|
-
@see https://github.com/microsoft/TypeScript/issues/29732
|
|
218
|
-
*/
|
|
219
|
-
type HasMultipleCallSignatures<T extends (...arguments_: any[]) => unknown> =
|
|
220
|
-
T extends {(...arguments_: infer A): unknown; (...arguments_: any[]): unknown}
|
|
221
|
-
? unknown[] extends A
|
|
222
|
-
? false
|
|
223
|
-
: true
|
|
224
|
-
: false;
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
@see PartialDeep
|
|
228
|
-
*/
|
|
229
|
-
type PartialDeepOptions = {
|
|
230
|
-
/**
|
|
231
|
-
Whether to affect the individual elements of arrays and tuples.
|
|
232
|
-
|
|
233
|
-
@default false
|
|
234
|
-
*/
|
|
235
|
-
readonly recurseIntoArrays?: boolean;
|
|
236
|
-
};
|
|
162
|
+
declare const emptyObjectSymbol: unique symbol;
|
|
237
163
|
|
|
238
164
|
/**
|
|
239
|
-
|
|
165
|
+
Represents a strictly empty plain object, the `{}` value.
|
|
240
166
|
|
|
241
|
-
|
|
242
|
-
- Merging a default settings/config object with another object, the second object would be a deep partial of the default object.
|
|
243
|
-
- Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.
|
|
167
|
+
When you annotate something as the type `{}`, it can be anything except `null` and `undefined`. This means that you cannot use `{}` to represent an empty plain object ([read more](https://stackoverflow.com/questions/47339869/typescript-empty-object-and-any-difference/52193484#52193484)).
|
|
244
168
|
|
|
245
169
|
@example
|
|
246
170
|
```
|
|
247
|
-
import type {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
const
|
|
260
|
-
return {...settings, ...savedSettings};
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
settings = applySavedSettings({textEditor: {fontWeight: 500}});
|
|
171
|
+
import type {EmptyObject} from 'type-fest';
|
|
172
|
+
|
|
173
|
+
// The following illustrates the problem with `{}`.
|
|
174
|
+
const foo1: {} = {}; // Pass
|
|
175
|
+
const foo2: {} = []; // Pass
|
|
176
|
+
const foo3: {} = 42; // Pass
|
|
177
|
+
const foo4: {} = {a: 1}; // Pass
|
|
178
|
+
|
|
179
|
+
// With `EmptyObject` only the first case is valid.
|
|
180
|
+
const bar1: EmptyObject = {}; // Pass
|
|
181
|
+
const bar2: EmptyObject = 42; // Fail
|
|
182
|
+
const bar3: EmptyObject = []; // Fail
|
|
183
|
+
const bar4: EmptyObject = {a: 1}; // Fail
|
|
264
184
|
```
|
|
265
185
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
```
|
|
269
|
-
import type {PartialDeep} from 'type-fest';
|
|
270
|
-
|
|
271
|
-
interface Settings {
|
|
272
|
-
languages: string[];
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {
|
|
276
|
-
languages: [undefined]
|
|
277
|
-
};
|
|
278
|
-
```
|
|
186
|
+
Unfortunately, `Record<string, never>`, `Record<keyof any, never>` and `Record<never, never>` do not work. See {@link https://github.com/sindresorhus/type-fest/issues/395 #395}.
|
|
279
187
|
|
|
280
188
|
@category Object
|
|
281
|
-
@category Array
|
|
282
|
-
@category Set
|
|
283
|
-
@category Map
|
|
284
|
-
*/
|
|
285
|
-
type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIns | (((...arguments_: any[]) => unknown)) | (new (...arguments_: any[]) => unknown)
|
|
286
|
-
? T
|
|
287
|
-
: T extends Map<infer KeyType, infer ValueType>
|
|
288
|
-
? PartialMapDeep<KeyType, ValueType, Options>
|
|
289
|
-
: T extends Set<infer ItemType>
|
|
290
|
-
? PartialSetDeep<ItemType, Options>
|
|
291
|
-
: T extends ReadonlyMap<infer KeyType, infer ValueType>
|
|
292
|
-
? PartialReadonlyMapDeep<KeyType, ValueType, Options>
|
|
293
|
-
: T extends ReadonlySet<infer ItemType>
|
|
294
|
-
? PartialReadonlySetDeep<ItemType, Options>
|
|
295
|
-
: T extends object
|
|
296
|
-
? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
|
|
297
|
-
? Options['recurseIntoArrays'] extends true
|
|
298
|
-
? ItemType[] extends T // Test for arrays (non-tuples) specifically
|
|
299
|
-
? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
|
|
300
|
-
? ReadonlyArray<PartialDeep<ItemType | undefined, Options>>
|
|
301
|
-
: Array<PartialDeep<ItemType | undefined, Options>>
|
|
302
|
-
: PartialObjectDeep<T, Options> // Tuples behave properly
|
|
303
|
-
: T // If they don't opt into array testing, just use the original type
|
|
304
|
-
: PartialObjectDeep<T, Options>
|
|
305
|
-
: unknown;
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
|
|
309
189
|
*/
|
|
310
|
-
type
|
|
190
|
+
type EmptyObject = {[emptyObjectSymbol]?: never};
|
|
311
191
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
*/
|
|
315
|
-
type PartialSetDeep<T, Options extends PartialDeepOptions> = {} & Set<PartialDeep<T, Options>>;
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
|
|
319
|
-
*/
|
|
320
|
-
type PartialReadonlyMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> = {} & ReadonlyMap<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>>;
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
|
|
324
|
-
*/
|
|
325
|
-
type PartialReadonlySetDeep<T, Options extends PartialDeepOptions> = {} & ReadonlySet<PartialDeep<T, Options>>;
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
|
|
329
|
-
*/
|
|
330
|
-
type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOptions> = {
|
|
331
|
-
[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType], Options>
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
type ExcludeUndefined<T> = Exclude<T, undefined>;
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
Create a type from another type with all keys and nested keys set to required.
|
|
338
|
-
|
|
339
|
-
Use-cases:
|
|
340
|
-
- Creating optional configuration interfaces where the underlying implementation still requires all options to be fully specified.
|
|
341
|
-
- Modeling the resulting type after a deep merge with a set of defaults.
|
|
342
|
-
|
|
343
|
-
@example
|
|
344
|
-
```
|
|
345
|
-
import type {RequiredDeep} from 'type-fest';
|
|
346
|
-
|
|
347
|
-
type Settings = {
|
|
348
|
-
textEditor?: {
|
|
349
|
-
fontSize?: number | undefined;
|
|
350
|
-
fontColor?: string | undefined;
|
|
351
|
-
fontWeight?: number | undefined;
|
|
352
|
-
}
|
|
353
|
-
autocomplete?: boolean | undefined;
|
|
354
|
-
autosave?: boolean | undefined;
|
|
355
|
-
};
|
|
356
|
-
|
|
357
|
-
type RequiredSettings = RequiredDeep<Settings>;
|
|
358
|
-
// type RequiredSettings = {
|
|
359
|
-
// textEditor: {
|
|
360
|
-
// fontSize: number;
|
|
361
|
-
// fontColor: string;
|
|
362
|
-
// fontWeight: number;
|
|
363
|
-
// }
|
|
364
|
-
// autocomplete: boolean;
|
|
365
|
-
// autosave: boolean;
|
|
366
|
-
// }
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
Note that types containing overloaded functions are not made deeply required due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
|
|
370
|
-
|
|
371
|
-
@category Utilities
|
|
372
|
-
@category Object
|
|
373
|
-
@category Array
|
|
374
|
-
@category Set
|
|
375
|
-
@category Map
|
|
376
|
-
*/
|
|
377
|
-
type RequiredDeep<T, E extends ExcludeUndefined<T> = ExcludeUndefined<T>> = E extends BuiltIns
|
|
378
|
-
? E
|
|
379
|
-
: E extends Map<infer KeyType, infer ValueType>
|
|
380
|
-
? Map<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
|
|
381
|
-
: E extends Set<infer ItemType>
|
|
382
|
-
? Set<RequiredDeep<ItemType>>
|
|
383
|
-
: E extends ReadonlyMap<infer KeyType, infer ValueType>
|
|
384
|
-
? ReadonlyMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
|
|
385
|
-
: E extends ReadonlySet<infer ItemType>
|
|
386
|
-
? ReadonlySet<RequiredDeep<ItemType>>
|
|
387
|
-
: E extends WeakMap<infer KeyType, infer ValueType>
|
|
388
|
-
? WeakMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
|
|
389
|
-
: E extends WeakSet<infer ItemType>
|
|
390
|
-
? WeakSet<RequiredDeep<ItemType>>
|
|
391
|
-
: E extends Promise<infer ValueType>
|
|
392
|
-
? Promise<RequiredDeep<ValueType>>
|
|
393
|
-
: E extends (...arguments_: any[]) => unknown
|
|
394
|
-
? {} extends RequiredObjectDeep<E>
|
|
395
|
-
? E
|
|
396
|
-
: HasMultipleCallSignatures<E> extends true
|
|
397
|
-
? E
|
|
398
|
-
: ((...arguments_: Parameters<E>) => ReturnType<E>) & RequiredObjectDeep<E>
|
|
399
|
-
: E extends object
|
|
400
|
-
? E extends Array<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
|
|
401
|
-
? ItemType[] extends E // Test for arrays (non-tuples) specifically
|
|
402
|
-
? Array<RequiredDeep<ItemType>> // Recreate relevant array type to prevent eager evaluation of circular reference
|
|
403
|
-
: RequiredObjectDeep<E> // Tuples behave properly
|
|
404
|
-
: RequiredObjectDeep<E>
|
|
405
|
-
: unknown;
|
|
406
|
-
|
|
407
|
-
type RequiredObjectDeep<ObjectType extends object> = {
|
|
408
|
-
[KeyType in keyof ObjectType]-?: RequiredDeep<ObjectType[KeyType]>
|
|
409
|
-
};
|
|
410
|
-
|
|
411
|
-
interface Regle<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> {
|
|
412
|
-
$state: Ref<PartialDeep<TState>>;
|
|
413
|
-
$regle: RegleStatus<TState, TRules>;
|
|
192
|
+
interface Regle<TState extends Record<string, any> = EmptyObject, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree> = EmptyObject> {
|
|
193
|
+
regle: RegleStatus<TState, TRules>;
|
|
414
194
|
/** Show active errors based on your behaviour options (lazy, autoDirty)
|
|
415
195
|
* It allow you to skip scouting the `$regle` object
|
|
416
196
|
*/
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
$invalid: ComputedRef<boolean>;
|
|
197
|
+
errors: RegleErrorTree<TRules>;
|
|
198
|
+
invalid: ComputedRef<boolean>;
|
|
420
199
|
resetForm: () => void;
|
|
421
200
|
validateForm: () => Promise<false | DeepSafeFormState<TState, TRules>>;
|
|
422
201
|
}
|
|
423
202
|
type DeepReactiveState<T extends Record<string, any>> = {
|
|
424
203
|
[K in keyof T]: MaybeRef<T[K]>;
|
|
425
204
|
};
|
|
426
|
-
type DeepSafeFormState<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> = {
|
|
205
|
+
type DeepSafeFormState<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> = [unknown] extends [TState] ? {} : {
|
|
427
206
|
[K in keyof TState as [SafeProperty<TState[K], TRules[K]>] extends [never] ? K : never]?: [
|
|
428
207
|
SafeProperty<TState[K], TRules[K]>
|
|
429
208
|
] extends [never] ? TState[K] : SafeProperty<TState[K], TRules[K]>;
|
|
@@ -457,16 +236,9 @@ interface LocalRegleBehaviourOptions<TState extends Record<string, any>> {
|
|
|
457
236
|
type FieldRegleBehaviourOptions = AddDollarToOptions<RegleBehaviourOptions> & {
|
|
458
237
|
$debounce?: number;
|
|
459
238
|
};
|
|
460
|
-
type ResolvedRegleBehaviourOptions = DeepMaybeRef<RequiredDeep<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<Record<string, any>>;
|
|
461
239
|
type AddDollarToOptions<T extends Record<string, any>> = {
|
|
462
240
|
[K in keyof T as `$${string & K}`]: T[K];
|
|
463
241
|
};
|
|
464
|
-
type FilterDollarProperties<T extends Record<string, any>> = {
|
|
465
|
-
[K in keyof T as K extends `$${string}` ? never : K]: T[K];
|
|
466
|
-
};
|
|
467
|
-
type PickDollarProperties<T extends Record<string, any>> = {
|
|
468
|
-
[K in keyof T as K extends `$${string}` ? K : never]: T[K];
|
|
469
|
-
};
|
|
470
242
|
|
|
471
243
|
/**
|
|
472
244
|
* @public
|
|
@@ -481,21 +253,15 @@ type RegleValidationTree<TForm extends Record<string, any>, TCustomRules extends
|
|
|
481
253
|
[TKey in keyof TForm]: RegleFormPropertyType<TForm[TKey], TCustomRules>;
|
|
482
254
|
};
|
|
483
255
|
/**
|
|
484
|
-
* @
|
|
485
|
-
* @reference {@link ReglePartialValidationTree}
|
|
256
|
+
* @public
|
|
486
257
|
*/
|
|
487
|
-
type
|
|
488
|
-
[
|
|
258
|
+
type RegleComputedRules<TForm extends MaybeRef<Record<string, any>>, TCustomRules extends Partial<AllRulesDeclarations> | Regle<any, any> = Partial<AllRulesDeclarations>, TState = TForm extends Ref<infer R> ? R : TForm, TCustom = TCustomRules extends Regle<any, infer R> ? R extends ReglePartialValidationTree<any, infer C> ? C : Partial<AllRulesDeclarations> : TCustomRules> = {
|
|
259
|
+
[TKey in keyof TState]?: RegleFormPropertyType<TState[TKey], TCustom extends Partial<AllRulesDeclarations> ? TCustom : {}>;
|
|
489
260
|
};
|
|
490
261
|
/**
|
|
491
262
|
* @public
|
|
492
263
|
*/
|
|
493
|
-
type RegleFormPropertyType<TValue = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = [NonNullable<TValue>] extends [never] ? RegleRuleDecl<TValue, 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>;
|
|
494
|
-
/**
|
|
495
|
-
* @internal
|
|
496
|
-
* @reference {@link RegleFormPropertyType}
|
|
497
|
-
*/
|
|
498
|
-
type $InternalFormPropertyTypes = $InternalRegleRuleDecl | $InternalRegleCollectionRuleDecl | $InternalReglePartialValidationTree;
|
|
264
|
+
type RegleFormPropertyType<TValue = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = [NonNullable<TValue>] extends [never] ? RegleRuleDecl<TValue, 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 Ref<infer V> ? RegleFormPropertyType<V, TCustomRules> : NonNullable<TValue> extends Record<string, any> ? ReglePartialValidationTree<NonNullable<TValue>, TCustomRules> : RegleRuleDecl<NonNullable<TValue>, TCustomRules>;
|
|
499
265
|
/**
|
|
500
266
|
* @public
|
|
501
267
|
* Rule tree for a form property
|
|
@@ -503,11 +269,6 @@ type $InternalFormPropertyTypes = $InternalRegleRuleDecl | $InternalRegleCollect
|
|
|
503
269
|
type RegleRuleDecl<TValue extends any = any, TCustomRules extends Partial<AllRulesDeclarations> = Partial<AllRulesDeclarations>> = FieldRegleBehaviourOptions & {
|
|
504
270
|
[TKey in keyof TCustomRules]?: NonNullable<TCustomRules[TKey]> extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, TParams, boolean> : NonNullable<TCustomRules[TKey]> extends RegleRuleDefinition<any, any, any, any> ? FormRuleDeclaration<TValue, any> : FormRuleDeclaration<TValue, any> | FieldRegleBehaviourOptions[keyof FieldRegleBehaviourOptions];
|
|
505
271
|
};
|
|
506
|
-
/**
|
|
507
|
-
* @internal
|
|
508
|
-
* @reference {@link RegleRuleDecl}
|
|
509
|
-
*/
|
|
510
|
-
type $InternalRegleRuleDecl = Record<string, FormRuleDeclaration<any, any>>;
|
|
511
272
|
/**
|
|
512
273
|
* @public
|
|
513
274
|
*/
|
|
@@ -516,36 +277,24 @@ type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends Partial<AllRul
|
|
|
516
277
|
}) | {
|
|
517
278
|
$each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
|
|
518
279
|
};
|
|
519
|
-
/**
|
|
520
|
-
* @internal
|
|
521
|
-
* @reference {@link RegleCollectionRuleDecl}
|
|
522
|
-
*
|
|
523
|
-
*/
|
|
524
|
-
type $InternalRegleCollectionRuleDecl = $InternalRegleRuleDecl & {
|
|
525
|
-
$each?: $InternalFormPropertyTypes;
|
|
526
|
-
};
|
|
527
280
|
/**
|
|
528
281
|
* @public
|
|
529
282
|
*/
|
|
530
|
-
type InlineRuleDeclaration<TValue extends any, TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>> = (value: Maybe<TValue>, ...args: any[]) => TReturn;
|
|
283
|
+
type InlineRuleDeclaration<TValue extends any = any, TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition> = RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>> = (value: Maybe<TValue>, ...args: any[]) => TReturn;
|
|
531
284
|
/**
|
|
532
285
|
* @public
|
|
533
286
|
* Regroup inline and registered rules
|
|
534
287
|
* */
|
|
535
|
-
type FormRuleDeclaration<TValue extends any, TParams extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition> = RegleRuleMetadataDefinition
|
|
288
|
+
type FormRuleDeclaration<TValue extends any, TParams extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition> = RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TMetadata extends RegleRuleMetadataDefinition = TReturn extends Promise<infer M> ? M : TReturn, TAsync extends boolean = boolean> = InlineRuleDeclaration<TValue, TReturn> | RegleRuleDefinition<TValue, TParams, TAsync, TMetadata>;
|
|
536
289
|
|
|
537
290
|
type RegleErrorTree<TRules extends ReglePartialValidationTree<any, any>> = {
|
|
538
291
|
readonly [K in keyof TRules]: RegleValidationErrors<TRules[K]>;
|
|
539
292
|
};
|
|
540
|
-
type RegleValidationErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleCollectionRuleDefinition ? RegleCollectionErrors<TRule['$each']> : TRule extends
|
|
293
|
+
type RegleValidationErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleCollectionRuleDefinition ? RegleCollectionErrors<TRule['$each']> : TRule extends RegleRuleDecl<any, any> ? string[] : TRule extends ReglePartialValidationTree<any, any> ? RegleErrorTree<TRule> : string[];
|
|
541
294
|
type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = {
|
|
542
295
|
readonly $errors: string[];
|
|
543
296
|
readonly $each: RegleValidationErrors<TRule>[];
|
|
544
297
|
};
|
|
545
|
-
/**
|
|
546
|
-
* @internal
|
|
547
|
-
*/
|
|
548
|
-
type $InternalRegleErrors = RegleCollectionErrors<any> | string[] | RegleErrorTree<any>;
|
|
549
298
|
type RegleExternalErrorTree<TState extends Record<string, any> = Record<string, any>> = {
|
|
550
299
|
[K in keyof TState]?: RegleExternalValidationErrors<TState[K]>;
|
|
551
300
|
};
|
|
@@ -556,8 +305,6 @@ type RegleExternalCollectionErrors<TValue extends any = any> = {
|
|
|
556
305
|
$errors?: string[];
|
|
557
306
|
$each?: RegleExternalValidationErrors<TValue>[];
|
|
558
307
|
};
|
|
559
|
-
type $InternalExternalRegleErrors = RegleExternalCollectionErrors<any> | string[] | RegleExternalErrorTree<any>;
|
|
560
|
-
type DataType = string | number | Record<string, any> | File | Array<any> | Date | null | undefined;
|
|
561
308
|
|
|
562
309
|
/**
|
|
563
310
|
* @public
|
|
@@ -567,43 +314,20 @@ interface RegleStatus<TState extends Record<string, any> = Record<string, any>,
|
|
|
567
314
|
readonly [TKey in keyof TRules]: InferRegleStatusType<NonNullable<TRules[TKey]>, TState, TKey>;
|
|
568
315
|
};
|
|
569
316
|
}
|
|
570
|
-
/**
|
|
571
|
-
* @internal
|
|
572
|
-
* @reference {@link RegleStatus}
|
|
573
|
-
*/
|
|
574
|
-
interface $InternalRegleStatus extends RegleCommonStatus {
|
|
575
|
-
$fields: {
|
|
576
|
-
[x: string]: $InternalRegleStatusType;
|
|
577
|
-
};
|
|
578
|
-
}
|
|
579
317
|
/**
|
|
580
318
|
* @public
|
|
581
319
|
*/
|
|
582
|
-
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>;
|
|
583
|
-
/**
|
|
584
|
-
* @internal
|
|
585
|
-
* @reference {@link InferRegleStatusType}
|
|
586
|
-
*/
|
|
587
|
-
type $InternalRegleStatusType = $InternalRegleCollectionStatus | RegleCommonStatus | $InternalRegleStatus | $InternalRegleFieldStatus;
|
|
320
|
+
type InferRegleStatusType<TRule extends RegleCollectionRuleDecl | RegleRuleDecl | ReglePartialValidationTree<any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> = TRule extends RegleCollectionRuleDefinition<any, any> ? NonNullable<TState[TKey]> extends Array<Record<string, any> | any> ? RegleCollectionStatus<TRule['$each'], TState[TKey]> : RegleFieldStatus<TRule, TState, TKey> : TRule extends ReglePartialValidationTree<any> ? NonNullable<TState[TKey]> extends Array<any> ? RegleCommonStatus<TState[TKey]> : NonNullable<TState[TKey]> extends Record<PropertyKey, any> ? RegleStatus<TState[TKey], TRule> : RegleFieldStatus<TRule, TState, TKey> : RegleFieldStatus<TRule, TState, TKey>;
|
|
588
321
|
/**
|
|
589
322
|
* @public
|
|
590
323
|
*/
|
|
591
324
|
interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, Partial<AllRulesDeclarations>> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus<TState> {
|
|
592
|
-
$value: TState[TKey]
|
|
325
|
+
$value: UnwrapNestedRefs<TState[TKey]>;
|
|
593
326
|
readonly $externalErrors?: string[];
|
|
594
327
|
readonly $rules: {
|
|
595
328
|
readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
|
|
596
329
|
};
|
|
597
330
|
}
|
|
598
|
-
/**
|
|
599
|
-
* @internal
|
|
600
|
-
* @reference {@link RegleFieldStatus}
|
|
601
|
-
*/
|
|
602
|
-
interface $InternalRegleFieldStatus extends RegleCommonStatus {
|
|
603
|
-
$value: any;
|
|
604
|
-
$rules: Record<string, $InternalRegleRuleStatus>;
|
|
605
|
-
$externalErrors?: string[];
|
|
606
|
-
}
|
|
607
331
|
/**
|
|
608
332
|
* @public
|
|
609
333
|
*/
|
|
@@ -614,8 +338,8 @@ interface RegleCommonStatus<TValue = any> {
|
|
|
614
338
|
readonly $anyDirty: boolean;
|
|
615
339
|
readonly $pending: boolean;
|
|
616
340
|
readonly $error: boolean;
|
|
617
|
-
$id?:
|
|
618
|
-
$value: TValue
|
|
341
|
+
$id?: number;
|
|
342
|
+
$value: UnwrapNestedRefs<TValue>;
|
|
619
343
|
$touch(): void;
|
|
620
344
|
$reset(): void;
|
|
621
345
|
$validate(): Promise<boolean>;
|
|
@@ -635,47 +359,16 @@ type RegleRuleStatus<TValue = any, TParams extends any[] = any[]> = {
|
|
|
635
359
|
readonly $path: string;
|
|
636
360
|
$validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
|
|
637
361
|
$validate(): Promise<boolean>;
|
|
362
|
+
$reset(): void;
|
|
638
363
|
} & ([TParams] extends [[]] ? {} : {
|
|
639
364
|
readonly $params: TParams;
|
|
640
365
|
});
|
|
641
|
-
/**
|
|
642
|
-
* @internal
|
|
643
|
-
* @reference {@link RegleRuleStatus}
|
|
644
|
-
*/
|
|
645
|
-
interface $InternalRegleRuleStatus {
|
|
646
|
-
$type: string;
|
|
647
|
-
$message: string | string[];
|
|
648
|
-
$active: boolean;
|
|
649
|
-
$valid: boolean;
|
|
650
|
-
$pending: boolean;
|
|
651
|
-
$path: string;
|
|
652
|
-
$externalErrors?: string[];
|
|
653
|
-
$params?: any[];
|
|
654
|
-
$validator(value: any, ...args: any[]): RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>;
|
|
655
|
-
$validate(): Promise<boolean>;
|
|
656
|
-
$unwatch(): void;
|
|
657
|
-
$watch(): void;
|
|
658
|
-
}
|
|
659
366
|
/**
|
|
660
367
|
* @public
|
|
661
368
|
*/
|
|
662
369
|
interface RegleCollectionStatus<TRules extends RegleRuleDecl | ReglePartialValidationTree<any>, TState extends any[]> extends RegleFieldStatus<TRules, TState> {
|
|
663
370
|
readonly $each: Array<InferRegleStatusType<NonNullable<TRules>, TState, number>>;
|
|
664
371
|
}
|
|
665
|
-
/**
|
|
666
|
-
* @internal
|
|
667
|
-
* @reference {@link RegleCollectionStatus}
|
|
668
|
-
*/
|
|
669
|
-
interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fields'> {
|
|
670
|
-
$each: Array<$InternalRegleStatusType>;
|
|
671
|
-
$rules?: Record<string, $InternalRegleRuleStatus>;
|
|
672
|
-
/** Track each array state */
|
|
673
|
-
$unwatch(): void;
|
|
674
|
-
$watch(): void;
|
|
675
|
-
$fields?: {
|
|
676
|
-
[x: string]: $InternalRegleStatusType;
|
|
677
|
-
};
|
|
678
|
-
}
|
|
679
372
|
|
|
680
373
|
/**
|
|
681
374
|
* @description
|
|
@@ -697,7 +390,8 @@ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fi
|
|
|
697
390
|
* // Create a simple rule with no params
|
|
698
391
|
* import {ruleHelpers} from '@regle/validators';
|
|
699
392
|
*
|
|
700
|
-
* export const isFoo = createRule
|
|
393
|
+
* export const isFoo = createRule({
|
|
394
|
+
* type: defineType<string>('foo'),
|
|
701
395
|
* validator(value) {
|
|
702
396
|
* if (ruleHelpers.isFilled(value)) {
|
|
703
397
|
* return value === 'foo';
|
|
@@ -732,6 +426,6 @@ declare const useRegle: <TState extends Record<string, any>, TRules extends Regl
|
|
|
732
426
|
declare function defineRegleConfig<TCustomRules extends Partial<AllRulesDeclarations>>({ rules, options, }: {
|
|
733
427
|
rules?: () => TCustomRules;
|
|
734
428
|
options?: RegleBehaviourOptions;
|
|
735
|
-
}): <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, Partial<AllRulesDeclarations> & TCustomRules>, TValid = keyof TRules extends keyof TState ? true : false>(state:
|
|
429
|
+
}): <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, Partial<AllRulesDeclarations> & TCustomRules>, TValid = keyof TRules extends keyof TState ? true : false>(state: Ref<TState> | DeepReactiveState<TState>, rulesFactory: TValid extends true ? TRules | ComputedRef<TRules> | (() => TRules) : never, options?: (Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<TState>) | undefined) => Regle<TState, TRules>;
|
|
736
430
|
|
|
737
|
-
export { type
|
|
431
|
+
export { type DeepMaybeRef, type FormRuleDeclaration, type InlineRuleDeclaration, InternalRuleType, type LocalRegleBehaviourOptions, type Maybe, type ParamDecl, type Regle, type RegleBehaviourOptions, type RegleCollectionErrors, type RegleCollectionRuleDecl, type RegleCollectionRuleDefinition, type RegleCollectionStatus, type RegleCommonStatus, type RegleComputedRules, type RegleErrorTree, type RegleExternalCollectionErrors, type RegleExternalErrorTree, type RegleExternalValidationErrors, type RegleFieldStatus, type RegleFormPropertyType, 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 RegleValidationTree, type UnwrapRegleUniversalParams, createRule, defineRegleConfig, defineType, unwrapRuleParameters, useRegle };
|