@regle/core 1.26.0-beta.1 → 1.26.0-beta.6
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/regle-core.d.ts +749 -749
- package/dist/regle-core.js +8 -17
- package/dist/regle-core.min.js +2 -2
- package/package.json +7 -7
package/dist/regle-core.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @regle/core v1.26.0-beta.
|
|
2
|
+
* @regle/core v1.26.0-beta.6
|
|
3
3
|
* (c) 2026 Victor Garcia
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -7,723 +7,382 @@
|
|
|
7
7
|
import { ComputedRef, MaybeRef, MaybeRefOrGetter, Plugin, Raw, Ref, UnwrapNestedRefs, UnwrapRef } from "vue";
|
|
8
8
|
import { EmptyObject, Get, IsAny, IsEmptyObject, IsLiteral, IsNever, IsNullable, IsOptional, IsStringLiteral, IsUnion, IsUnknown, Merge, Or, Paths, RequireAtLeastOne, RequireOneOrNone, RequiredDeep, UnionToIntersection, UnionToTuple } from "type-fest";
|
|
9
9
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
10
|
-
|
|
10
|
+
declare const RegleStaticSymbol: unique symbol;
|
|
11
|
+
type RegleStatic<T> = T extends (new (...args: infer Args) => infer U) ? RegleStaticImpl<new (...args: Args) => RegleStaticImpl<U>> : RegleStaticImpl<T>;
|
|
12
|
+
type RegleStaticImpl<T> = Raw<T & {
|
|
13
|
+
[RegleStaticSymbol]: true;
|
|
14
|
+
}>;
|
|
15
|
+
type IsRegleStatic<T> = T extends RegleStaticImpl<T> ? true : false;
|
|
16
|
+
type UnwrapStatic<T> = IsAny<T> extends true ? any : IsUnknown<T> extends true ? any : NonNullable<T> extends RegleStaticImpl<infer U> ? Raw<U> : UnwrapStaticSimple<T>;
|
|
17
|
+
type UnwrapStaticSimple<T> = IsAny<T> extends true ? any : NonNullable<T> extends Array<infer U> ? Array<UnwrapStatic<U>> : isRecordLiteral<NonNullable<T>> extends true ? { [K in keyof T]: UnwrapStatic<T[K]> } : T;
|
|
18
|
+
/**
|
|
19
|
+
* Combine all members of a union type, merging types for each key, and keeping loose types
|
|
20
|
+
*/
|
|
21
|
+
type JoinDiscriminatedUnions<TUnion extends unknown> = IsAny<TUnion> extends true ? any : IsUnknown<TUnion> extends true ? any : HasNamedKeys<TUnion> extends true ? isRecordLiteral<TUnion> extends true ? NonNullable<TUnion> extends infer TNonNull ? NormalizeUnion<TNonNull> extends infer TNormalized extends Record<string, any> ? HasCommonKey<UnionToTuple<TNonNull>, keyof TNormalized> extends true ? ResolveKeys<TUnion> extends infer TResolved extends Record<string, any> ? DumbJoinDiscriminatedUnions<TUnion> extends infer TDumbJoin ? Omit<TDumbJoin, keyof TResolved> extends infer TLoose ? Prettify<TResolved & (TLoose extends EmptyObject ? {} : TLoose)> : never : never : never : DumbJoinDiscriminatedUnions<TUnion> : never : never : TUnion : TUnion;
|
|
22
|
+
type ResolveKeys<TUnion extends unknown> = NonNullable<TUnion> extends infer TNonNull ? NormalizeUnion<TNonNull> extends infer TNormalized extends Record<string, any> ? Partial<UnionToIntersection<RemoveCommonKey<UnionToTuple<TNonNull>, keyof TNormalized>[number]>> & Pick<TNormalized, keyof TNormalized> : never : never;
|
|
23
|
+
/**
|
|
24
|
+
* Combine all members of a union type on one level and not nested.
|
|
25
|
+
*/
|
|
26
|
+
type LazyJoinDiscriminatedUnions<TUnion extends unknown> = IsAny<TUnion> extends true ? any : IsUnknown<TUnion> extends true ? any : isRecordLiteral<TUnion> extends true ? NonNullable<TUnion> extends infer TNonNull extends Record<string, any> ? Prettify<Partial<UnionToIntersection<RemoveCommonKey<UnionToTuple<TNonNull>, keyof TNonNull>[number]>> & Pick<TNonNull, keyof TNonNull>> : never : TUnion;
|
|
27
|
+
type DumbJoinDiscriminatedUnions<TUnion extends unknown> = IsAny<TUnion> extends true ? any : IsUnknown<TUnion> extends true ? any : isRecordLiteral<TUnion> extends true ? NonNullable<TUnion> extends infer TNonNull extends Record<string, any> ? Prettify<Partial<UnionToIntersection<TNonNull>> & Pick<TNonNull, keyof TNonNull>> : never : TUnion;
|
|
28
|
+
type RemoveCommonKey<T extends readonly any[], K extends PropertyKey> = T extends [infer F, ...infer R] ? [Prettify<Omit<F, K>>, ...RemoveCommonKey<R, K>] : [];
|
|
29
|
+
type HasCommonKey<T extends readonly any[], K extends PropertyKey> = T extends [infer F, ...infer R] ? K extends keyof F ? true : HasCommonKey<R, K> : false;
|
|
30
|
+
/**
|
|
31
|
+
* Transforms a union and apply undefined values to non-present keys to support intersection
|
|
32
|
+
*/
|
|
33
|
+
type NormalizeUnion<TUnion> = RetrieveUnionUnknownValues<NonNullable<UnionToTuple<TUnion>>, RetrieveUnionUnknownKeysOf<NonNullable<UnionToTuple<TUnion>>>[number]>[number];
|
|
34
|
+
/**
|
|
35
|
+
* Combine all union values to be able to get even the normally "never" values, act as an intersection type
|
|
36
|
+
*/
|
|
37
|
+
type RetrieveUnionUnknownValues<T extends readonly any[], TKeys extends string> = T extends [infer F extends Record<string, any>, ...infer R] ? [{ [K in TKeys as GetMaybeObjectValue<F, K> extends NonUndefined<GetMaybeObjectValue<F, K>> ? never : K]?: GetMaybeObjectValue<F, K> } & { [K in TKeys as GetMaybeObjectValue<F, K> extends NonUndefined<GetMaybeObjectValue<F, K>> ? K : never]: GetMaybeObjectValue<F, K> }, ...RetrieveUnionUnknownValues<R, TKeys>] : [];
|
|
38
|
+
/**
|
|
39
|
+
* Get all possible keys from a union, even the ones present only on one union
|
|
40
|
+
*/
|
|
41
|
+
type RetrieveUnionUnknownKeysOf<T extends readonly any[]> = T extends [infer F, ...infer R] ? [keyof F, ...RetrieveUnionUnknownKeysOf<R>] : [];
|
|
42
|
+
/**
|
|
43
|
+
* Get item value from object, otherwise fallback to undefined. Avoid TS to not be able to infer keys not present on all unions
|
|
44
|
+
*/
|
|
45
|
+
type GetMaybeObjectValue<O extends Record<string, any>, K extends string> = K extends keyof O ? O[K] : undefined;
|
|
46
|
+
type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K] };
|
|
47
|
+
/**
|
|
48
|
+
* Merge every boolean property into a single boolean.
|
|
49
|
+
*/
|
|
50
|
+
type MergePropsIntoRequiredBooleans<TObject extends Record<string, any>> = { [K in keyof TObject]-?: TObject[K] extends NonNullable<TObject[K]> ? true : false }[keyof TObject];
|
|
51
|
+
/**
|
|
52
|
+
* Ensure that if at least one prop is required, the "prop" object will be required too
|
|
53
|
+
*/
|
|
54
|
+
type HaveAnyRequiredProps<TObject extends Record<string, any>> = [TObject] extends [never] ? false : TObject extends Record<string, any> ? MergePropsIntoRequiredBooleans<TObject> extends false ? false : true : false;
|
|
55
|
+
type EnumType<T extends Record<string, unknown>> = T[keyof T];
|
|
56
|
+
type EnumLike = {
|
|
57
|
+
[k: string]: string | number;
|
|
58
|
+
[nu: number]: string;
|
|
59
|
+
};
|
|
60
|
+
type MaybeRefOrComputedRef<T extends any> = MaybeRef<T> | ComputedRef<T>;
|
|
61
|
+
type UnwrapMaybeRef<T extends MaybeRef<any>> = T extends Ref<any> ? UnwrapRef<T> : UnwrapNestedRefs<T>;
|
|
62
|
+
type TupleToPlainObj<T> = { [I in keyof T & `${number}`]: T[I] };
|
|
63
|
+
type HasNamedKeys<T> = IsAny<T> extends true ? false : IsUnknown<T> extends true ? false : IsUnion<T> extends true ? ProcessHasNamedKeys<LazyJoinDiscriminatedUnions<T>> : ProcessHasNamedKeys<T>;
|
|
64
|
+
type ProcessHasNamedKeys<T> = { [K in keyof NonNullable<T>]: K extends string ? (string extends K ? never : K) : never }[keyof NonNullable<T>] extends never ? false : true;
|
|
65
|
+
/**
|
|
66
|
+
* Convert a nested object to a deeply nested partial object.
|
|
67
|
+
*/
|
|
68
|
+
type DeepPartial<T> = IsAny<T> extends true ? any : IsUnknown<T> extends true ? any : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends Date | File | RegleStaticImpl<unknown> ? T : T extends Record<string, any> ? { [K in keyof T]?: DeepPartial<T[K]> | undefined } : T | undefined;
|
|
69
|
+
type Prettify<T> = T extends infer R ? { [K in keyof R]: R[K] } & {} : never;
|
|
70
|
+
type Maybe<T = any> = T | null | undefined;
|
|
71
|
+
type MaybeInput<T = any> = T | null | undefined;
|
|
72
|
+
type MaybeOutput<T = any> = T | undefined;
|
|
73
|
+
type MaybeReadonly<T> = T | Readonly<T>;
|
|
74
|
+
type NonUndefined<T> = Exclude<T, undefined>;
|
|
75
|
+
type isUndefinedOrNull<T> = Or<IsOptional<T>, IsNullable<T>>;
|
|
76
|
+
type MaybeOrPartial<T> = T extends Record<string, any> ? Partial<T> : MaybeOutput<T>;
|
|
77
|
+
type MaybeNullable<T> = Or<IsNullable<T>, IsOptional<T>>;
|
|
78
|
+
type PromiseReturn<T> = T extends Promise<infer U> ? U : T;
|
|
79
|
+
type MaybeGetter<T, V = any, TAdd extends Record<string, any> = {}, TSelf extends Record<string, any> = {}> = (T & TSelf) | ((value: Ref<V>, index: number) => T & TAdd & TSelf);
|
|
80
|
+
type MaybeComputedOrGetter<T> = MaybeRefOrComputedRef<T> | ((...args: any[]) => T);
|
|
81
|
+
type Unwrap<T extends MaybeRef<unknown>> = T extends Ref ? UnwrapRef<T> : UnwrapNestedRefs<T>;
|
|
82
|
+
type UnwrapSimple<T extends MaybeRef<Record<string, any>>> = T extends MaybeComputedOrGetter<infer U> ? U : T extends Ref<infer U> ? U : T extends ((...args: any[]) => infer U) ? U : T;
|
|
83
|
+
type ExtractFromGetter<T extends MaybeGetter<any, any, any>> = T extends ((value: Ref<any>, index: number) => infer U extends Record<string, any>) ? U : T;
|
|
84
|
+
type ExtendOnlyRealRecord<T extends unknown> = NonNullable<T> extends File | Date | RegleStatic<{}> ? false : NonNullable<T> extends Record<string, any> ? true : false;
|
|
85
|
+
type OmitByType<T extends Record<string, any>, U> = { [K in keyof T as T[K] extends U ? never : K]: T[K] };
|
|
86
|
+
type DeepMaybeRef<T extends Record<string, any>> = { [K in keyof T]: MaybeRefOrGetter<T[K]> };
|
|
87
|
+
type ExcludeByType<T, U> = { [K in keyof T as T[K] extends U ? never : K]: T[K] extends U ? never : T[K] };
|
|
88
|
+
type PrimitiveTypes = string | number | boolean | bigint | Date | File;
|
|
89
|
+
type WidenPrimitiveLiterals<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends bigint ? bigint : T;
|
|
90
|
+
type isRecordLiteral<T extends unknown> = NonNullable<T> extends Date | File | RegleStatic<unknown> ? false : NonNullable<T> extends Record<string, any> ? true : false;
|
|
91
|
+
type NoInferLegacy<A extends any> = [A][A extends any ? 0 : never];
|
|
92
|
+
/**
|
|
93
|
+
* Extract the element type from an array.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* type Element = ArrayElement<[1, 2, 3]>; // number
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
type ArrayElement<T> = T extends Array<infer U> ? U : never;
|
|
101
|
+
/**
|
|
102
|
+
* Declares a tuple that must have at least one element
|
|
103
|
+
*/
|
|
104
|
+
type NonEmptyTuple<T> = [T, ...T[]] | T[];
|
|
105
|
+
interface CommonComparisonOptions {
|
|
11
106
|
/**
|
|
12
|
-
*
|
|
107
|
+
* Change the behaviour of the rule to check only if the value is equal in addition to be strictly superior or inferior
|
|
108
|
+
* @default true
|
|
13
109
|
*/
|
|
14
|
-
|
|
110
|
+
allowEqual?: boolean;
|
|
15
111
|
}
|
|
16
|
-
|
|
17
|
-
type RegleImmediateDirtyMode = 'eager' | 'non-empty' | 'lazy-non-empty';
|
|
18
|
-
interface RegleBehaviourOptions {
|
|
19
|
-
/**
|
|
20
|
-
* Does not run rules until the field is dirty.
|
|
21
|
-
* @default false
|
|
22
|
-
*/
|
|
23
|
-
lazy?: boolean | undefined;
|
|
112
|
+
interface CommonAlphaOptions {
|
|
24
113
|
/**
|
|
25
|
-
*
|
|
114
|
+
* Allow symbols in alphabetical-like rules (like "_")
|
|
26
115
|
* @default true
|
|
27
|
-
*
|
|
28
|
-
*/
|
|
29
|
-
autoDirty?: boolean | undefined;
|
|
30
|
-
/**
|
|
31
|
-
* Only update error status when calling `$validate` or `$validateSync`.
|
|
32
|
-
* Will not display errors as you type
|
|
33
|
-
* @default false
|
|
34
|
-
*
|
|
35
|
-
* @default true if rewardEarly is true
|
|
36
|
-
*
|
|
37
116
|
*/
|
|
38
|
-
|
|
117
|
+
allowSymbols?: boolean;
|
|
118
|
+
}
|
|
119
|
+
interface UrlOptions {
|
|
39
120
|
/**
|
|
40
|
-
*
|
|
41
|
-
* @default false
|
|
121
|
+
* Optional regex for validating the URL protocol
|
|
42
122
|
*/
|
|
43
|
-
|
|
123
|
+
protocol?: RegExp;
|
|
124
|
+
}
|
|
125
|
+
type EnumOneOfLike<TValue extends unknown> = {
|
|
126
|
+
readonly [k: string]: TValue;
|
|
127
|
+
};
|
|
128
|
+
type DefaultValidators = {
|
|
129
|
+
alpha: RegleRuleWithParamsDefinition<unknown, string, [options?: CommonAlphaOptions | undefined]>;
|
|
130
|
+
alphaNum: RegleRuleWithParamsDefinition<unknown, string | number, [options?: CommonAlphaOptions | undefined]>;
|
|
131
|
+
atLeastOne: RegleRuleWithParamsDefinition<unknown, Record<string, unknown> | object, [keys?: readonly string[] | undefined], false, boolean, false, Record<string, unknown> | object>;
|
|
132
|
+
between: RegleRuleWithParamsDefinition<unknown, number, [min: number, max: number, options?: CommonComparisonOptions]>;
|
|
133
|
+
boolean: RegleRuleDefinition<unknown, unknown, [], false, boolean, any, unknown>;
|
|
134
|
+
checked: RegleRuleDefinition<unknown, boolean, [], false, boolean, boolean>;
|
|
135
|
+
contains: RegleRuleWithParamsDefinition<unknown, string, [part: MaybeInput<string>], false, boolean>;
|
|
136
|
+
containsSpecialCharacter: RegleRuleWithParamsDefinition<unknown, string, [minCharactersCount?: number | undefined], false, {
|
|
137
|
+
$valid: boolean;
|
|
138
|
+
minCharactersCount: number;
|
|
139
|
+
}>;
|
|
140
|
+
containsUppercase: RegleRuleWithParamsDefinition<unknown, string, [minUppercaseCount?: number | undefined], false, {
|
|
141
|
+
$valid: boolean;
|
|
142
|
+
minUppercaseCount: number;
|
|
143
|
+
}>;
|
|
144
|
+
date: RegleRuleDefinition<unknown, unknown, [], false, boolean, MaybeInput<Date>, unknown>;
|
|
145
|
+
dateAfter: RegleRuleWithParamsDefinition<unknown, string | Date, [after: MaybeInput<string | Date>, options?: CommonComparisonOptions], false, true | {
|
|
146
|
+
$valid: false;
|
|
147
|
+
error: 'date-not-after';
|
|
148
|
+
} | {
|
|
149
|
+
$valid: false;
|
|
150
|
+
error: 'value-or-parameter-not-a-date';
|
|
151
|
+
}>;
|
|
152
|
+
dateBefore: RegleRuleWithParamsDefinition<unknown, string | Date, [before: MaybeInput<string | Date>, options?: CommonComparisonOptions], false, true | {
|
|
153
|
+
$valid: false;
|
|
154
|
+
error: 'date-not-before';
|
|
155
|
+
} | {
|
|
156
|
+
$valid: false;
|
|
157
|
+
error: 'value-or-parameter-not-a-date';
|
|
158
|
+
}>;
|
|
159
|
+
dateBetween: RegleRuleWithParamsDefinition<unknown, string | Date, [before: MaybeInput<string | Date>, after: MaybeInput<string | Date>, options?: CommonComparisonOptions], false, boolean>;
|
|
160
|
+
decimal: RegleRuleDefinition<unknown, string | number, [], false, boolean, string | number>;
|
|
161
|
+
domain: RegleRuleDefinition<unknown, string, [], false, boolean, string>;
|
|
162
|
+
email: RegleRuleDefinition<unknown, string, [], false, boolean, string>;
|
|
163
|
+
endsWith: RegleRuleWithParamsDefinition<unknown, string, [part: MaybeInput<string>], false, boolean>;
|
|
164
|
+
exactLength: RegleRuleWithParamsDefinition<unknown, MeasurableValue, [count: number], false, boolean>;
|
|
165
|
+
exactValue: RegleRuleWithParamsDefinition<unknown, number, [count: number], false, boolean>;
|
|
166
|
+
file: RegleRuleDefinition<unknown, unknown, [], false, boolean, MaybeInput<File>, unknown>;
|
|
167
|
+
fileType: RegleRuleWithParamsDefinition<unknown, File, [accept: readonly string[]], false, boolean>;
|
|
168
|
+
hexadecimal: RegleRuleDefinition<unknown, string, [], false, boolean, string>;
|
|
169
|
+
integer: RegleRuleDefinition<unknown, string | number, [], false, boolean, string | number>;
|
|
170
|
+
ipv4Address: RegleRuleDefinition<unknown, string, [], false, boolean, string>;
|
|
171
|
+
literal: RegleRuleDefinition<unknown, string | number, [literal: string | number], false, boolean, MaybeInput<string | number>, string | number, true>;
|
|
172
|
+
macAddress: RegleRuleWithParamsDefinition<unknown, string, [separator?: string | undefined], false, boolean>;
|
|
173
|
+
maxLength: RegleRuleWithParamsDefinition<unknown, MeasurableValue, [max: number, options?: CommonComparisonOptions], false, boolean>;
|
|
174
|
+
maxFileSize: RegleRuleWithParamsDefinition<unknown, File, [maxSize: number], false, true | {
|
|
175
|
+
$valid: boolean;
|
|
176
|
+
fileSize: number;
|
|
177
|
+
}, MaybeInput<File>, File>;
|
|
178
|
+
maxValue: RegleRuleWithParamsDefinition<unknown, number | string, [max: number | string, options?: CommonComparisonOptions], false, boolean>;
|
|
179
|
+
minFileSize: RegleRuleWithParamsDefinition<unknown, File, [minSize: number], false, true | {
|
|
180
|
+
$valid: boolean;
|
|
181
|
+
fileSize: number;
|
|
182
|
+
}, MaybeInput<File>, File>;
|
|
183
|
+
minLength: RegleRuleWithParamsDefinition<unknown, MeasurableValue, [min: number, options?: CommonComparisonOptions], false, boolean>;
|
|
184
|
+
minValue: RegleRuleWithParamsDefinition<unknown, number | string, [min: number | string, options?: CommonComparisonOptions], false, boolean>;
|
|
185
|
+
nativeEnum: RegleRuleDefinition<unknown, string | number, [enumLike: EnumLike], false, boolean, string | number>;
|
|
186
|
+
number: RegleRuleDefinition<unknown, unknown, [], false, boolean, any, unknown>;
|
|
187
|
+
numeric: RegleRuleDefinition<unknown, string | number, [], false, boolean, string | number>;
|
|
188
|
+
oneOf: RegleRuleDefinition<unknown, string | number, [options: readonly [string | number, ...(string | number)[]] | EnumOneOfLike<unknown>], false, boolean, MaybeInput<string | number>, string | number>;
|
|
189
|
+
regex: RegleRuleWithParamsDefinition<unknown, string | number, [regexp: RegExp | readonly RegExp[]], false, boolean>;
|
|
190
|
+
required: RegleRuleDefinition<unknown, unknown, [], false, boolean, unknown, unknown, true>;
|
|
191
|
+
sameAs: RegleRuleWithParamsDefinition<unknown, unknown, [target: any, otherName?: string], false, boolean>;
|
|
192
|
+
string: RegleRuleDefinition<unknown, unknown, [], false, boolean, any, unknown>;
|
|
193
|
+
type: RegleRuleDefinition<unknown, unknown, [], false, boolean, unknown, unknown>;
|
|
194
|
+
startsWith: RegleRuleWithParamsDefinition<unknown, string, [part: MaybeInput<string>], false, boolean>;
|
|
195
|
+
url: RegleRuleWithParamsDefinition<unknown, string, [options?: UrlOptions | undefined], false, boolean, unknown, string>;
|
|
196
|
+
};
|
|
197
|
+
interface inferRulesFn<TCustomRules extends Partial<ExtendedRulesDeclarationsOverrides>> {
|
|
198
|
+
<TState extends MaybeRef<Record<string, any> | MaybeInput<PrimitiveTypes>>, TRules extends ReglePartialRuleTree<Unwrap<TState extends Record<string, any> ? TState : {}>, Partial<DefaultValidatorsTree> & TCustomRules>, TDecl extends RegleRuleDecl<NonNullable<TState>, Partial<DefaultValidatorsTree> & TCustomRules>>(state: Maybe<TState> | DeepReactiveState<TState>, rulesFactory: Unwrap<TState> extends MaybeInput<PrimitiveTypes> ? TDecl : Unwrap<TState> extends Record<string, any> ? TRules : {}): NonNullable<Unwrap<TState>> extends PrimitiveTypes ? TDecl : TRules;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Type helper to provide autocomplete and type-checking for your form rules.
|
|
202
|
+
* It returns the rules without any processing - useful with computed rules.
|
|
203
|
+
*
|
|
204
|
+
* @param state - The state reference
|
|
205
|
+
* @param rules - Your rule tree
|
|
206
|
+
* @returns The rules object (passthrough)
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* import { inferRules, useRegle } from '@regle/core';
|
|
211
|
+
* import { required, minLength } from '@regle/rules';
|
|
212
|
+
*
|
|
213
|
+
* const state = ref({ name: '' });
|
|
214
|
+
*
|
|
215
|
+
* // inferRules preserves TypeScript autocompletion
|
|
216
|
+
* const rules = computed(() => {
|
|
217
|
+
* return inferRules(state, {
|
|
218
|
+
* name: { required, minLength: minLength(2) }
|
|
219
|
+
* })
|
|
220
|
+
* });
|
|
221
|
+
*
|
|
222
|
+
* const { r$ } = useRegle(state, rules);
|
|
223
|
+
* ```
|
|
224
|
+
*
|
|
225
|
+
* @see {@link https://reglejs.dev/core-concepts/#dynamic-rules-object Documentation}
|
|
226
|
+
*/
|
|
227
|
+
declare const inferRules: inferRulesFn<Partial<ExtendedRulesDeclarationsOverrides>>;
|
|
228
|
+
interface GlobalConfigOptions<TCustomRules extends Partial<ExtendedRulesDeclarationsOverrides> = EmptyObject, TShortcuts extends RegleShortcutDefinition<any> = never> {
|
|
44
229
|
/**
|
|
45
|
-
*
|
|
230
|
+
* Declare custom rules to be used globally or override the default rules messages.
|
|
46
231
|
*
|
|
47
|
-
*
|
|
232
|
+
* Ex:
|
|
233
|
+
* ```ts
|
|
234
|
+
* import { defineRegleConfig } from '@regle/core';
|
|
235
|
+
* import { required, withMessage } from '@regle/rules';
|
|
48
236
|
*
|
|
49
|
-
*
|
|
237
|
+
* export const { useRegle, inferRules, useRules } = defineRegleConfig({
|
|
238
|
+
* rules: () => ({
|
|
239
|
+
* required: withMessage(required, 'This field cannot be empty'),
|
|
240
|
+
* }),
|
|
241
|
+
* });
|
|
242
|
+
* ```
|
|
243
|
+
* @see {@link https://reglejs.dev/advanced-usage/global-config Documentation}
|
|
50
244
|
*/
|
|
51
|
-
|
|
245
|
+
rules?: () => TCustomRules;
|
|
52
246
|
/**
|
|
53
|
-
* Define
|
|
247
|
+
* Define modifiers to be used globally.
|
|
54
248
|
*
|
|
55
|
-
*
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Set the dirty state when the form is initialized.
|
|
60
|
-
*
|
|
61
|
-
* - `true` or `'eager'`: mark every field dirty.
|
|
62
|
-
* - `'non-empty'`: mark every field dirty when any initial value is non-empty.
|
|
63
|
-
* - `'lazy-non-empty'`: mark only fields with non-empty initial values dirty.
|
|
64
|
-
*
|
|
65
|
-
* @default false
|
|
66
|
-
*/
|
|
67
|
-
immediateDirty?: boolean | RegleImmediateDirtyMode | undefined;
|
|
68
|
-
/**
|
|
69
|
-
* Disable all the computation
|
|
70
|
-
* @default false
|
|
71
|
-
*/
|
|
72
|
-
disabled?: boolean | undefined;
|
|
73
|
-
}
|
|
74
|
-
interface LocalRegleBehaviourOptions<TState extends Record<string, any>, TRules extends ReglePartialRuleTree<TState>, TValidationGroups extends Record<string, RegleValidationGroupEntry[]> = {}> {
|
|
75
|
-
/**
|
|
76
|
-
* A dictionary of external errors to be injected into the field statuses.
|
|
77
|
-
*
|
|
78
|
-
* Useful for integrating errors from a backend or other validation sources.
|
|
79
|
-
* External errors can be assigned using a reactive object or a Ref, and will be merged into the `$externalErrors` and `$errors` properties for each field.
|
|
80
|
-
* You can also call `r$.$setExternalErrors(...)` directly without providing this option.
|
|
81
|
-
* Pass this option when the same external errors ref needs to be owned and synchronized outside of Regle.
|
|
249
|
+
* Ex:
|
|
250
|
+
* ```ts
|
|
251
|
+
* import { defineRegleConfig } from '@regle/core';
|
|
82
252
|
*
|
|
83
|
-
*
|
|
253
|
+
* export const { useRegle, inferRules, useRules } = defineRegleConfig({
|
|
254
|
+
* modifiers: {
|
|
255
|
+
* lazy: true,
|
|
256
|
+
* rewardEarly: true
|
|
257
|
+
* }
|
|
258
|
+
* });
|
|
259
|
+
* ```
|
|
260
|
+
* @see {@link https://reglejs.dev/advanced-usage/global-config#declare-modifiers Documentation}
|
|
84
261
|
*/
|
|
85
|
-
|
|
262
|
+
modifiers?: RegleBehaviourOptions;
|
|
86
263
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* The `validationGroups` option lets you define logical groupings of fields within your form that should be validated or checked together.
|
|
90
|
-
* This can be used, for example, to easily determine if a subset of your form (e.g. an "address" group or a set of "contact information" fields) are all valid or share a collective error state.
|
|
264
|
+
* Define reusable validation shortcuts to be used globally.
|
|
91
265
|
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
266
|
+
* Ex:
|
|
267
|
+
* ```ts
|
|
268
|
+
* import { defineRegleConfig } from '@regle/core';
|
|
94
269
|
*
|
|
95
|
-
*
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
type FieldOnlyRegleBehaviourOptions = {
|
|
105
|
-
/**
|
|
106
|
-
* Set external errors for the field.
|
|
107
|
-
*/
|
|
108
|
-
externalErrors?: Ref<string[]>;
|
|
109
|
-
/**
|
|
110
|
-
* A unique identifier for the Regle instance in the devtools.
|
|
111
|
-
* @default undefined
|
|
270
|
+
* export const { useRegle, inferRules, useRules } = defineRegleConfig({
|
|
271
|
+
* shortcuts: {
|
|
272
|
+
* fields: {
|
|
273
|
+
* $isRequired: (field) => field.$rules.required?.$active ?? false,
|
|
274
|
+
* },
|
|
275
|
+
* },
|
|
276
|
+
* });
|
|
277
|
+
* ```
|
|
278
|
+
* @see {@link https://reglejs.dev/advanced-usage/extend-properties#extend-properties Documentation}
|
|
112
279
|
*/
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
interface RegleValidationGroupOutput {
|
|
117
|
-
/** Indicates whether any field in the validation group is invalid. */
|
|
118
|
-
$invalid: boolean;
|
|
119
|
-
/** Convenience flag to easily decide if an error message should be displayed. True when any field in the group is dirty, not pending, and invalid. */
|
|
120
|
-
$error: boolean;
|
|
121
|
-
/** Indicates if any async rule in the validation group is currently running. */
|
|
122
|
-
$pending: boolean;
|
|
123
|
-
/** Indicates whether any field in the validation group has been interacted with by the user. */
|
|
124
|
-
$dirty: boolean;
|
|
125
|
-
/** Indicates whether all fields in the validation group pass validation and are dirty. */
|
|
126
|
-
$correct: boolean;
|
|
127
|
-
/** Collection of all error messages from fields in the group where $dirty equals true. */
|
|
128
|
-
$errors: string[];
|
|
129
|
-
/** Collection of all error messages from fields in the group, regardless of $dirty state. */
|
|
130
|
-
$silentErrors: string[];
|
|
280
|
+
shortcuts?: TShortcuts;
|
|
281
|
+
/** Override default behaviors of Regle processors. */
|
|
282
|
+
overrides?: GlobalConfigOverrides<unknown>;
|
|
131
283
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
284
|
+
/**
|
|
285
|
+
* Define a global Regle configuration to customize the validation behavior across your application.
|
|
286
|
+
*
|
|
287
|
+
* Features:
|
|
288
|
+
* - Customize built-in rules messages
|
|
289
|
+
* - Add your custom rules with full type inference
|
|
290
|
+
* - Define global modifiers (lazy, rewardEarly, etc.)
|
|
291
|
+
* - Define shortcuts for common validation patterns
|
|
292
|
+
*
|
|
293
|
+
* @param options - Configuration options
|
|
294
|
+
* @returns Object containing typed `useRegle`, `inferRules`, and `useRules` functions
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```ts
|
|
298
|
+
* import { defineRegleConfig } from '@regle/core';
|
|
299
|
+
* import { required, withMessage } from '@regle/rules';
|
|
300
|
+
*
|
|
301
|
+
* export const { useRegle, inferRules, useRules } = defineRegleConfig({
|
|
302
|
+
* rules: () => ({
|
|
303
|
+
* // Override default required message
|
|
304
|
+
* required: withMessage(required, 'This field cannot be empty'),
|
|
305
|
+
* // Add custom rule
|
|
306
|
+
* myCustomRule: createRule({
|
|
307
|
+
* validator: (value) => value === 'valid',
|
|
308
|
+
* message: 'Invalid value'
|
|
309
|
+
* })
|
|
310
|
+
* }),
|
|
311
|
+
* modifiers: {
|
|
312
|
+
* lazy: true,
|
|
313
|
+
* rewardEarly: true
|
|
314
|
+
* }
|
|
315
|
+
* });
|
|
316
|
+
* ```
|
|
317
|
+
*
|
|
318
|
+
* @see {@link https://reglejs.dev/advanced-usage/global-config Documentation}
|
|
319
|
+
*/
|
|
320
|
+
declare function defineRegleConfig<TShortcuts extends RegleShortcutDefinition<TCustomRules>, TCustomRules extends Partial<ExtendedRulesDeclarationsOverrides>>({
|
|
321
|
+
rules,
|
|
322
|
+
modifiers,
|
|
323
|
+
shortcuts,
|
|
324
|
+
overrides
|
|
325
|
+
}: GlobalConfigOptions<TCustomRules, TShortcuts>): {
|
|
326
|
+
useRegle: useRegleFn<Omit<TCustomRules, keyof DefaultValidators>, TShortcuts>;
|
|
327
|
+
inferRules: inferRulesFn<Omit<TCustomRules, keyof DefaultValidators>>;
|
|
328
|
+
useRules: useRulesFn<TCustomRules, TShortcuts>;
|
|
172
329
|
};
|
|
173
|
-
type AddDollarToOptions<T extends Record<string, any>> = { [K in keyof T as `$${string & K}`]: T[K] };
|
|
174
330
|
/**
|
|
175
|
-
*
|
|
331
|
+
* Extend an already created custom `useRegle` configuration with additional rules, modifiers, or shortcuts.
|
|
176
332
|
*
|
|
177
|
-
* @
|
|
178
|
-
* @
|
|
179
|
-
* @
|
|
180
|
-
* @
|
|
181
|
-
* @
|
|
333
|
+
* @param regle - The existing useRegle function to extend
|
|
334
|
+
* @param options - Additional configuration to merge
|
|
335
|
+
* @param options.rules - Additional custom rules
|
|
336
|
+
* @param options.modifiers - Additional modifiers to merge
|
|
337
|
+
* @param options.shortcuts - Additional shortcuts to merge
|
|
338
|
+
* @returns Object containing the extended `useRegle` and `inferRules` functions
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```ts
|
|
342
|
+
* import { extendRegleConfig } from '@regle/core';
|
|
343
|
+
* import { baseUseRegle } from './base-config';
|
|
344
|
+
*
|
|
345
|
+
* export const { useRegle, inferRules } = extendRegleConfig(baseUseRegle, {
|
|
346
|
+
* rules: () => ({
|
|
347
|
+
* additionalRule: myNewRule
|
|
348
|
+
* }),
|
|
349
|
+
* modifiers: {
|
|
350
|
+
* rewardEarly: true
|
|
351
|
+
* }
|
|
352
|
+
* });
|
|
353
|
+
* ```
|
|
182
354
|
*
|
|
355
|
+
* @see {@link https://reglejs.dev/advanced-usage/global-config Documentation}
|
|
183
356
|
*/
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
357
|
+
declare function extendRegleConfig<TRootCustomRules extends Partial<ExtendedRulesDeclarationsOverrides>, TRootShortcuts extends RegleShortcutDefinition<{}>, TShortcuts extends RegleShortcutDefinition<Merge<TRootCustomRules, TCustomRules>>, TCustomRules extends Partial<ExtendedRulesDeclarationsOverrides>>(regle: useRegleFn<TRootCustomRules, TRootShortcuts>, {
|
|
358
|
+
rules,
|
|
359
|
+
modifiers,
|
|
360
|
+
shortcuts,
|
|
361
|
+
overrides
|
|
362
|
+
}: GlobalConfigOptions<TCustomRules, TShortcuts>): {
|
|
363
|
+
useRegle: useRegleFn<Merge<TRootCustomRules, TCustomRules>, TRootShortcuts & TShortcuts>;
|
|
364
|
+
inferRules: inferRulesFn<Merge<TRootCustomRules, TCustomRules>>;
|
|
365
|
+
};
|
|
192
366
|
/**
|
|
193
|
-
*
|
|
367
|
+
* Define a global Regle options to customize the validation behavior across your application.
|
|
368
|
+
* It's meant to be used with the Regle Vue plugin.
|
|
194
369
|
*
|
|
195
|
-
* @
|
|
196
|
-
* @
|
|
197
|
-
*
|
|
198
|
-
* @
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Reset validation status and reset form state to its initial state.
|
|
213
|
-
*
|
|
214
|
-
* Initial state is different than the original state as the initial state can be mutated when using `$reset`.
|
|
215
|
-
*
|
|
216
|
-
* This serve as the base comparison state for `$edited` property.
|
|
217
|
-
*
|
|
218
|
-
* ⚠️ This doesn't work if the state is a `reactive` object.
|
|
219
|
-
*/
|
|
220
|
-
toInitialState?: boolean;
|
|
221
|
-
/**
|
|
222
|
-
* Reset validation status and reset form state to its original state.
|
|
223
|
-
*
|
|
224
|
-
* Original state is the unmutated state that was passed to the form when it was initialized.
|
|
225
|
-
*/
|
|
226
|
-
toOriginalState?: boolean;
|
|
227
|
-
/**
|
|
228
|
-
* Reset validation status and reset form state to the given state
|
|
229
|
-
* Also set the new state as the initial state.
|
|
230
|
-
*/
|
|
231
|
-
toState?: TState | (() => TState);
|
|
232
|
-
/**
|
|
233
|
-
* Clears the $externalErrors state back to an empty object.
|
|
234
|
-
*
|
|
235
|
-
* @default true
|
|
236
|
-
*/
|
|
237
|
-
clearExternalErrors?: boolean;
|
|
238
|
-
/**
|
|
239
|
-
* Keep the validation state of the form ($dirty, $invalid, $pending etc..)
|
|
240
|
-
* Only useful if you only want to reset the form state.
|
|
241
|
-
*
|
|
242
|
-
* @default false
|
|
243
|
-
*/
|
|
244
|
-
keepValidationState?: boolean;
|
|
245
|
-
}, 'toInitialState' | 'toState'>;
|
|
246
|
-
type ScopedInstancesRecord = Record<string, Record<string, SuperCompatibleRegleRoot>> & {
|
|
247
|
-
'~~global': Record<string, SuperCompatibleRegleRoot>;
|
|
248
|
-
};
|
|
249
|
-
type ScopedInstancesRecordLike = Partial<ScopedInstancesRecord>;
|
|
250
|
-
type PartialFormState<TState extends Record<string, any>> = [unknown] extends [TState] ? {} : Prettify<{ [K in keyof TState as ExtendOnlyRealRecord<TState[K]> extends true ? never : TState[K] extends Array<any> ? never : K]?: MaybeOutput<TState[K]> } & { [K in keyof TState as ExtendOnlyRealRecord<TState[K]> extends true ? K : TState[K] extends Array<any> ? K : never]: NonNullable<TState[K]> extends Array<infer U extends Record<string, any>> ? PartialFormState<U>[] : PartialFormState<TState[K]> }>;
|
|
251
|
-
type RegleResult<Data extends Record<string, any> | any[] | unknown, TRules extends ReglePartialRuleTree<any> | RegleFormPropertyType<any>> = {
|
|
252
|
-
/** The result of the validation */valid: false; /** Output data, filter the result by checking the `valid` property */
|
|
253
|
-
data: IsUnknown<Data> extends true ? unknown : IsAny<Data> extends true ? unknown : IsEmptyObject<TRules> extends true ? isRecordLiteral<Data> extends true ? PartialFormState<Data extends Record<string, any> ? Data : {}> : MaybeOutput<Data> : HasNamedKeys<Data> extends true ? HasNamedKeys<TRules> extends true ? NonNullable<Data> extends Date | File ? MaybeOutput<Raw<Data>> : NonNullable<Data> extends Array<infer U extends Record<string, any>> ? PartialFormState<U>[] : NonNullable<Data> extends Record<string, any> ? PartialFormState<NonNullable<Data>> : MaybeOutput<Data> : isRecordLiteral<Data> extends true ? PartialFormState<Data extends Record<string, any> ? Data : {}> : MaybeOutput<Data> : unknown;
|
|
254
|
-
/**
|
|
255
|
-
* Collection of all the error messages, collected for all children properties and nested forms.
|
|
256
|
-
*
|
|
257
|
-
* Only contains errors from properties where $dirty equals true.
|
|
258
|
-
* */
|
|
259
|
-
issues: DataTypeRegleIssues<Data, TRules>;
|
|
260
|
-
/**
|
|
261
|
-
* Collection of all the issues, collected for all children properties and nested forms.
|
|
262
|
-
*
|
|
263
|
-
* Only contains issues from properties where $dirty equals true.
|
|
264
|
-
*/
|
|
265
|
-
errors: DataTypeRegleErrors<Data>;
|
|
266
|
-
} | {
|
|
267
|
-
valid: true;
|
|
268
|
-
data: IsUnknown<Data> extends true ? unknown : IsAny<Data> extends true ? unknown : IsEmptyObject<TRules> extends true ? isRecordLiteral<Data> extends true ? PartialFormState<Data extends Record<string, any> ? Data : {}> : MaybeOutput<Data> : HasNamedKeys<Data> extends true ? HasNamedKeys<TRules> extends true ? NonNullable<Data> extends Array<infer U extends Record<string, any>> ? DeepSafeFormState<U, TRules extends ReglePartialRuleTree<any> ? TRules : {}>[] : NonNullable<Data> extends Date | File ? SafeFieldProperty<Raw<NonNullable<Data>>, TRules extends ReglePartialRuleTree<any> ? TRules : {}> : NonNullable<Data> extends Record<string, any> ? DeepSafeFormState<NonNullable<Data>, TRules extends ReglePartialRuleTree<any> ? TRules : {}> : SafeFieldProperty<Data, TRules extends ReglePartialRuleTree<any> ? TRules : {}> : isRecordLiteral<Data> extends true ? PartialFormState<Data extends Record<string, any> ? Data : {}> : MaybeOutput<Data> : unknown;
|
|
269
|
-
issues: EmptyObject;
|
|
270
|
-
errors: EmptyObject;
|
|
271
|
-
};
|
|
272
|
-
type DataTypeRegleIssues<TData extends Record<string, any> | any[] | unknown, TRules extends ReglePartialRuleTree<any> | RegleFormPropertyType<any> = never> = HasNamedKeys<TData> extends true ? NonNullable<TData> extends Array<infer U> ? isRecordLiteral<U> extends true ? RegleFieldIssue<TRules>[] : RegleCollectionErrors<TData, true> : isRecordLiteral<TData> extends true ? RegleIssuesTree<TData> : RegleFieldIssue<TRules>[] : RegleFieldIssue | RegleCollectionErrors<TData, true> | RegleIssuesTree<TData>;
|
|
273
|
-
type DataTypeRegleErrors<TData extends Record<string, any> | any[] | unknown> = HasNamedKeys<TData> extends true ? NonNullable<TData> extends Array<infer U> ? isRecordLiteral<U> extends true ? string[] : RegleCollectionErrors<TData> : isRecordLiteral<TData> extends true ? RegleErrorTree<TData> : string[] : string[] | RegleErrorTree<TData> | RegleCollectionErrors<TData>;
|
|
274
|
-
type RegleCollectionResult<Data extends any[], TRules extends ReglePartialRuleTree<any> | RegleFormPropertyType<any>> = RegleResult<Data, TRules> & ({
|
|
275
|
-
valid: false;
|
|
276
|
-
issues: RegleCollectionErrors<Data, true>;
|
|
277
|
-
errors: RegleCollectionErrors<Data>;
|
|
278
|
-
} | {
|
|
279
|
-
valid: true;
|
|
280
|
-
issues: EmptyObject;
|
|
281
|
-
errors: EmptyObject;
|
|
282
|
-
});
|
|
283
|
-
type RegleFieldResult<Data extends any, TRules extends ReglePartialRuleTree<any> | RegleFormPropertyType<any>> = RegleResult<Data, TRules> & ({
|
|
284
|
-
valid: false;
|
|
285
|
-
issues: RegleFieldIssue<TRules>[];
|
|
286
|
-
errors: string[];
|
|
287
|
-
} | {
|
|
288
|
-
valid: true;
|
|
289
|
-
issues: [];
|
|
290
|
-
errors: [];
|
|
291
|
-
});
|
|
292
|
-
type $InternalRegleResult = {
|
|
293
|
-
valid: boolean;
|
|
294
|
-
data: any;
|
|
295
|
-
errors: $InternalRegleErrorTree | $InternalRegleCollectionErrors | string[];
|
|
296
|
-
issues: $InternalRegleIssuesTree | $InternalRegleCollectionIssues | RegleFieldIssue[];
|
|
297
|
-
};
|
|
298
|
-
type DeepSafeFormState<TState extends Record<string, any>, TRules extends ReglePartialRuleTree<Record<string, any>, CustomRulesDeclarationTree> | undefined> = IsUnknown<TState> extends true ? {} : IsAny<TState> extends true ? unknown : TRules extends undefined ? TState : HasNamedKeys<TState> extends true ? TRules extends ReglePartialRuleTree<TState, CustomRulesDeclarationTree> ? Prettify<{ [K in keyof TState as IsPropertyOutputRequired<TState[K], TRules[K]> extends false ? K : never]?: SafeProperty<TState[K], TRules[K]> extends MaybeInput<infer M> ? MaybeOutput<M> : SafeProperty<TState[K], TRules[K]> } & { [K in keyof TState as IsPropertyOutputRequired<TState[K], TRules[K]> extends false ? never : K]-?: IsUnknown<SafeProperty<TState[K], TRules[K]>> extends true ? unknown : IsAny<TState[K]> extends true ? unknown : NonNullable<SafeProperty<TState[K], TRules[K]>> }> : TState : TState;
|
|
299
|
-
type FieldHaveRequiredRule<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends MaybeRefOrComputedRef<RegleRuleDecl<any, any>> ? UnwrapRef<TRule> extends infer TRuleResolved extends Record<string, any> ? { [K in keyof TRuleResolved]-?: TRuleResolved[K] extends RegleRuleDefinition<unknown, any, any[], any, any, any, any, true> ? true : false }[keyof TRuleResolved] extends false ? false : true : false : false;
|
|
300
|
-
type ObjectHaveAtLeastOneRequiredField<TState extends Record<string, any>, TRules extends ReglePartialRuleTree<TState, any>> = TState extends Maybe<TState> ? ({ [K in keyof NonNullable<TState>]-?: IsPropertyOutputRequired<NonNullable<TState>[K], TRules[K] extends RegleFormPropertyType<any, any> ? TRules[K] : {}> } & {
|
|
301
|
-
$self: IsPropertyOutputRequired<string, TRules['$self']>;
|
|
302
|
-
})[keyof TState | '$self'] extends false ? false : true : true;
|
|
303
|
-
type ArrayHaveAtLeastOneRequiredField<TState extends Maybe<any[]>, TRule extends RegleCollectionRuleDecl<any>> = TState extends Maybe<TState> ? FieldHaveRequiredRule<Omit<TRule, '$each'> extends MaybeRef<RegleRuleDecl> ? Omit<UnwrapRef<TRule>, '$each'> : {}> | ObjectHaveAtLeastOneRequiredField<ArrayElement<NonNullable<TState>>, ExtractFromGetter<TRule['$each']> extends undefined ? {} : NonNullable<ExtractFromGetter<TRule['$each']>>> extends false ? false : true : true;
|
|
304
|
-
type SafeProperty<TState, TRule extends RegleFormPropertyType<any, any> | undefined> = unknown extends TState ? unknown : [TRule] extends [RegleCollectionRuleDecl<any, any>] ? TState extends Array<infer U extends Record<string, any>> ? DeepSafeFormState<U, ExtractFromGetter<TRule['$each']>>[] : TState : [TRule] extends [ReglePartialRuleTree<any, any>] ? ExtendOnlyRealRecord<TState> extends true ? DeepSafeFormState<NonNullable<TState> extends Record<string, any> ? JoinDiscriminatedUnions<NonNullable<TState>> : {}, TRule> : [TRule] extends [MaybeRef<RegleRuleDecl<any, any>>] ? FieldHaveRequiredRule<UnwrapRef<TRule>> extends true ? TState : MaybeOutput<TState> : TState : TState;
|
|
305
|
-
type IsPropertyOutputRequired<TState, TRule extends RegleFormPropertyType<any, any> | undefined> = [unknown] extends [TState] ? unknown : NonNullable<TState> extends Array<any> ? TRule extends RegleCollectionRuleDecl<any, any> ? ArrayHaveAtLeastOneRequiredField<NonNullable<TState>, TRule> extends false ? false : true : false : TRule extends ReglePartialRuleTree<any, any> ? ExtendOnlyRealRecord<TState> extends true ? ObjectHaveAtLeastOneRequiredField<NonNullable<TState> extends Record<string, any> ? NonNullable<TState> : {}, TRule extends ReglePartialRuleTree<NonNullable<TState> extends Record<string, any> ? NonNullable<TState> : {}, any> ? TRule : {}> extends false ? false : true : TRule extends MaybeRef<RegleRuleDecl<any, any>> ? FieldHaveRequiredRule<UnwrapRef<TRule>> extends false ? false : true : false : false;
|
|
306
|
-
type SafeFieldProperty<TState, TRule extends RegleFormPropertyType<any, any> | undefined = never> = FieldHaveRequiredRule<TRule> extends true ? NonNullable<TState> : MaybeOutput<TState>;
|
|
307
|
-
/** Types to be augmented by @regle/schemas */
|
|
308
|
-
interface NarrowVariantExtracts {}
|
|
309
|
-
interface NarrowVariantFieldExtracts<T extends unknown> {}
|
|
310
|
-
type NarrowVariant<TRoot extends {
|
|
311
|
-
[x: string]: unknown;
|
|
312
|
-
$fields: {
|
|
313
|
-
[x: string]: unknown;
|
|
314
|
-
};
|
|
315
|
-
$value: unknown;
|
|
316
|
-
}, TKey extends keyof TRoot, TValue extends (LazyJoinDiscriminatedUnions<Exclude<TRoot[TKey], RegleCollectionStatus<any, any, any> | RegleStatus<any, any, any> | NarrowVariantExtracts[keyof NarrowVariantExtracts]>> extends {
|
|
317
|
-
$value: infer V;
|
|
318
|
-
} ? V : unknown)> = Extract<TRoot, { [K in TKey]: RegleFieldStatus<TValue, any, any> | RegleFieldStatus<MaybeInput<TValue>, any, any> | (IsEmptyObject<NarrowVariantFieldExtracts<TValue>> extends true ? EmptyObject : NarrowVariantFieldExtracts<TValue>[keyof NarrowVariantFieldExtracts<TValue>]) }> & {
|
|
319
|
-
$fields: Extract<TRoot['$fields'], { [K in TKey]: RegleFieldStatus<TValue, any, any> | RegleFieldStatus<MaybeInput<TValue>, any, any> | (IsEmptyObject<NarrowVariantFieldExtracts<TValue>> extends true ? EmptyObject : NarrowVariantFieldExtracts<TValue>[keyof NarrowVariantFieldExtracts<TValue>]) }> & { [K in TKey]: TRoot[K] & {
|
|
320
|
-
$value: TValue;
|
|
321
|
-
} };
|
|
322
|
-
} & {
|
|
323
|
-
$value: Omit<TRoot['$value'], TKey> & { [K in TKey]: TValue };
|
|
324
|
-
} & { [K in TKey]: TRoot[K] & {
|
|
325
|
-
$value: TValue;
|
|
326
|
-
} };
|
|
327
|
-
type MaybeVariantStatus<TState extends Record<string, any> | undefined = Record<string, any>, TRules extends ReglePartialRuleTree<NonNullable<TState>> = Record<string, any>, TShortcuts extends RegleShortcutDefinition = {}, TIsUnionOverride extends boolean = false,
|
|
328
|
-
/**
|
|
329
|
-
* Workaround for $each variants, TS generic can't detect if the Rules are an union when type is too nested, to the tuple is passed from parent
|
|
330
|
-
*/
|
|
331
|
-
TRulesTuple extends readonly any[] = never> = IsUnion<NonNullable<TState>> extends true ? Or<TIsUnionOverride, IsUnion<TRules>> extends true ? (IsNever<TRulesTuple> extends true ? TRules : TRulesTuple[number]) extends infer RulesUnionTuple extends ReglePartialRuleTree<NonNullable<TState>> ? ProcessChildrenFields<NonNullable<TState>, RulesUnionTuple, TShortcuts> extends infer TChildren ? Omit<RegleStatus<NonNullable<TState>, RulesUnionTuple, TShortcuts>, '$fields'> & {
|
|
332
|
-
$fields: TChildren[keyof TChildren];
|
|
333
|
-
} & (HasNamedKeys<NonNullable<TState>> extends true ? TChildren[keyof TChildren] : {}) : never : RegleStatus<JoinDiscriminatedUnions<NonNullable<TState>>, TRules extends ReglePartialRuleTree<NonNullable<JoinDiscriminatedUnions<NonNullable<TState>>>> ? TRules : EmptyObject, TShortcuts> : RegleStatus<JoinDiscriminatedUnions<NonNullable<TState>>, TRules extends ReglePartialRuleTree<NonNullable<JoinDiscriminatedUnions<NonNullable<TState>>>> ? TRules : EmptyObject, TShortcuts> : RegleStatus<TState, TRules, TShortcuts>;
|
|
334
|
-
/** Helper type to extract state tuple item at index */
|
|
335
|
-
type StateTupleItem<TStateTuple extends readonly any[], TIndexInt extends number> = TStateTuple[TIndexInt] extends Record<string, any> ? TStateTuple[TIndexInt] : never;
|
|
336
|
-
/** Helper type to extract NonNullable state tuple item */
|
|
337
|
-
type NonNullableStateTupleItem<TStateTuple extends readonly any[], TIndexInt extends number> = NonNullable<StateTupleItem<TStateTuple, TIndexInt>>;
|
|
338
|
-
/** Helper type to find the corresponding variant rule */
|
|
339
|
-
type VariantRuleForKey<TStateTuple extends readonly any[], TRulesTuple extends readonly any[], TIndexInt extends number, TKey> = FindCorrespondingVariant<StateTupleItem<TStateTuple, TIndexInt>, TRulesTuple> extends [infer U] ? TKey extends keyof U ? U[TKey] : EmptyObject : EmptyObject;
|
|
340
|
-
type ProcessChildrenFields<TState extends Record<string, any> | undefined, TRules extends ReglePartialRuleTree<NonNullable<TState>>, TShortcuts extends RegleShortcutDefinition = {}> = UnionToTuple<TState> extends infer TStateTuple extends readonly any[] ? UnionToTuple<TRules> extends infer TRulesTuple extends readonly any[] ? { [TIndex in keyof TupleToPlainObj<TStateTuple>]: TIndex extends `${infer TIndexInt extends number}` ? { [TKey in keyof TStateTuple[TIndexInt] as IsEmptyObject<VariantRuleForKey<TStateTuple, TRulesTuple, TIndexInt, TKey>> extends true ? TKey extends keyof TState ? TState[TKey] extends NonNullable<TState[TKey]> ? TKey : never : never : TKey]-?: InferRegleStatusType<VariantRuleForKey<TStateTuple, TRulesTuple, TIndexInt, TKey>, NonNullableStateTupleItem<TStateTuple, TIndexInt>, TKey, TShortcuts> } & { [TKey in keyof TStateTuple[TIndexInt] as IsEmptyObject<VariantRuleForKey<TStateTuple, TRulesTuple, TIndexInt, TKey>> extends true ? TKey extends keyof TState ? TState[TKey] extends NonNullable<TState[TKey]> ? never : TKey : TKey : never]?: InferRegleStatusType<VariantRuleForKey<TStateTuple, TRulesTuple, TIndexInt, TKey>, NonNullableStateTupleItem<TStateTuple, TIndexInt>, TKey, TShortcuts> } : {} } : {} : {};
|
|
341
|
-
type FindCorrespondingVariant<TState extends Record<string, any>, TRules extends readonly any[]> = TRules extends [infer F, ...infer R] ? F extends ReglePartialRuleTree<TState> ? [F] : FindCorrespondingVariant<TState, R> : [];
|
|
342
|
-
type PossibleLiteralTypes<T extends Record<string, any>, TKey extends keyof T> = unknown extends T[TKey] ? {
|
|
343
|
-
[x: string]: { [K in TKey]-?: Omit<RegleRuleDecl<any, Partial<ExtendedRulesDeclarations>>, 'literal'> & {
|
|
344
|
-
literal?: RegleRuleDefinition<'literal', any, [literal: any], false, boolean, any, string | number, true>;
|
|
345
|
-
} };
|
|
346
|
-
} : { [TVal in NonNullable<T[TKey]>]: { [K in TKey]-?: Omit<RegleRuleDecl<TVal, Partial<ExtendedRulesDeclarations>>, 'literal'> & {
|
|
347
|
-
literal?: RegleRuleDefinition<'literal', MaybeInput<TVal>, [literal: TVal], false, boolean, MaybeInput<TVal>, string | number, true>;
|
|
348
|
-
} } };
|
|
349
|
-
type RequiredForm<T extends Record<string, any>, TKey extends keyof T> = Omit<ReglePartialRuleTree<T>, TKey> & PossibleLiteralTypes<T, TKey>[keyof PossibleLiteralTypes<T, TKey>];
|
|
350
|
-
type VariantTuple<T extends Record<string, any>, TKey extends keyof T> = [RequiredForm<T, TKey>, ...RequiredForm<T, TKey>[]];
|
|
351
|
-
declare const RegleStaticSymbol: unique symbol;
|
|
352
|
-
type RegleStatic<T> = T extends (new (...args: infer Args) => infer U) ? RegleStaticImpl<new (...args: Args) => RegleStaticImpl<U>> : RegleStaticImpl<T>;
|
|
353
|
-
type RegleStaticImpl<T> = Raw<T & {
|
|
354
|
-
[RegleStaticSymbol]: true;
|
|
355
|
-
}>;
|
|
356
|
-
type IsRegleStatic<T> = T extends RegleStaticImpl<T> ? true : false;
|
|
357
|
-
type UnwrapStatic<T> = IsAny<T> extends true ? any : IsUnknown<T> extends true ? any : NonNullable<T> extends RegleStaticImpl<infer U> ? Raw<U> : UnwrapStaticSimple<T>;
|
|
358
|
-
type UnwrapStaticSimple<T> = IsAny<T> extends true ? any : NonNullable<T> extends Array<infer U> ? Array<UnwrapStatic<U>> : isRecordLiteral<NonNullable<T>> extends true ? { [K in keyof T]: UnwrapStatic<T[K]> } : T;
|
|
359
|
-
/**
|
|
360
|
-
* Combine all members of a union type, merging types for each key, and keeping loose types
|
|
361
|
-
*/
|
|
362
|
-
type JoinDiscriminatedUnions<TUnion extends unknown> = IsAny<TUnion> extends true ? any : IsUnknown<TUnion> extends true ? any : HasNamedKeys<TUnion> extends true ? isRecordLiteral<TUnion> extends true ? NonNullable<TUnion> extends infer TNonNull ? NormalizeUnion<TNonNull> extends infer TNormalized extends Record<string, any> ? HasCommonKey<UnionToTuple<TNonNull>, keyof TNormalized> extends true ? ResolveKeys<TUnion> extends infer TResolved extends Record<string, any> ? DumbJoinDiscriminatedUnions<TUnion> extends infer TDumbJoin ? Omit<TDumbJoin, keyof TResolved> extends infer TLoose ? Prettify<TResolved & (TLoose extends EmptyObject ? {} : TLoose)> : never : never : never : DumbJoinDiscriminatedUnions<TUnion> : never : never : TUnion : TUnion;
|
|
363
|
-
type ResolveKeys<TUnion extends unknown> = NonNullable<TUnion> extends infer TNonNull ? NormalizeUnion<TNonNull> extends infer TNormalized extends Record<string, any> ? Partial<UnionToIntersection<RemoveCommonKey<UnionToTuple<TNonNull>, keyof TNormalized>[number]>> & Pick<TNormalized, keyof TNormalized> : never : never;
|
|
364
|
-
/**
|
|
365
|
-
* Combine all members of a union type on one level and not nested.
|
|
366
|
-
*/
|
|
367
|
-
type LazyJoinDiscriminatedUnions<TUnion extends unknown> = IsAny<TUnion> extends true ? any : IsUnknown<TUnion> extends true ? any : isRecordLiteral<TUnion> extends true ? NonNullable<TUnion> extends infer TNonNull extends Record<string, any> ? Prettify<Partial<UnionToIntersection<RemoveCommonKey<UnionToTuple<TNonNull>, keyof TNonNull>[number]>> & Pick<TNonNull, keyof TNonNull>> : never : TUnion;
|
|
368
|
-
type DumbJoinDiscriminatedUnions<TUnion extends unknown> = IsAny<TUnion> extends true ? any : IsUnknown<TUnion> extends true ? any : isRecordLiteral<TUnion> extends true ? NonNullable<TUnion> extends infer TNonNull extends Record<string, any> ? Prettify<Partial<UnionToIntersection<TNonNull>> & Pick<TNonNull, keyof TNonNull>> : never : TUnion;
|
|
369
|
-
type RemoveCommonKey<T extends readonly any[], K extends PropertyKey> = T extends [infer F, ...infer R] ? [Prettify<Omit<F, K>>, ...RemoveCommonKey<R, K>] : [];
|
|
370
|
-
type HasCommonKey<T extends readonly any[], K extends PropertyKey> = T extends [infer F, ...infer R] ? K extends keyof F ? true : HasCommonKey<R, K> : false;
|
|
371
|
-
/**
|
|
372
|
-
* Transforms a union and apply undefined values to non-present keys to support intersection
|
|
373
|
-
*/
|
|
374
|
-
type NormalizeUnion<TUnion> = RetrieveUnionUnknownValues<NonNullable<UnionToTuple<TUnion>>, RetrieveUnionUnknownKeysOf<NonNullable<UnionToTuple<TUnion>>>[number]>[number];
|
|
375
|
-
/**
|
|
376
|
-
* Combine all union values to be able to get even the normally "never" values, act as an intersection type
|
|
377
|
-
*/
|
|
378
|
-
type RetrieveUnionUnknownValues<T extends readonly any[], TKeys extends string> = T extends [infer F extends Record<string, any>, ...infer R] ? [{ [K in TKeys as GetMaybeObjectValue<F, K> extends NonUndefined<GetMaybeObjectValue<F, K>> ? never : K]?: GetMaybeObjectValue<F, K> } & { [K in TKeys as GetMaybeObjectValue<F, K> extends NonUndefined<GetMaybeObjectValue<F, K>> ? K : never]: GetMaybeObjectValue<F, K> }, ...RetrieveUnionUnknownValues<R, TKeys>] : [];
|
|
379
|
-
/**
|
|
380
|
-
* Get all possible keys from a union, even the ones present only on one union
|
|
381
|
-
*/
|
|
382
|
-
type RetrieveUnionUnknownKeysOf<T extends readonly any[]> = T extends [infer F, ...infer R] ? [keyof F, ...RetrieveUnionUnknownKeysOf<R>] : [];
|
|
383
|
-
/**
|
|
384
|
-
* Get item value from object, otherwise fallback to undefined. Avoid TS to not be able to infer keys not present on all unions
|
|
385
|
-
*/
|
|
386
|
-
type GetMaybeObjectValue<O extends Record<string, any>, K extends string> = K extends keyof O ? O[K] : undefined;
|
|
387
|
-
type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K] };
|
|
388
|
-
/**
|
|
389
|
-
* Merge every boolean property into a single boolean.
|
|
390
|
-
*/
|
|
391
|
-
type MergePropsIntoRequiredBooleans<TObject extends Record<string, any>> = { [K in keyof TObject]-?: TObject[K] extends NonNullable<TObject[K]> ? true : false }[keyof TObject];
|
|
392
|
-
/**
|
|
393
|
-
* Ensure that if at least one prop is required, the "prop" object will be required too
|
|
394
|
-
*/
|
|
395
|
-
type HaveAnyRequiredProps<TObject extends Record<string, any>> = [TObject] extends [never] ? false : TObject extends Record<string, any> ? MergePropsIntoRequiredBooleans<TObject> extends false ? false : true : false;
|
|
396
|
-
type EnumType<T extends Record<string, unknown>> = T[keyof T];
|
|
397
|
-
type EnumLike = {
|
|
398
|
-
[k: string]: string | number;
|
|
399
|
-
[nu: number]: string;
|
|
400
|
-
};
|
|
401
|
-
type MaybeRefOrComputedRef<T extends any> = MaybeRef<T> | ComputedRef<T>;
|
|
402
|
-
type UnwrapMaybeRef<T extends MaybeRef<any> | DeepReactiveState<any>> = T extends Ref<any> ? UnwrapRef<T> : UnwrapNestedRefs<T>;
|
|
403
|
-
type TupleToPlainObj<T> = { [I in keyof T & `${number}`]: T[I] };
|
|
404
|
-
type HasNamedKeys<T> = IsAny<T> extends true ? false : IsUnknown<T> extends true ? false : IsUnion<T> extends true ? ProcessHasNamedKeys<LazyJoinDiscriminatedUnions<T>> : ProcessHasNamedKeys<T>;
|
|
405
|
-
type ProcessHasNamedKeys<T> = { [K in keyof NonNullable<T>]: K extends string ? (string extends K ? never : K) : never }[keyof NonNullable<T>] extends never ? false : true;
|
|
406
|
-
/**
|
|
407
|
-
* Convert a nested object to a deeply nested partial object.
|
|
408
|
-
*/
|
|
409
|
-
type DeepPartial<T> = IsAny<T> extends true ? any : IsUnknown<T> extends true ? any : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends Date | File | RegleStaticImpl<unknown> ? T : T extends Record<string, any> ? { [K in keyof T]?: DeepPartial<T[K]> | undefined } : T | undefined;
|
|
410
|
-
type Prettify<T> = T extends infer R ? { [K in keyof R]: R[K] } & {} : never;
|
|
411
|
-
type Maybe<T = any> = T | null | undefined;
|
|
412
|
-
type MaybeInput<T = any> = T | null | undefined;
|
|
413
|
-
type MaybeOutput<T = any> = T | undefined;
|
|
414
|
-
type MaybeReadonly<T> = T | Readonly<T>;
|
|
415
|
-
type NonUndefined<T> = Exclude<T, undefined>;
|
|
416
|
-
type isUndefinedOrNull<T> = Or<IsOptional<T>, IsNullable<T>>;
|
|
417
|
-
type MaybeOrPartial<T> = T extends Record<string, any> ? Partial<T> : MaybeOutput<T>;
|
|
418
|
-
type MaybeNullable<T> = Or<IsNullable<T>, IsOptional<T>>;
|
|
419
|
-
type PromiseReturn<T> = T extends Promise<infer U> ? U : T;
|
|
420
|
-
type MaybeGetter<T, V = any, TAdd extends Record<string, any> = {}, TSelf extends Record<string, any> = {}> = (T & TSelf) | ((value: Ref<V>, index: number) => T & TAdd & TSelf);
|
|
421
|
-
type MaybeComputedOrGetter<T> = MaybeRefOrComputedRef<T> | ((...args: any[]) => T);
|
|
422
|
-
type Unwrap<T extends MaybeRef<unknown>> = T extends Ref ? UnwrapRef<T> : UnwrapNestedRefs<T>;
|
|
423
|
-
type UnwrapSimple<T extends MaybeRef<Record<string, any>>> = T extends MaybeComputedOrGetter<infer U> ? U : T extends Ref<infer U> ? U : T extends ((...args: any[]) => infer U) ? U : T;
|
|
424
|
-
type ExtractFromGetter<T extends MaybeGetter<any, any, any>> = T extends ((value: Ref<any>, index: number) => infer U extends Record<string, any>) ? U : T;
|
|
425
|
-
type ExtendOnlyRealRecord<T extends unknown> = NonNullable<T> extends File | Date | RegleStatic<{}> | RegleStaticImpl<{}> ? false : NonNullable<T> extends Record<string, any> ? true : false;
|
|
426
|
-
type OmitByType<T extends Record<string, any>, U> = { [K in keyof T as T[K] extends U ? never : K]: T[K] };
|
|
427
|
-
type DeepMaybeRef<T extends Record<string, any>> = { [K in keyof T]: MaybeRefOrGetter<T[K]> };
|
|
428
|
-
type ExcludeByType<T, U> = { [K in keyof T as T[K] extends U ? never : K]: T[K] extends U ? never : T[K] };
|
|
429
|
-
type PrimitiveTypes = string | number | boolean | bigint | Date | File;
|
|
430
|
-
type WidenPrimitiveLiterals<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends bigint ? bigint : T;
|
|
431
|
-
type isRecordLiteral<T extends unknown> = NonNullable<T> extends Date | File | RegleStatic<unknown> | RegleStaticImpl<unknown> ? false : NonNullable<T> extends Record<string, any> ? true : false;
|
|
432
|
-
type NoInferLegacy<A extends any> = [A][A extends any ? 0 : never];
|
|
433
|
-
/**
|
|
434
|
-
* Extract the element type from an array.
|
|
435
|
-
*
|
|
436
|
-
* @example
|
|
437
|
-
* ```ts
|
|
438
|
-
* type Element = ArrayElement<[1, 2, 3]>; // number
|
|
439
|
-
* ```
|
|
440
|
-
*/
|
|
441
|
-
type ArrayElement<T> = T extends Array<infer U> ? U : never;
|
|
442
|
-
/**
|
|
443
|
-
* Declares a tuple that must have at least one element
|
|
444
|
-
*/
|
|
445
|
-
type NonEmptyTuple<T> = [T, ...T[]] | T[];
|
|
446
|
-
interface CommonComparisonOptions {
|
|
447
|
-
/**
|
|
448
|
-
* Change the behaviour of the rule to check only if the value is equal in addition to be strictly superior or inferior
|
|
449
|
-
* @default true
|
|
450
|
-
*/
|
|
451
|
-
allowEqual?: boolean;
|
|
452
|
-
}
|
|
453
|
-
interface CommonAlphaOptions {
|
|
454
|
-
/**
|
|
455
|
-
* Allow symbols in alphabetical-like rules (like "_")
|
|
456
|
-
* @default true
|
|
457
|
-
*/
|
|
458
|
-
allowSymbols?: boolean;
|
|
459
|
-
}
|
|
460
|
-
interface UrlOptions {
|
|
461
|
-
/**
|
|
462
|
-
* Optional regex for validating the URL protocol
|
|
463
|
-
*/
|
|
464
|
-
protocol?: RegExp;
|
|
465
|
-
}
|
|
466
|
-
type EnumOneOfLike<TValue extends unknown> = {
|
|
467
|
-
readonly [k: string]: TValue;
|
|
468
|
-
};
|
|
469
|
-
type DefaultValidators = {
|
|
470
|
-
alpha: RegleRuleWithParamsDefinition<unknown, string, [options?: CommonAlphaOptions | undefined]>;
|
|
471
|
-
alphaNum: RegleRuleWithParamsDefinition<unknown, string | number, [options?: CommonAlphaOptions | undefined]>;
|
|
472
|
-
atLeastOne: RegleRuleWithParamsDefinition<unknown, Record<string, unknown> | object, [keys?: readonly string[] | undefined], false, boolean, false, Record<string, unknown> | object>;
|
|
473
|
-
between: RegleRuleWithParamsDefinition<unknown, number, [min: number, max: number, options?: CommonComparisonOptions]>;
|
|
474
|
-
boolean: RegleRuleDefinition<unknown, unknown, [], false, boolean, any, unknown>;
|
|
475
|
-
checked: RegleRuleDefinition<unknown, boolean, [], false, boolean, boolean>;
|
|
476
|
-
contains: RegleRuleWithParamsDefinition<unknown, string, [part: MaybeInput<string>], false, boolean>;
|
|
477
|
-
containsSpecialCharacter: RegleRuleWithParamsDefinition<unknown, string, [minCharactersCount?: number | undefined], false, {
|
|
478
|
-
$valid: boolean;
|
|
479
|
-
minCharactersCount: number;
|
|
480
|
-
}>;
|
|
481
|
-
containsUppercase: RegleRuleWithParamsDefinition<unknown, string, [minUppercaseCount?: number | undefined], false, {
|
|
482
|
-
$valid: boolean;
|
|
483
|
-
minUppercaseCount: number;
|
|
484
|
-
}>;
|
|
485
|
-
date: RegleRuleDefinition<unknown, unknown, [], false, boolean, MaybeInput<Date>, unknown>;
|
|
486
|
-
dateAfter: RegleRuleWithParamsDefinition<unknown, string | Date, [after: MaybeInput<string | Date>, options?: CommonComparisonOptions], false, true | {
|
|
487
|
-
$valid: false;
|
|
488
|
-
error: 'date-not-after';
|
|
489
|
-
} | {
|
|
490
|
-
$valid: false;
|
|
491
|
-
error: 'value-or-parameter-not-a-date';
|
|
492
|
-
}>;
|
|
493
|
-
dateBefore: RegleRuleWithParamsDefinition<unknown, string | Date, [before: MaybeInput<string | Date>, options?: CommonComparisonOptions], false, true | {
|
|
494
|
-
$valid: false;
|
|
495
|
-
error: 'date-not-before';
|
|
496
|
-
} | {
|
|
497
|
-
$valid: false;
|
|
498
|
-
error: 'value-or-parameter-not-a-date';
|
|
499
|
-
}>;
|
|
500
|
-
dateBetween: RegleRuleWithParamsDefinition<unknown, string | Date, [before: MaybeInput<string | Date>, after: MaybeInput<string | Date>, options?: CommonComparisonOptions], false, boolean>;
|
|
501
|
-
decimal: RegleRuleDefinition<unknown, string | number, [], false, boolean, string | number>;
|
|
502
|
-
domain: RegleRuleDefinition<unknown, string, [], false, boolean, string>;
|
|
503
|
-
email: RegleRuleDefinition<unknown, string, [], false, boolean, string>;
|
|
504
|
-
endsWith: RegleRuleWithParamsDefinition<unknown, string, [part: MaybeInput<string>], false, boolean>;
|
|
505
|
-
exactLength: RegleRuleWithParamsDefinition<unknown, MeasurableValue, [count: number], false, boolean>;
|
|
506
|
-
exactValue: RegleRuleWithParamsDefinition<unknown, number, [count: number], false, boolean>;
|
|
507
|
-
file: RegleRuleDefinition<unknown, unknown, [], false, boolean, MaybeInput<File>, unknown>;
|
|
508
|
-
fileType: RegleRuleWithParamsDefinition<unknown, File, [accept: readonly string[]], false, boolean>;
|
|
509
|
-
hexadecimal: RegleRuleDefinition<unknown, string, [], false, boolean, string>;
|
|
510
|
-
integer: RegleRuleDefinition<unknown, string | number, [], false, boolean, string | number>;
|
|
511
|
-
ipv4Address: RegleRuleDefinition<unknown, string, [], false, boolean, string>;
|
|
512
|
-
literal: RegleRuleDefinition<unknown, string | number, [literal: string | number], false, boolean, MaybeInput<string | number>, string | number, true>;
|
|
513
|
-
macAddress: RegleRuleWithParamsDefinition<unknown, string, [separator?: string | undefined], false, boolean>;
|
|
514
|
-
maxLength: RegleRuleWithParamsDefinition<unknown, MeasurableValue, [max: number, options?: CommonComparisonOptions], false, boolean>;
|
|
515
|
-
maxFileSize: RegleRuleWithParamsDefinition<unknown, File, [maxSize: number], false, true | {
|
|
516
|
-
$valid: boolean;
|
|
517
|
-
fileSize: number;
|
|
518
|
-
}, MaybeInput<File>, File>;
|
|
519
|
-
maxValue: RegleRuleWithParamsDefinition<unknown, number | string, [max: number | string, options?: CommonComparisonOptions], false, boolean>;
|
|
520
|
-
minFileSize: RegleRuleWithParamsDefinition<unknown, File, [minSize: number], false, true | {
|
|
521
|
-
$valid: boolean;
|
|
522
|
-
fileSize: number;
|
|
523
|
-
}, MaybeInput<File>, File>;
|
|
524
|
-
minLength: RegleRuleWithParamsDefinition<unknown, MeasurableValue, [min: number, options?: CommonComparisonOptions], false, boolean>;
|
|
525
|
-
minValue: RegleRuleWithParamsDefinition<unknown, number | string, [min: number | string, options?: CommonComparisonOptions], false, boolean>;
|
|
526
|
-
nativeEnum: RegleRuleDefinition<unknown, string | number, [enumLike: EnumLike], false, boolean, string | number>;
|
|
527
|
-
number: RegleRuleDefinition<unknown, unknown, [], false, boolean, any, unknown>;
|
|
528
|
-
numeric: RegleRuleDefinition<unknown, string | number, [], false, boolean, string | number>;
|
|
529
|
-
oneOf: RegleRuleDefinition<unknown, string | number, [options: readonly [string | number, ...(string | number)[]] | EnumOneOfLike<unknown>], false, boolean, MaybeInput<string | number>, string | number>;
|
|
530
|
-
regex: RegleRuleWithParamsDefinition<unknown, string | number, [regexp: RegExp | readonly RegExp[]], false, boolean>;
|
|
531
|
-
required: RegleRuleDefinition<unknown, unknown, [], false, boolean, unknown, unknown, true>;
|
|
532
|
-
sameAs: RegleRuleWithParamsDefinition<unknown, unknown, [target: any, otherName?: string], false, boolean>;
|
|
533
|
-
string: RegleRuleDefinition<unknown, unknown, [], false, boolean, any, unknown>;
|
|
534
|
-
type: RegleRuleDefinition<unknown, unknown, [], false, boolean, unknown, unknown>;
|
|
535
|
-
startsWith: RegleRuleWithParamsDefinition<unknown, string, [part: MaybeInput<string>], false, boolean>;
|
|
536
|
-
url: RegleRuleWithParamsDefinition<unknown, string, [options?: UrlOptions | undefined], false, boolean, unknown, string>;
|
|
537
|
-
};
|
|
538
|
-
interface inferRulesFn<TCustomRules extends Partial<ExtendedRulesDeclarationsOverrides>> {
|
|
539
|
-
<TState extends MaybeRef<Record<string, any> | MaybeInput<PrimitiveTypes>>, TRules extends ReglePartialRuleTree<Unwrap<TState extends Record<string, any> ? TState : {}>, Partial<DefaultValidatorsTree> & TCustomRules>, TDecl extends RegleRuleDecl<NonNullable<TState>, Partial<DefaultValidatorsTree> & TCustomRules>>(state: Maybe<TState> | DeepReactiveState<TState>, rulesFactory: Unwrap<TState> extends MaybeInput<PrimitiveTypes> ? TDecl : Unwrap<TState> extends Record<string, any> ? TRules : {}): NonNullable<Unwrap<TState>> extends PrimitiveTypes ? TDecl : TRules;
|
|
540
|
-
}
|
|
541
|
-
/**
|
|
542
|
-
* Type helper to provide autocomplete and type-checking for your form rules.
|
|
543
|
-
* It returns the rules without any processing - useful with computed rules.
|
|
544
|
-
*
|
|
545
|
-
* @param state - The state reference
|
|
546
|
-
* @param rules - Your rule tree
|
|
547
|
-
* @returns The rules object (passthrough)
|
|
548
|
-
*
|
|
549
|
-
* @example
|
|
550
|
-
* ```ts
|
|
551
|
-
* import { inferRules, useRegle } from '@regle/core';
|
|
552
|
-
* import { required, minLength } from '@regle/rules';
|
|
553
|
-
*
|
|
554
|
-
* const state = ref({ name: '' });
|
|
555
|
-
*
|
|
556
|
-
* // inferRules preserves TypeScript autocompletion
|
|
557
|
-
* const rules = computed(() => {
|
|
558
|
-
* return inferRules(state, {
|
|
559
|
-
* name: { required, minLength: minLength(2) }
|
|
560
|
-
* })
|
|
561
|
-
* });
|
|
562
|
-
*
|
|
563
|
-
* const { r$ } = useRegle(state, rules);
|
|
564
|
-
* ```
|
|
565
|
-
*
|
|
566
|
-
* @see {@link https://reglejs.dev/core-concepts/#dynamic-rules-object Documentation}
|
|
567
|
-
*/
|
|
568
|
-
declare const inferRules: inferRulesFn<Partial<ExtendedRulesDeclarationsOverrides>>;
|
|
569
|
-
interface GlobalConfigOptions<TCustomRules extends Partial<ExtendedRulesDeclarationsOverrides> = EmptyObject, TShortcuts extends RegleShortcutDefinition<any> = never> {
|
|
570
|
-
/**
|
|
571
|
-
* Declare custom rules to be used globally or override the default rules messages.
|
|
572
|
-
*
|
|
573
|
-
* Ex:
|
|
574
|
-
* ```ts
|
|
575
|
-
* import { defineRegleConfig } from '@regle/core';
|
|
576
|
-
* import { required, withMessage } from '@regle/rules';
|
|
577
|
-
*
|
|
578
|
-
* export const { useRegle, inferRules, useRules } = defineRegleConfig({
|
|
579
|
-
* rules: () => ({
|
|
580
|
-
* required: withMessage(required, 'This field cannot be empty'),
|
|
581
|
-
* }),
|
|
582
|
-
* });
|
|
583
|
-
* ```
|
|
584
|
-
* @see {@link https://reglejs.dev/advanced-usage/global-config Documentation}
|
|
585
|
-
*/
|
|
586
|
-
rules?: () => TCustomRules;
|
|
587
|
-
/**
|
|
588
|
-
* Define modifiers to be used globally.
|
|
589
|
-
*
|
|
590
|
-
* Ex:
|
|
591
|
-
* ```ts
|
|
592
|
-
* import { defineRegleConfig } from '@regle/core';
|
|
593
|
-
*
|
|
594
|
-
* export const { useRegle, inferRules, useRules } = defineRegleConfig({
|
|
595
|
-
* modifiers: {
|
|
596
|
-
* lazy: true,
|
|
597
|
-
* rewardEarly: true
|
|
598
|
-
* }
|
|
599
|
-
* });
|
|
600
|
-
* ```
|
|
601
|
-
* @see {@link https://reglejs.dev/advanced-usage/global-config#declare-modifiers Documentation}
|
|
602
|
-
*/
|
|
603
|
-
modifiers?: RegleBehaviourOptions;
|
|
604
|
-
/**
|
|
605
|
-
* Define reusable validation shortcuts to be used globally.
|
|
606
|
-
*
|
|
607
|
-
* Ex:
|
|
608
|
-
* ```ts
|
|
609
|
-
* import { defineRegleConfig } from '@regle/core';
|
|
610
|
-
*
|
|
611
|
-
* export const { useRegle, inferRules, useRules } = defineRegleConfig({
|
|
612
|
-
* shortcuts: {
|
|
613
|
-
* fields: {
|
|
614
|
-
* $isRequired: (field) => field.$rules.required?.$active ?? false,
|
|
615
|
-
* },
|
|
616
|
-
* },
|
|
617
|
-
* });
|
|
618
|
-
* ```
|
|
619
|
-
* @see {@link https://reglejs.dev/advanced-usage/extend-properties#extend-properties Documentation}
|
|
620
|
-
*/
|
|
621
|
-
shortcuts?: TShortcuts;
|
|
622
|
-
/** Override default behaviors of Regle processors. */
|
|
623
|
-
overrides?: GlobalConfigOverrides<unknown>;
|
|
624
|
-
}
|
|
625
|
-
/**
|
|
626
|
-
* Define a global Regle configuration to customize the validation behavior across your application.
|
|
627
|
-
*
|
|
628
|
-
* Features:
|
|
629
|
-
* - Customize built-in rules messages
|
|
630
|
-
* - Add your custom rules with full type inference
|
|
631
|
-
* - Define global modifiers (lazy, rewardEarly, etc.)
|
|
632
|
-
* - Define shortcuts for common validation patterns
|
|
633
|
-
*
|
|
634
|
-
* @param options - Configuration options
|
|
635
|
-
* @returns Object containing typed `useRegle`, `inferRules`, and `useRules` functions
|
|
636
|
-
*
|
|
637
|
-
* @example
|
|
638
|
-
* ```ts
|
|
639
|
-
* import { defineRegleConfig } from '@regle/core';
|
|
640
|
-
* import { required, withMessage } from '@regle/rules';
|
|
641
|
-
*
|
|
642
|
-
* export const { useRegle, inferRules, useRules } = defineRegleConfig({
|
|
643
|
-
* rules: () => ({
|
|
644
|
-
* // Override default required message
|
|
645
|
-
* required: withMessage(required, 'This field cannot be empty'),
|
|
646
|
-
* // Add custom rule
|
|
647
|
-
* myCustomRule: createRule({
|
|
648
|
-
* validator: (value) => value === 'valid',
|
|
649
|
-
* message: 'Invalid value'
|
|
650
|
-
* })
|
|
651
|
-
* }),
|
|
652
|
-
* modifiers: {
|
|
653
|
-
* lazy: true,
|
|
654
|
-
* rewardEarly: true
|
|
655
|
-
* }
|
|
656
|
-
* });
|
|
657
|
-
* ```
|
|
658
|
-
*
|
|
659
|
-
* @see {@link https://reglejs.dev/advanced-usage/global-config Documentation}
|
|
660
|
-
*/
|
|
661
|
-
declare function defineRegleConfig<TShortcuts extends RegleShortcutDefinition<TCustomRules>, TCustomRules extends Partial<ExtendedRulesDeclarationsOverrides>>({
|
|
662
|
-
rules,
|
|
663
|
-
modifiers,
|
|
664
|
-
shortcuts,
|
|
665
|
-
overrides
|
|
666
|
-
}: GlobalConfigOptions<TCustomRules, TShortcuts>): {
|
|
667
|
-
useRegle: useRegleFn<Omit<TCustomRules, keyof DefaultValidators>, TShortcuts>;
|
|
668
|
-
inferRules: inferRulesFn<Omit<TCustomRules, keyof DefaultValidators>>;
|
|
669
|
-
useRules: useRulesFn<TCustomRules, TShortcuts>;
|
|
670
|
-
};
|
|
671
|
-
/**
|
|
672
|
-
* Extend an already created custom `useRegle` configuration with additional rules, modifiers, or shortcuts.
|
|
673
|
-
*
|
|
674
|
-
* @param regle - The existing useRegle function to extend
|
|
675
|
-
* @param options - Additional configuration to merge
|
|
676
|
-
* @param options.rules - Additional custom rules
|
|
677
|
-
* @param options.modifiers - Additional modifiers to merge
|
|
678
|
-
* @param options.shortcuts - Additional shortcuts to merge
|
|
679
|
-
* @returns Object containing the extended `useRegle` and `inferRules` functions
|
|
680
|
-
*
|
|
681
|
-
* @example
|
|
682
|
-
* ```ts
|
|
683
|
-
* import { extendRegleConfig } from '@regle/core';
|
|
684
|
-
* import { baseUseRegle } from './base-config';
|
|
685
|
-
*
|
|
686
|
-
* export const { useRegle, inferRules } = extendRegleConfig(baseUseRegle, {
|
|
687
|
-
* rules: () => ({
|
|
688
|
-
* additionalRule: myNewRule
|
|
689
|
-
* }),
|
|
690
|
-
* modifiers: {
|
|
691
|
-
* rewardEarly: true
|
|
692
|
-
* }
|
|
693
|
-
* });
|
|
694
|
-
* ```
|
|
695
|
-
*
|
|
696
|
-
* @see {@link https://reglejs.dev/advanced-usage/global-config Documentation}
|
|
697
|
-
*/
|
|
698
|
-
declare function extendRegleConfig<TRootCustomRules extends Partial<ExtendedRulesDeclarationsOverrides>, TRootShortcuts extends RegleShortcutDefinition<{}>, TShortcuts extends RegleShortcutDefinition<Merge<TRootCustomRules, TCustomRules>>, TCustomRules extends Partial<ExtendedRulesDeclarationsOverrides>>(regle: useRegleFn<TRootCustomRules, TRootShortcuts>, {
|
|
699
|
-
rules,
|
|
700
|
-
modifiers,
|
|
701
|
-
shortcuts,
|
|
702
|
-
overrides
|
|
703
|
-
}: GlobalConfigOptions<TCustomRules, TShortcuts>): {
|
|
704
|
-
useRegle: useRegleFn<Merge<TRootCustomRules, TCustomRules>, TRootShortcuts & TShortcuts>;
|
|
705
|
-
inferRules: inferRulesFn<Merge<TRootCustomRules, TCustomRules>>;
|
|
706
|
-
};
|
|
707
|
-
/**
|
|
708
|
-
* Define a global Regle options to customize the validation behavior across your application.
|
|
709
|
-
* It's meant to be used with the Regle Vue plugin.
|
|
710
|
-
*
|
|
711
|
-
* @param options - Configuration options
|
|
712
|
-
* @returns The configuration options
|
|
713
|
-
*
|
|
714
|
-
* @example
|
|
715
|
-
* ```ts
|
|
716
|
-
* import { defineRegleOptions } from '@regle/core';
|
|
717
|
-
*
|
|
718
|
-
* const regleOptions = defineRegleOptions({
|
|
719
|
-
* modifiers: {
|
|
720
|
-
* lazy: true,
|
|
721
|
-
* rewardEarly: true
|
|
722
|
-
* }
|
|
723
|
-
* });
|
|
724
|
-
* ```
|
|
725
|
-
*
|
|
726
|
-
* @see {@link https://reglejs.dev/advanced-usage/global-config Documentation}
|
|
370
|
+
* @param options - Configuration options
|
|
371
|
+
* @returns The configuration options
|
|
372
|
+
*
|
|
373
|
+
* @example
|
|
374
|
+
* ```ts
|
|
375
|
+
* import { defineRegleOptions } from '@regle/core';
|
|
376
|
+
*
|
|
377
|
+
* const regleOptions = defineRegleOptions({
|
|
378
|
+
* modifiers: {
|
|
379
|
+
* lazy: true,
|
|
380
|
+
* rewardEarly: true
|
|
381
|
+
* }
|
|
382
|
+
* });
|
|
383
|
+
* ```
|
|
384
|
+
*
|
|
385
|
+
* @see {@link https://reglejs.dev/advanced-usage/global-config Documentation}
|
|
727
386
|
*/
|
|
728
387
|
declare function defineRegleOptions<T extends GlobalConfigOptions<Partial<ExtendedRulesDeclarationsOverrides>, RegleShortcutDefinition<any>>>(options: T): T;
|
|
729
388
|
type useRegleFnOptions<TState extends Record<string, any> | MaybeInput<PrimitiveTypes>, TRules extends ReglePartialRuleTree<NonNullable<JoinDiscriminatedUnions<TState>>, CustomRulesDeclarationTree>, TAdditionalOptions extends Record<string, any>, TValidationGroups extends Record<string, RegleValidationGroupEntry[]>> = TState extends Record<string, any> ? Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<JoinDiscriminatedUnions<TState>, TRules, TValidationGroups> & TAdditionalOptions : Partial<DeepMaybeRef<RegleBehaviourOptions & FieldOnlyRegleBehaviourOptions>> & TAdditionalOptions;
|
|
@@ -918,72 +577,413 @@ interface useRulesFn<TCustomRules extends Partial<ExtendedRulesDeclarationsOverr
|
|
|
918
577
|
__config?: GlobalConfigOptions<TCustomRules, TShortcuts>;
|
|
919
578
|
}
|
|
920
579
|
/**
|
|
921
|
-
* `useRules` is a variant of `useRegle` that doesn't require you to provide initial state.
|
|
922
|
-
* It creates an empty state based on your rules structure and implements the Standard Schema spec.
|
|
923
|
-
*
|
|
924
|
-
* This is useful when you want to define validation rules first and infer the state type from them.
|
|
925
|
-
*
|
|
926
|
-
* @param rules - Your validation rules object
|
|
927
|
-
* @param modifiers - Optional configuration to customize regle behavior
|
|
928
|
-
* @returns The reactive validation state (implements StandardSchemaV1)
|
|
929
|
-
*
|
|
930
|
-
* @example
|
|
931
|
-
* ```ts
|
|
932
|
-
* import { useRules, type InferInput } from '@regle/core';
|
|
933
|
-
* import { required, string, email } from '@regle/rules';
|
|
934
|
-
*
|
|
935
|
-
* const r$ = useRules({
|
|
936
|
-
* name: { required, string },
|
|
937
|
-
* email: { required, email }
|
|
938
|
-
* });
|
|
939
|
-
*
|
|
940
|
-
* // State is automatically created and typed
|
|
941
|
-
* r$.$value.name // string | null
|
|
942
|
-
* r$.$value.email // string | null
|
|
580
|
+
* `useRules` is a variant of `useRegle` that doesn't require you to provide initial state.
|
|
581
|
+
* It creates an empty state based on your rules structure and implements the Standard Schema spec.
|
|
582
|
+
*
|
|
583
|
+
* This is useful when you want to define validation rules first and infer the state type from them.
|
|
584
|
+
*
|
|
585
|
+
* @param rules - Your validation rules object
|
|
586
|
+
* @param modifiers - Optional configuration to customize regle behavior
|
|
587
|
+
* @returns The reactive validation state (implements StandardSchemaV1)
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
* ```ts
|
|
591
|
+
* import { useRules, type InferInput } from '@regle/core';
|
|
592
|
+
* import { required, string, email } from '@regle/rules';
|
|
593
|
+
*
|
|
594
|
+
* const r$ = useRules({
|
|
595
|
+
* name: { required, string },
|
|
596
|
+
* email: { required, email }
|
|
597
|
+
* });
|
|
598
|
+
*
|
|
599
|
+
* // State is automatically created and typed
|
|
600
|
+
* r$.$value.name // string | null
|
|
601
|
+
* r$.$value.email // string | null
|
|
602
|
+
*
|
|
603
|
+
* // Can be used with Standard Schema compatible libraries
|
|
604
|
+
* const result = await r$['~standard'].validate({ name: '', email: '' });
|
|
605
|
+
* ```
|
|
606
|
+
*
|
|
607
|
+
* @see {@link https://reglejs.dev/common-usage/standard-schema#userules Documentation}
|
|
608
|
+
*/
|
|
609
|
+
declare const useRules: useRulesFn<Partial<ExtendedRulesDeclarationsOverrides>, RegleShortcutDefinition<any>>;
|
|
610
|
+
/**
|
|
611
|
+
* Marks a value as static and treats the constructor as a regular raw Field.
|
|
612
|
+
* @param value - The value to mark as static.
|
|
613
|
+
* @returns The marked value.
|
|
614
|
+
* @example
|
|
615
|
+
* ```ts
|
|
616
|
+
* const StaticDecimal = markStatic(Decimal);
|
|
617
|
+
* const StaticBigWrapper = markStatic(BigWrapper);
|
|
618
|
+
* ```
|
|
619
|
+
*/
|
|
620
|
+
declare function markStatic<T extends object>(value: T): T extends RegleStaticImpl<unknown> ? T : RegleStatic<T>;
|
|
621
|
+
/**
|
|
622
|
+
* Infer type of the `r$` of any function returning a regle instance
|
|
623
|
+
*/
|
|
624
|
+
type InferRegleRoot<T extends (...args: any[]) => SuperCompatibleRegle> = T extends ((...args: any[]) => infer U) ? U extends SuperCompatibleRegle ? U['r$'] : never : never;
|
|
625
|
+
/**
|
|
626
|
+
* Infer custom rules declared in a global configuration
|
|
627
|
+
*/
|
|
628
|
+
type InferRegleRules<T extends useRegleFn<any, any>> = T extends useRegleFn<infer U, any> ? UnwrapRuleTree<Partial<DefaultValidators>> & UnwrapRuleTree<Partial<U>> : {};
|
|
629
|
+
/**
|
|
630
|
+
* Infer custom shortcuts declared in a global configuration
|
|
631
|
+
*/
|
|
632
|
+
type InferRegleShortcuts<T extends useRegleFn<any, any>> = T extends useRegleFn<any, infer U> ? U : {};
|
|
633
|
+
/**
|
|
634
|
+
* Extract a set rules and setting them as required
|
|
635
|
+
*/
|
|
636
|
+
type RegleEnforceRequiredRules<TRules extends keyof DefaultValidators> = Omit<Partial<DefaultValidatorsTree>, TRules> & { [K in TRules]-?: UnwrapRuleWithParams<DefaultValidators[K]> };
|
|
637
|
+
/**
|
|
638
|
+
* Extract a set of custom rules from a global configuration and setting them as required
|
|
639
|
+
|
|
640
|
+
*/
|
|
641
|
+
type RegleEnforceCustomRequiredRules<T extends useRegleFn<any, any>, TRules extends keyof InferRegleRules<T>> = Omit<Partial<DefaultValidatorsTree>, TRules> & { [K in TRules]-?: T extends useRegleFn<any, any> ? K extends keyof InferRegleRules<T> ? NonNullable<UnwrapRuleWithParams<InferRegleRules<T>[K]>> : never : K extends keyof T ? NonNullable<T[K]> : never };
|
|
642
|
+
/**
|
|
643
|
+
* Extract custom rules and custom shortcuts and apply them to a RegleFieldStatus type
|
|
644
|
+
*/
|
|
645
|
+
type RegleCustomFieldStatus<T extends useRegleFn<any, any>, TState extends unknown = any, TRules extends keyof InferRegleRules<T> = never> = RegleFieldStatus<TState, [TRules] extends [never] ? Partial<InferRegleRules<T>> : RegleEnforceCustomRequiredRules<T, TRules>, InferRegleShortcuts<T>>;
|
|
646
|
+
interface GlobalConfigOverrides<TValue extends unknown = unknown> {
|
|
647
|
+
/**
|
|
648
|
+
* Override the default $edited handler.
|
|
649
|
+
*/
|
|
650
|
+
isEdited?: isEditedHandlerFn<TValue>;
|
|
651
|
+
}
|
|
652
|
+
type isEditedHandlerFn<TValue extends unknown = unknown> = (currentValue: MaybeInput<TValue>, initialValue: MaybeInput<TValue>, defaultHandlerFn: (currentValue: unknown, initialValue: unknown) => boolean) => boolean;
|
|
653
|
+
type RegleImmediateDirtyMode = 'eager' | 'non-empty' | 'lazy-non-empty';
|
|
654
|
+
interface RegleBehaviourOptions {
|
|
655
|
+
/**
|
|
656
|
+
* Does not run rules until the field is dirty.
|
|
657
|
+
* @default false
|
|
658
|
+
*/
|
|
659
|
+
lazy?: boolean | undefined;
|
|
660
|
+
/**
|
|
661
|
+
* Automatically set the dirty state to true when value is changed without the need of calling `$touch`.
|
|
662
|
+
* @default true
|
|
663
|
+
*
|
|
664
|
+
*/
|
|
665
|
+
autoDirty?: boolean | undefined;
|
|
666
|
+
/**
|
|
667
|
+
* Only update error status when calling `$validate` or `$validateSync`.
|
|
668
|
+
* Will not display errors as you type
|
|
669
|
+
* @default false
|
|
670
|
+
*
|
|
671
|
+
* @default true if rewardEarly is true
|
|
672
|
+
*
|
|
673
|
+
*/
|
|
674
|
+
silent?: boolean | undefined;
|
|
675
|
+
/**
|
|
676
|
+
* The fields will turn valid when they are, but not invalid unless calling `r$.$validate()` or `$validateSync`
|
|
677
|
+
* @default false
|
|
678
|
+
*/
|
|
679
|
+
rewardEarly?: boolean | undefined;
|
|
680
|
+
/**
|
|
681
|
+
* Define whether the external errors should be cleared when updating a field
|
|
682
|
+
*
|
|
683
|
+
* Default to `false` if `$silent` is set to `true`
|
|
684
|
+
*
|
|
685
|
+
* @default true
|
|
686
|
+
*/
|
|
687
|
+
clearExternalErrorsOnChange?: boolean | undefined;
|
|
688
|
+
/**
|
|
689
|
+
* Define whether the external errors should be cleared when calling `$validate` or `$validateSync`
|
|
690
|
+
*
|
|
691
|
+
* @default true
|
|
692
|
+
*/
|
|
693
|
+
clearExternalErrorsOnValidate?: boolean | undefined;
|
|
694
|
+
/**
|
|
695
|
+
* Set the dirty state when the form is initialized.
|
|
696
|
+
*
|
|
697
|
+
* - `true` or `'eager'`: mark every field dirty.
|
|
698
|
+
* - `'non-empty'`: mark every field dirty when any initial value is non-empty.
|
|
699
|
+
* - `'lazy-non-empty'`: mark only fields with non-empty initial values dirty.
|
|
700
|
+
*
|
|
701
|
+
* @default false
|
|
702
|
+
*/
|
|
703
|
+
immediateDirty?: boolean | RegleImmediateDirtyMode | undefined;
|
|
704
|
+
/**
|
|
705
|
+
* Disable all the computation
|
|
706
|
+
* @default false
|
|
707
|
+
*/
|
|
708
|
+
disabled?: boolean | undefined;
|
|
709
|
+
}
|
|
710
|
+
interface LocalRegleBehaviourOptions<TState extends Record<string, any>, TRules extends ReglePartialRuleTree<TState>, TValidationGroups extends Record<string, RegleValidationGroupEntry[]> = {}> {
|
|
711
|
+
/**
|
|
712
|
+
* A dictionary of external errors to be injected into the field statuses.
|
|
713
|
+
*
|
|
714
|
+
* Useful for integrating errors from a backend or other validation sources.
|
|
715
|
+
* External errors can be assigned using a reactive object or a Ref, and will be merged into the `$externalErrors` and `$errors` properties for each field.
|
|
716
|
+
* You can also call `r$.$setExternalErrors(...)` directly without providing this option.
|
|
717
|
+
* Pass this option when the same external errors ref needs to be owned and synchronized outside of Regle.
|
|
718
|
+
*
|
|
719
|
+
* More details: https://reglejs.dev/advanced-usage/external-errors
|
|
720
|
+
*/
|
|
721
|
+
externalErrors?: Ref<RegleExternalErrorTree<Unwrap<TState>> | Record<string, string[]>>;
|
|
722
|
+
/**
|
|
723
|
+
* Allows you to group fields for custom collective validation logic.
|
|
724
|
+
*
|
|
725
|
+
* The `validationGroups` option lets you define logical groupings of fields within your form that should be validated or checked together.
|
|
726
|
+
* This can be used, for example, to easily determine if a subset of your form (e.g. an "address" group or a set of "contact information" fields) are all valid or share a collective error state.
|
|
727
|
+
*
|
|
728
|
+
* The function receives the `$fields` object and must return an object where each key is a group name and the value is an array of RegleStatus or RegleFieldStatus instances representing the grouped fields.
|
|
729
|
+
* These groups can then be referenced using `$validationGroups.<groupName>` to access their combined validation state (e.g. `$invalid`, `$error`, `$errors`, etc).
|
|
730
|
+
*
|
|
731
|
+
* More details: https://reglejs.dev/core-concepts/modifiers#validationgroups
|
|
732
|
+
*/
|
|
733
|
+
validationGroups?: (fields: RegleStatus<TState, TRules>['$fields']) => TValidationGroups;
|
|
734
|
+
/**
|
|
735
|
+
* A unique identifier for the Regle instance in the devtools.
|
|
736
|
+
* @default undefined
|
|
737
|
+
*/
|
|
738
|
+
id?: string | undefined;
|
|
739
|
+
}
|
|
740
|
+
type FieldOnlyRegleBehaviourOptions = {
|
|
741
|
+
/**
|
|
742
|
+
* Set external errors for the field.
|
|
743
|
+
*/
|
|
744
|
+
externalErrors?: Ref<string[]>;
|
|
745
|
+
/**
|
|
746
|
+
* A unique identifier for the Regle instance in the devtools.
|
|
747
|
+
* @default undefined
|
|
748
|
+
*/
|
|
749
|
+
id?: string | undefined;
|
|
750
|
+
};
|
|
751
|
+
type RegleValidationGroupEntry = RegleFieldStatus<any, any> | undefined;
|
|
752
|
+
interface RegleValidationGroupOutput {
|
|
753
|
+
/** Indicates whether any field in the validation group is invalid. */
|
|
754
|
+
$invalid: boolean;
|
|
755
|
+
/** Convenience flag to easily decide if an error message should be displayed. True when any field in the group is dirty, not pending, and invalid. */
|
|
756
|
+
$error: boolean;
|
|
757
|
+
/** Indicates if any async rule in the validation group is currently running. */
|
|
758
|
+
$pending: boolean;
|
|
759
|
+
/** Indicates whether any field in the validation group has been interacted with by the user. */
|
|
760
|
+
$dirty: boolean;
|
|
761
|
+
/** Indicates whether all fields in the validation group pass validation and are dirty. */
|
|
762
|
+
$correct: boolean;
|
|
763
|
+
/** Collection of all error messages from fields in the group where $dirty equals true. */
|
|
764
|
+
$errors: string[];
|
|
765
|
+
/** Collection of all error messages from fields in the group, regardless of $dirty state. */
|
|
766
|
+
$silentErrors: string[];
|
|
767
|
+
}
|
|
768
|
+
type FieldRegleBehaviourOptions<TValue extends unknown = unknown> = AddDollarToOptions<RegleBehaviourOptions> & {
|
|
769
|
+
/**
|
|
770
|
+
* Let you declare the number of milliseconds the rule needs to wait before executing. Useful for async or heavy computations.
|
|
771
|
+
*/
|
|
772
|
+
$debounce?: number;
|
|
773
|
+
/**
|
|
774
|
+
* Override the default `$edited` handler.
|
|
775
|
+
*/
|
|
776
|
+
$isEdited?: isEditedHandlerFn<TValue>;
|
|
777
|
+
};
|
|
778
|
+
type CollectionRegleBehaviourOptions<TValue extends unknown = unknown> = FieldRegleBehaviourOptions<TValue> & {
|
|
779
|
+
/**
|
|
780
|
+
* Allow deep compare of array children to compute the `$edited` property
|
|
781
|
+
*
|
|
782
|
+
* Disabled by default for performance
|
|
783
|
+
*
|
|
784
|
+
* @default false
|
|
785
|
+
* */
|
|
786
|
+
$deepCompare?: boolean;
|
|
787
|
+
};
|
|
788
|
+
type ResolvedRegleBehaviourOptions = DeepMaybeRef<RequiredDeep<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<Record<string, any>, Record<string, any>, Record<string, any[]>>;
|
|
789
|
+
type ShortcutCommonFn<T extends Record<string, any>> = {
|
|
790
|
+
[x: string]: (element: Omit<OmitByType<T, Function>, '~standard'>) => unknown;
|
|
791
|
+
};
|
|
792
|
+
type ShortcutFieldStatus<TCustomRules extends Record<string, any>> = RegleFieldStatus<any, Partial<TCustomRules> & Partial<DefaultValidators>>;
|
|
793
|
+
type ShortcutNestedStatus<TCustomRules extends Record<string, any>> = RegleStatus<Record<string, any>, ReglePartialRuleTree<any, Partial<TCustomRules>>>;
|
|
794
|
+
type ShortcutCollectionStatus<TCustomRules extends Record<string, any>> = RegleCollectionStatus<any[], Partial<TCustomRules> & Partial<DefaultValidators>>;
|
|
795
|
+
type RegleShortcutDefinition<TCustomRules extends Record<string, any> = {}> = {
|
|
796
|
+
/**
|
|
797
|
+
* Allow you to customize the properties for every field
|
|
798
|
+
*/
|
|
799
|
+
fields?: ShortcutCommonFn<ShortcutFieldStatus<TCustomRules>>;
|
|
800
|
+
/**
|
|
801
|
+
* Allow you to customize the properties for every parent of a nested object
|
|
802
|
+
*/
|
|
803
|
+
nested?: ShortcutCommonFn<ShortcutNestedStatus<TCustomRules>>;
|
|
804
|
+
/**
|
|
805
|
+
* Allow you to customize the properties for every parent of a collection
|
|
806
|
+
*/
|
|
807
|
+
collections?: ShortcutCommonFn<ShortcutCollectionStatus<TCustomRules>>;
|
|
808
|
+
};
|
|
809
|
+
type AddDollarToOptions<T extends Record<string, any>> = { [K in keyof T as `$${string & K}`]: T[K] };
|
|
810
|
+
/**
|
|
811
|
+
* The main Regle type that represents a complete validation instance.
|
|
943
812
|
*
|
|
944
|
-
*
|
|
945
|
-
*
|
|
946
|
-
*
|
|
813
|
+
* @template TState - The shape of the state object being validated
|
|
814
|
+
* @template TRules - The validation rules tree for the state
|
|
815
|
+
* @template TValidationGroups - Groups of validation rules that can be run together
|
|
816
|
+
* @template TShortcuts - Custom shortcut definitions for common validation patterns
|
|
817
|
+
* @template TAdditionalReturnProperties - Additional properties to extend the return type
|
|
947
818
|
*
|
|
948
|
-
* @see {@link https://reglejs.dev/common-usage/standard-schema#userules Documentation}
|
|
949
|
-
*/
|
|
950
|
-
declare const useRules: useRulesFn<Partial<ExtendedRulesDeclarationsOverrides>, RegleShortcutDefinition<any>>;
|
|
951
|
-
/**
|
|
952
|
-
* Marks a value as static and treats the constructor as a regular raw Field.
|
|
953
|
-
* @param value - The value to mark as static.
|
|
954
|
-
* @returns The marked value.
|
|
955
|
-
* @example
|
|
956
|
-
* ```ts
|
|
957
|
-
* const StaticDecimal = markStatic(Decimal);
|
|
958
|
-
* const StaticBigWrapper = markStatic(BigWrapper);
|
|
959
|
-
* ```
|
|
960
|
-
*/
|
|
961
|
-
declare function markStatic<T extends object>(value: T): T extends RegleStaticImpl<unknown> ? T : RegleStatic<T>;
|
|
962
|
-
/**
|
|
963
|
-
* Infer type of the `r$` of any function returning a regle instance
|
|
964
|
-
*/
|
|
965
|
-
type InferRegleRoot<T extends (...args: any[]) => SuperCompatibleRegle> = T extends ((...args: any[]) => infer U) ? U extends SuperCompatibleRegle ? U['r$'] : never : never;
|
|
966
|
-
/**
|
|
967
|
-
* Infer custom rules declared in a global configuration
|
|
968
|
-
*/
|
|
969
|
-
type InferRegleRules<T extends useRegleFn<any, any>> = T extends useRegleFn<infer U, any> ? UnwrapRuleTree<Partial<DefaultValidators>> & UnwrapRuleTree<Partial<U>> : {};
|
|
970
|
-
/**
|
|
971
|
-
* Infer custom shortcuts declared in a global configuration
|
|
972
|
-
*/
|
|
973
|
-
type InferRegleShortcuts<T extends useRegleFn<any, any>> = T extends useRegleFn<any, infer U> ? U : {};
|
|
974
|
-
/**
|
|
975
|
-
* Extract a set rules and setting them as required
|
|
976
819
|
*/
|
|
977
|
-
type
|
|
820
|
+
type Regle<TState extends Record<string, any> = EmptyObject, TRules extends ReglePartialRuleTree<TState, CustomRulesDeclarationTree> = EmptyObject, TValidationGroups extends Record<string, RegleValidationGroupEntry[]> = {}, TShortcuts extends RegleShortcutDefinition = {}, TAdditionalReturnProperties extends Record<string, any> = {}> = {
|
|
821
|
+
/**
|
|
822
|
+
* r$ is a reactive object containing the values, errors, dirty state and all the necessary validations properties you'll need to display information.
|
|
823
|
+
*
|
|
824
|
+
* To see the list of properties: {@link https://reglejs.dev/core-concepts/validation-properties}
|
|
825
|
+
*/
|
|
826
|
+
r$: Raw<RegleRoot<TState, TRules, TValidationGroups, TShortcuts>>;
|
|
827
|
+
} & TAdditionalReturnProperties;
|
|
978
828
|
/**
|
|
979
|
-
*
|
|
980
|
-
|
|
829
|
+
* The type for a single field validation instance.
|
|
830
|
+
*
|
|
831
|
+
* @template TState - The type of the state value being validated
|
|
832
|
+
* @template TRules - The validation rules for the field
|
|
833
|
+
* @template TShortcuts - Custom shortcut definitions for common validation patterns
|
|
834
|
+
* @template TAdditionalReturnProperties - Additional properties to extend the return type
|
|
981
835
|
*/
|
|
982
|
-
type
|
|
836
|
+
type RegleSingleField<TState extends Maybe<PrimitiveTypes> = any, TRules extends RegleRuleDecl<NonNullable<TState>, CustomRulesDeclarationTree> = EmptyObject, TShortcuts extends RegleShortcutDefinition = {}, TAdditionalReturnProperties extends Record<string, any> = {}> = {
|
|
837
|
+
/**
|
|
838
|
+
* r$ is a reactive object containing the values, errors, dirty state and all the necessary validations properties you'll need to display information.
|
|
839
|
+
*
|
|
840
|
+
* To see the list of properties: {@link https://reglejs.dev/core-concepts/validation-properties}
|
|
841
|
+
*/
|
|
842
|
+
r$: Raw<RegleFieldStatus<TState, TRules, TShortcuts>>;
|
|
843
|
+
} & TAdditionalReturnProperties;
|
|
844
|
+
type DeepReactiveState<T extends Record<string, any> | unknown | undefined> = IsAny<T> extends true ? any : IsUnknown<T> extends true ? never : ExtendOnlyRealRecord<T> extends true ? { [K in keyof T]: InferDeepReactiveState<T[K]> } : never;
|
|
845
|
+
type InferDeepReactiveState<TState> = IsAny<TState> extends true ? any : IsUnknown<TState> extends true ? MaybeRef<TState> : NonNullable<TState> extends Array<infer U extends Record<string, any>> ? DeepReactiveState<U[]> : NonNullable<TState> extends Date | File ? MaybeRef<TState> : NonNullable<TState> extends Record<string, any> ? DeepReactiveState<TState> : MaybeRef<TState>;
|
|
846
|
+
type ResetOptions<TState extends unknown> = RequireOneOrNone<{
|
|
847
|
+
/**
|
|
848
|
+
* Reset validation status and reset form state to its initial state.
|
|
849
|
+
*
|
|
850
|
+
* Initial state is different than the original state as the initial state can be mutated when using `$reset`.
|
|
851
|
+
*
|
|
852
|
+
* This serve as the base comparison state for `$edited` property.
|
|
853
|
+
*
|
|
854
|
+
* ⚠️ This doesn't work if the state is a `reactive` object.
|
|
855
|
+
*/
|
|
856
|
+
toInitialState?: boolean;
|
|
857
|
+
/**
|
|
858
|
+
* Reset validation status and reset form state to its original state.
|
|
859
|
+
*
|
|
860
|
+
* Original state is the unmutated state that was passed to the form when it was initialized.
|
|
861
|
+
*/
|
|
862
|
+
toOriginalState?: boolean;
|
|
863
|
+
/**
|
|
864
|
+
* Reset validation status and reset form state to the given state
|
|
865
|
+
* Also set the new state as the initial state.
|
|
866
|
+
*/
|
|
867
|
+
toState?: TState | (() => TState);
|
|
868
|
+
/**
|
|
869
|
+
* Clears the $externalErrors state back to an empty object.
|
|
870
|
+
*
|
|
871
|
+
* @default true
|
|
872
|
+
*/
|
|
873
|
+
clearExternalErrors?: boolean;
|
|
874
|
+
/**
|
|
875
|
+
* Keep the validation state of the form ($dirty, $invalid, $pending etc..)
|
|
876
|
+
* Only useful if you only want to reset the form state.
|
|
877
|
+
*
|
|
878
|
+
* @default false
|
|
879
|
+
*/
|
|
880
|
+
keepValidationState?: boolean;
|
|
881
|
+
}, 'toInitialState' | 'toState'>;
|
|
882
|
+
type ScopedInstancesRecord = Record<string, Record<string, SuperCompatibleRegleRoot>> & {
|
|
883
|
+
'~~global': Record<string, SuperCompatibleRegleRoot>;
|
|
884
|
+
};
|
|
885
|
+
type ScopedInstancesRecordLike = Partial<ScopedInstancesRecord>;
|
|
886
|
+
type PartialFormState<TState extends Record<string, any>> = [unknown] extends [TState] ? {} : Prettify<{ [K in keyof TState as ExtendOnlyRealRecord<TState[K]> extends true ? never : TState[K] extends Array<any> ? never : K]?: MaybeOutput<TState[K]> } & { [K in keyof TState as ExtendOnlyRealRecord<TState[K]> extends true ? K : TState[K] extends Array<any> ? K : never]: NonNullable<TState[K]> extends Array<infer U extends Record<string, any>> ? PartialFormState<U>[] : PartialFormState<TState[K]> }>;
|
|
887
|
+
type RegleResult<Data extends Record<string, any> | any[] | unknown, TRules extends ReglePartialRuleTree<any> | RegleFormPropertyType<any>> = {
|
|
888
|
+
/** The result of the validation */valid: false; /** Output data, filter the result by checking the `valid` property */
|
|
889
|
+
data: IsUnknown<Data> extends true ? unknown : IsAny<Data> extends true ? unknown : IsEmptyObject<TRules> extends true ? isRecordLiteral<Data> extends true ? PartialFormState<Data extends Record<string, any> ? Data : {}> : MaybeOutput<Data> : HasNamedKeys<Data> extends true ? HasNamedKeys<TRules> extends true ? NonNullable<Data> extends Date | File ? MaybeOutput<Raw<Data>> : NonNullable<Data> extends Array<infer U extends Record<string, any>> ? PartialFormState<U>[] : NonNullable<Data> extends Record<string, any> ? PartialFormState<NonNullable<Data>> : MaybeOutput<Data> : isRecordLiteral<Data> extends true ? PartialFormState<Data extends Record<string, any> ? Data : {}> : MaybeOutput<Data> : unknown;
|
|
890
|
+
/**
|
|
891
|
+
* Collection of all the error messages, collected for all children properties and nested forms.
|
|
892
|
+
*
|
|
893
|
+
* Only contains errors from properties where $dirty equals true.
|
|
894
|
+
* */
|
|
895
|
+
issues: DataTypeRegleIssues<Data, TRules>;
|
|
896
|
+
/**
|
|
897
|
+
* Collection of all the issues, collected for all children properties and nested forms.
|
|
898
|
+
*
|
|
899
|
+
* Only contains issues from properties where $dirty equals true.
|
|
900
|
+
*/
|
|
901
|
+
errors: DataTypeRegleErrors<Data>;
|
|
902
|
+
} | {
|
|
903
|
+
valid: true;
|
|
904
|
+
data: IsUnknown<Data> extends true ? unknown : IsAny<Data> extends true ? unknown : IsEmptyObject<TRules> extends true ? isRecordLiteral<Data> extends true ? PartialFormState<Data extends Record<string, any> ? Data : {}> : MaybeOutput<Data> : HasNamedKeys<Data> extends true ? HasNamedKeys<TRules> extends true ? NonNullable<Data> extends Array<infer U extends Record<string, any>> ? DeepSafeFormState<U, TRules extends ReglePartialRuleTree<any> ? TRules : {}>[] : NonNullable<Data> extends Date | File ? SafeFieldProperty<Raw<NonNullable<Data>>, TRules extends ReglePartialRuleTree<any> ? TRules : {}> : NonNullable<Data> extends Record<string, any> ? DeepSafeFormState<NonNullable<Data>, TRules extends ReglePartialRuleTree<any> ? TRules : {}> : SafeFieldProperty<Data, TRules extends ReglePartialRuleTree<any> ? TRules : {}> : isRecordLiteral<Data> extends true ? PartialFormState<Data extends Record<string, any> ? Data : {}> : MaybeOutput<Data> : unknown;
|
|
905
|
+
issues: EmptyObject;
|
|
906
|
+
errors: EmptyObject;
|
|
907
|
+
};
|
|
908
|
+
type DataTypeRegleIssues<TData extends Record<string, any> | any[] | unknown, TRules extends ReglePartialRuleTree<any> | RegleFormPropertyType<any> = never> = HasNamedKeys<TData> extends true ? NonNullable<TData> extends Array<infer U> ? isRecordLiteral<U> extends true ? RegleFieldIssue<TRules>[] : RegleCollectionErrors<TData, true> : isRecordLiteral<TData> extends true ? RegleIssuesTree<TData> : RegleFieldIssue<TRules>[] : RegleFieldIssue | RegleCollectionErrors<TData, true> | RegleIssuesTree<TData>;
|
|
909
|
+
type DataTypeRegleErrors<TData extends Record<string, any> | any[] | unknown> = HasNamedKeys<TData> extends true ? NonNullable<TData> extends Array<infer U> ? isRecordLiteral<U> extends true ? string[] : RegleCollectionErrors<TData> : isRecordLiteral<TData> extends true ? RegleErrorTree<TData> : string[] : string[] | RegleErrorTree<TData> | RegleCollectionErrors<TData>;
|
|
910
|
+
type RegleCollectionResult<Data extends any[], TRules extends ReglePartialRuleTree<any> | RegleFormPropertyType<any>> = RegleResult<Data, TRules> & ({
|
|
911
|
+
valid: false;
|
|
912
|
+
issues: RegleCollectionErrors<Data, true>;
|
|
913
|
+
errors: RegleCollectionErrors<Data>;
|
|
914
|
+
} | {
|
|
915
|
+
valid: true;
|
|
916
|
+
issues: EmptyObject;
|
|
917
|
+
errors: EmptyObject;
|
|
918
|
+
});
|
|
919
|
+
type RegleFieldResult<Data extends any, TRules extends ReglePartialRuleTree<any> | RegleFormPropertyType<any>> = RegleResult<Data, TRules> & ({
|
|
920
|
+
valid: false;
|
|
921
|
+
issues: RegleFieldIssue<TRules>[];
|
|
922
|
+
errors: string[];
|
|
923
|
+
} | {
|
|
924
|
+
valid: true;
|
|
925
|
+
issues: [];
|
|
926
|
+
errors: [];
|
|
927
|
+
});
|
|
928
|
+
type $InternalRegleResult = {
|
|
929
|
+
valid: boolean;
|
|
930
|
+
data: any;
|
|
931
|
+
errors: $InternalRegleErrorTree | $InternalRegleCollectionErrors | string[];
|
|
932
|
+
issues: $InternalRegleIssuesTree | $InternalRegleCollectionIssues | RegleFieldIssue[];
|
|
933
|
+
};
|
|
934
|
+
type DeepSafeFormState<TState extends Record<string, any>, TRules extends ReglePartialRuleTree<Record<string, any>, CustomRulesDeclarationTree> | undefined> = IsUnknown<TState> extends true ? {} : IsAny<TState> extends true ? unknown : TRules extends undefined ? TState : HasNamedKeys<TState> extends true ? TRules extends ReglePartialRuleTree<TState, CustomRulesDeclarationTree> ? Prettify<{ [K in keyof TState as IsPropertyOutputRequired<TState[K], TRules[K]> extends false ? K : never]?: SafeProperty<TState[K], TRules[K]> extends MaybeInput<infer M> ? MaybeOutput<M> : SafeProperty<TState[K], TRules[K]> } & { [K in keyof TState as IsPropertyOutputRequired<TState[K], TRules[K]> extends false ? never : K]-?: IsUnknown<SafeProperty<TState[K], TRules[K]>> extends true ? unknown : IsAny<TState[K]> extends true ? unknown : NonNullable<SafeProperty<TState[K], TRules[K]>> }> : TState : TState;
|
|
935
|
+
type FieldHaveRequiredRule<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends MaybeRefOrComputedRef<RegleRuleDecl<any, any>> ? UnwrapRef<TRule> extends infer TRuleResolved extends Record<string, any> ? { [K in keyof TRuleResolved]-?: TRuleResolved[K] extends RegleRuleDefinition<unknown, any, any[], any, any, any, any, true> ? true : false }[keyof TRuleResolved] extends false ? false : true : false : false;
|
|
936
|
+
type ObjectHaveAtLeastOneRequiredField<TState extends Record<string, any>, TRules extends ReglePartialRuleTree<TState, any>> = TState extends Maybe<TState> ? ({ [K in keyof NonNullable<TState>]-?: IsPropertyOutputRequired<NonNullable<TState>[K], TRules[K] extends RegleFormPropertyType<any, any> ? TRules[K] : {}> } & {
|
|
937
|
+
$self: IsPropertyOutputRequired<string, TRules['$self']>;
|
|
938
|
+
})[keyof TState | '$self'] extends false ? false : true : true;
|
|
939
|
+
type ArrayHaveAtLeastOneRequiredField<TState extends Maybe<any[]>, TRule extends RegleCollectionRuleDecl<any>> = TState extends Maybe<TState> ? FieldHaveRequiredRule<Omit<TRule, '$each'> extends MaybeRef<RegleRuleDecl> ? Omit<UnwrapRef<TRule>, '$each'> : {}> | ObjectHaveAtLeastOneRequiredField<ArrayElement<NonNullable<TState>>, ExtractFromGetter<TRule['$each']> extends undefined ? {} : NonNullable<ExtractFromGetter<TRule['$each']>>> extends false ? false : true : true;
|
|
940
|
+
type SafeProperty<TState, TRule extends RegleFormPropertyType<any, any> | undefined> = unknown extends TState ? unknown : [TRule] extends [RegleCollectionRuleDecl<any, any>] ? TState extends Array<infer U extends Record<string, any>> ? DeepSafeFormState<U, ExtractFromGetter<TRule['$each']>>[] : TState : [TRule] extends [ReglePartialRuleTree<any, any>] ? ExtendOnlyRealRecord<TState> extends true ? DeepSafeFormState<NonNullable<TState> extends Record<string, any> ? JoinDiscriminatedUnions<NonNullable<TState>> : {}, TRule> : [TRule] extends [MaybeRef<RegleRuleDecl<any, any>>] ? FieldHaveRequiredRule<UnwrapRef<TRule>> extends true ? TState : MaybeOutput<TState> : TState : TState;
|
|
941
|
+
type IsPropertyOutputRequired<TState, TRule extends RegleFormPropertyType<any, any> | undefined> = [unknown] extends [TState] ? unknown : NonNullable<TState> extends Array<any> ? TRule extends RegleCollectionRuleDecl<any, any> ? ArrayHaveAtLeastOneRequiredField<NonNullable<TState>, TRule> extends false ? false : true : false : TRule extends ReglePartialRuleTree<any, any> ? ExtendOnlyRealRecord<TState> extends true ? ObjectHaveAtLeastOneRequiredField<NonNullable<TState> extends Record<string, any> ? NonNullable<TState> : {}, TRule extends ReglePartialRuleTree<NonNullable<TState> extends Record<string, any> ? NonNullable<TState> : {}, any> ? TRule : {}> extends false ? false : true : TRule extends MaybeRef<RegleRuleDecl<any, any>> ? FieldHaveRequiredRule<UnwrapRef<TRule>> extends false ? false : true : false : false;
|
|
942
|
+
type SafeFieldProperty<TState, TRule extends RegleFormPropertyType<any, any> | undefined = never> = FieldHaveRequiredRule<TRule> extends true ? NonNullable<TState> : MaybeOutput<TState>;
|
|
943
|
+
/** Types to be augmented by @regle/schemas */
|
|
944
|
+
interface NarrowVariantExtracts {}
|
|
945
|
+
interface NarrowVariantFieldExtracts<T extends unknown> {}
|
|
946
|
+
type NarrowVariant<TRoot extends {
|
|
947
|
+
[x: string]: unknown;
|
|
948
|
+
$fields: {
|
|
949
|
+
[x: string]: unknown;
|
|
950
|
+
};
|
|
951
|
+
$value: unknown;
|
|
952
|
+
}, TKey extends keyof TRoot, TValue extends (LazyJoinDiscriminatedUnions<Exclude<TRoot[TKey], RegleCollectionStatus<any, any, any> | RegleStatus<any, any, any> | NarrowVariantExtracts[keyof NarrowVariantExtracts]>> extends {
|
|
953
|
+
$value: infer V;
|
|
954
|
+
} ? V : unknown)> = Extract<TRoot, { [K in TKey]: RegleFieldStatus<TValue, any, any> | RegleFieldStatus<MaybeInput<TValue>, any, any> | (IsEmptyObject<NarrowVariantFieldExtracts<TValue>> extends true ? EmptyObject : NarrowVariantFieldExtracts<TValue>[keyof NarrowVariantFieldExtracts<TValue>]) }> & {
|
|
955
|
+
$fields: Extract<TRoot['$fields'], { [K in TKey]: RegleFieldStatus<TValue, any, any> | RegleFieldStatus<MaybeInput<TValue>, any, any> | (IsEmptyObject<NarrowVariantFieldExtracts<TValue>> extends true ? EmptyObject : NarrowVariantFieldExtracts<TValue>[keyof NarrowVariantFieldExtracts<TValue>]) }> & { [K in TKey]: TRoot[K] & {
|
|
956
|
+
$value: TValue;
|
|
957
|
+
} };
|
|
958
|
+
} & {
|
|
959
|
+
$value: Omit<TRoot['$value'], TKey> & { [K in TKey]: TValue };
|
|
960
|
+
} & { [K in TKey]: TRoot[K] & {
|
|
961
|
+
$value: TValue;
|
|
962
|
+
} };
|
|
963
|
+
type MaybeVariantStatus<TState extends Record<string, any> | undefined = Record<string, any>, TRules extends ReglePartialRuleTree<NonNullable<TState>> = Record<string, any>, TShortcuts extends RegleShortcutDefinition = {}, TIsUnionOverride extends boolean = false,
|
|
983
964
|
/**
|
|
984
|
-
*
|
|
965
|
+
* Workaround for $each variants, TS generic can't detect if the Rules are an union when type is too nested, to the tuple is passed from parent
|
|
985
966
|
*/
|
|
986
|
-
|
|
967
|
+
TRulesTuple extends readonly any[] = never> = IsUnion<NonNullable<TState>> extends true ? Or<TIsUnionOverride, IsUnion<TRules>> extends true ? (IsNever<TRulesTuple> extends true ? TRules : TRulesTuple[number]) extends infer RulesUnionTuple extends ReglePartialRuleTree<NonNullable<TState>> ? ProcessChildrenFields<NonNullable<TState>, RulesUnionTuple, TShortcuts> extends infer TChildren ? Omit<RegleStatus<NonNullable<TState>, RulesUnionTuple, TShortcuts>, '$fields'> & {
|
|
968
|
+
$fields: TChildren[keyof TChildren];
|
|
969
|
+
} & (HasNamedKeys<NonNullable<TState>> extends true ? TChildren[keyof TChildren] : {}) : never : RegleStatus<JoinDiscriminatedUnions<NonNullable<TState>>, TRules extends ReglePartialRuleTree<NonNullable<JoinDiscriminatedUnions<NonNullable<TState>>>> ? TRules : EmptyObject, TShortcuts> : RegleStatus<JoinDiscriminatedUnions<NonNullable<TState>>, TRules extends ReglePartialRuleTree<NonNullable<JoinDiscriminatedUnions<NonNullable<TState>>>> ? TRules : EmptyObject, TShortcuts> : RegleStatus<TState, TRules, TShortcuts>;
|
|
970
|
+
/** Helper type to extract state tuple item at index */
|
|
971
|
+
type StateTupleItem<TStateTuple extends readonly any[], TIndexInt extends number> = TStateTuple[TIndexInt] extends Record<string, any> ? TStateTuple[TIndexInt] : never;
|
|
972
|
+
/** Helper type to extract NonNullable state tuple item */
|
|
973
|
+
type NonNullableStateTupleItem<TStateTuple extends readonly any[], TIndexInt extends number> = NonNullable<StateTupleItem<TStateTuple, TIndexInt>>;
|
|
974
|
+
/** Helper type to find the corresponding variant rule */
|
|
975
|
+
type VariantRuleForKey<TStateTuple extends readonly any[], TRulesTuple extends readonly any[], TIndexInt extends number, TKey> = FindCorrespondingVariant<StateTupleItem<TStateTuple, TIndexInt>, TRulesTuple> extends [infer U] ? TKey extends keyof U ? U[TKey] : EmptyObject : EmptyObject;
|
|
976
|
+
type ProcessChildrenFields<TState extends Record<string, any> | undefined, TRules extends ReglePartialRuleTree<NonNullable<TState>>, TShortcuts extends RegleShortcutDefinition = {}> = UnionToTuple<TState> extends infer TStateTuple extends readonly any[] ? UnionToTuple<TRules> extends infer TRulesTuple extends readonly any[] ? { [TIndex in keyof TupleToPlainObj<TStateTuple>]: TIndex extends `${infer TIndexInt extends number}` ? { [TKey in keyof TStateTuple[TIndexInt] as IsEmptyObject<VariantRuleForKey<TStateTuple, TRulesTuple, TIndexInt, TKey>> extends true ? TKey extends keyof TState ? TState[TKey] extends NonNullable<TState[TKey]> ? TKey : never : never : TKey]-?: InferRegleStatusType<VariantRuleForKey<TStateTuple, TRulesTuple, TIndexInt, TKey>, NonNullableStateTupleItem<TStateTuple, TIndexInt>, TKey, TShortcuts> } & { [TKey in keyof TStateTuple[TIndexInt] as IsEmptyObject<VariantRuleForKey<TStateTuple, TRulesTuple, TIndexInt, TKey>> extends true ? TKey extends keyof TState ? TState[TKey] extends NonNullable<TState[TKey]> ? never : TKey : TKey : never]?: InferRegleStatusType<VariantRuleForKey<TStateTuple, TRulesTuple, TIndexInt, TKey>, NonNullableStateTupleItem<TStateTuple, TIndexInt>, TKey, TShortcuts> } : {} } : {} : {};
|
|
977
|
+
type FindCorrespondingVariant<TState extends Record<string, any>, TRules extends readonly any[]> = TRules extends [infer F, ...infer R] ? F extends ReglePartialRuleTree<TState> ? [F] : FindCorrespondingVariant<TState, R> : [];
|
|
978
|
+
type PossibleLiteralTypes<T extends Record<string, any>, TKey extends keyof T> = unknown extends T[TKey] ? {
|
|
979
|
+
[x: string]: { [K in TKey]-?: Omit<RegleRuleDecl<any, Partial<ExtendedRulesDeclarations>>, 'literal'> & {
|
|
980
|
+
literal?: RegleRuleDefinition<'literal', any, [literal: any], false, boolean, any, string | number, true>;
|
|
981
|
+
} };
|
|
982
|
+
} : { [TVal in NonNullable<T[TKey]>]: { [K in TKey]-?: Omit<RegleRuleDecl<TVal, Partial<ExtendedRulesDeclarations>>, 'literal'> & {
|
|
983
|
+
literal?: RegleRuleDefinition<'literal', MaybeInput<TVal>, [literal: TVal], false, boolean, MaybeInput<TVal>, string | number, true>;
|
|
984
|
+
} } };
|
|
985
|
+
type RequiredForm<T extends Record<string, any>, TKey extends keyof T> = Omit<ReglePartialRuleTree<T>, TKey> & PossibleLiteralTypes<T, TKey>[keyof PossibleLiteralTypes<T, TKey>];
|
|
986
|
+
type VariantTuple<T extends Record<string, any>, TKey extends keyof T> = [RequiredForm<T, TKey>, ...RequiredForm<T, TKey>[]];
|
|
987
987
|
/**
|
|
988
988
|
* Infer safe output type from a rules object and it's original state type
|
|
989
989
|
*
|