@regle/core 0.0.1 → 0.0.4-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -0
- package/dist/index.cjs +692 -16882
- package/dist/index.d.cts +131 -48
- package/dist/index.d.ts +131 -48
- package/dist/index.js +688 -16884
- package/package.json +7 -4
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as vue from 'vue';
|
|
2
|
-
import { MaybeRef
|
|
2
|
+
import { MaybeRef } from 'vue';
|
|
3
3
|
|
|
4
4
|
type ArrayElement<T> = T extends Array<infer U> ? U : never;
|
|
5
5
|
|
|
@@ -9,19 +9,18 @@ type Maybe<T> = T | null | undefined;
|
|
|
9
9
|
* @argument
|
|
10
10
|
* createRule arguments options
|
|
11
11
|
*/
|
|
12
|
-
interface RegleRuleInit<TValue extends any, TParams extends any[] = []> {
|
|
12
|
+
interface RegleRuleInit<TValue extends any, TParams extends any[] = [], TType extends string = string> {
|
|
13
13
|
validator: (value: Maybe<TValue>, ...args: TParams) => boolean | Promise<boolean>;
|
|
14
14
|
message: string | ((value: Maybe<TValue>, ...args: TParams) => string);
|
|
15
15
|
active?: boolean | ((value: Maybe<TValue>, ...args: TParams) => boolean);
|
|
16
|
-
type:
|
|
16
|
+
type: TType;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
type DefaultValidators = {
|
|
20
20
|
maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
|
|
21
21
|
required: RegleRuleDefinition<unknown, []>;
|
|
22
22
|
requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
|
|
23
23
|
};
|
|
24
|
-
type DefaultValidators = typeof defaultValidators;
|
|
25
24
|
|
|
26
25
|
type ParamDecl<T = any> = MaybeRef<T> | (() => T);
|
|
27
26
|
type CreateFn<T extends any[]> = (...args: T) => any;
|
|
@@ -93,26 +92,62 @@ type RegleCollectionRuleDefinition<TValue = any[], TCustomRules extends AllRules
|
|
|
93
92
|
type CustomRulesDeclarationTree = Record<string, RegleRuleRaw<any, any>>;
|
|
94
93
|
type AllRulesDeclarations = CustomRulesDeclarationTree & DefaultValidators;
|
|
95
94
|
|
|
95
|
+
/**
|
|
96
|
+
* @public
|
|
97
|
+
*/
|
|
96
98
|
type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
|
|
97
99
|
[TKey in keyof TForm]?: RegleFormPropertyType<TForm[TKey], TCustomRules>;
|
|
98
100
|
};
|
|
101
|
+
/**
|
|
102
|
+
* @internal
|
|
103
|
+
* @reference {@link ReglePartialValidationTree}
|
|
104
|
+
*/
|
|
105
|
+
type $InternalReglePartialValidationTree = {
|
|
106
|
+
[x: string]: $InternalFormPropertyTypes;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
99
111
|
type RegleFormPropertyType<TValue = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = [NonNullable<TValue>] extends [never] ? RegleRuleDecl<unknown, TCustomRules> : NonNullable<TValue> extends Array<any> ? RegleCollectionRuleDecl<TValue, TCustomRules> : NonNullable<TValue> extends Date ? RegleRuleDecl<NonNullable<TValue>, TCustomRules> : NonNullable<TValue> extends File ? RegleRuleDecl<NonNullable<TValue>, TCustomRules> : NonNullable<TValue> extends Record<string, any> ? ReglePartialValidationTree<NonNullable<TValue>, TCustomRules> : RegleRuleDecl<NonNullable<TValue>, TCustomRules>;
|
|
100
112
|
/**
|
|
113
|
+
* @internal
|
|
114
|
+
* @reference {@link RegleFormPropertyType}
|
|
115
|
+
*/
|
|
116
|
+
type $InternalFormPropertyTypes = $InternalRegleRuleDecl | $InternalRegleCollectionRuleDecl | $InternalReglePartialValidationTree;
|
|
117
|
+
/**
|
|
118
|
+
* @public
|
|
101
119
|
* Rule tree for a form property
|
|
102
120
|
*/
|
|
103
121
|
type RegleRuleDecl<TValue extends any = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
|
|
104
122
|
[TKey in keyof TCustomRules]?: TCustomRules[TKey] extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, TParams> : FormRuleDeclaration<TValue, any>;
|
|
105
123
|
};
|
|
124
|
+
/**
|
|
125
|
+
* @internal
|
|
126
|
+
* @reference {@link RegleRuleDecl}
|
|
127
|
+
*/
|
|
128
|
+
type $InternalRegleRuleDecl = Record<string, FormRuleDeclaration<any, any>>;
|
|
129
|
+
/**
|
|
130
|
+
* @public
|
|
131
|
+
*/
|
|
106
132
|
type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
|
|
107
133
|
$each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
|
|
108
134
|
}) | {
|
|
109
135
|
$each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
|
|
110
136
|
};
|
|
111
137
|
/**
|
|
112
|
-
*
|
|
138
|
+
* @internal
|
|
139
|
+
* @reference {@link RegleCollectionRuleDecl}
|
|
140
|
+
*
|
|
113
141
|
*/
|
|
114
|
-
type
|
|
142
|
+
type $InternalRegleCollectionRuleDecl = $InternalRegleRuleDecl & {
|
|
143
|
+
$each?: $InternalFormPropertyTypes;
|
|
144
|
+
};
|
|
115
145
|
/**
|
|
146
|
+
* @public
|
|
147
|
+
*/
|
|
148
|
+
type InlineRuleDeclaration<TValue extends any = any> = (value: Maybe<TValue>, ...args: any[]) => boolean | Promise<boolean>;
|
|
149
|
+
/**
|
|
150
|
+
* @public
|
|
116
151
|
* Regroup inline and registered rules
|
|
117
152
|
* */
|
|
118
153
|
type FormRuleDeclaration<TValue extends any, TParams extends any[] = []> = InlineRuleDeclaration<TValue> | RegleRuleDefinition<TValue, TParams>;
|
|
@@ -120,7 +155,7 @@ type FormRuleDeclaration<TValue extends any, TParams extends any[] = []> = Inlin
|
|
|
120
155
|
type RegleErrorTree<TRules extends ReglePartialValidationTree<any, any>> = {
|
|
121
156
|
readonly [K in keyof TRules]: RegleValidationErrors<TRules[K]>;
|
|
122
157
|
};
|
|
123
|
-
type RegleValidationErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends
|
|
158
|
+
type RegleValidationErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleCollectionRuleDefinition ? RegleCollectionErrors<TRule['$each']> : TRule extends ReglePartialValidationTree<any, any> ? RegleErrorTree<TRule> : TRule extends RegleRuleDecl<any, any> ? string[] : string[];
|
|
124
159
|
type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = {
|
|
125
160
|
readonly $errors: string[];
|
|
126
161
|
readonly $each: RegleValidationErrors<TRule>[];
|
|
@@ -128,54 +163,118 @@ type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undef
|
|
|
128
163
|
type PossibleRegleErrors = RegleCollectionErrors<any> | string[] | RegleErrorTree<any>;
|
|
129
164
|
type DataType = string | number | Record<string, any> | File | Array<any> | Date | null | undefined;
|
|
130
165
|
|
|
131
|
-
|
|
166
|
+
/**
|
|
167
|
+
* @public
|
|
168
|
+
*/
|
|
169
|
+
interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus {
|
|
132
170
|
readonly $fields: {
|
|
133
171
|
readonly [TKey in keyof TRules]: InferRegleStatusType<NonNullable<TRules[TKey]>, TState, TKey>;
|
|
134
172
|
};
|
|
135
173
|
}
|
|
136
|
-
|
|
137
|
-
|
|
174
|
+
/**
|
|
175
|
+
* @internal
|
|
176
|
+
* @reference {@link RegleStatus}
|
|
177
|
+
*/
|
|
178
|
+
interface $InternalRegleStatus extends RegleCommonStatus {
|
|
179
|
+
$fields: {
|
|
180
|
+
[x: string]: $InternalRegleStatusType;
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* @public
|
|
185
|
+
*/
|
|
186
|
+
type InferRegleStatusType<TRule extends RegleCollectionRuleDecl | RegleRuleDecl | ReglePartialValidationTree<any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> = TRule extends RegleCollectionRuleDefinition<any, any> ? RegleCollectionStatus<TRule['$each'], TState[TKey]> : TRule extends ReglePartialValidationTree<any> ? TState[TKey] extends Array<any> ? RegleCommonStatus : RegleStatus<TState[TKey], TRule> : RegleFieldStatus<TRule, TState, TKey>;
|
|
187
|
+
/**
|
|
188
|
+
* @internal
|
|
189
|
+
* @reference {@link InferRegleStatusType}
|
|
190
|
+
*/
|
|
191
|
+
type $InternalRegleStatusType = $InternalRegleCollectionStatus | RegleCommonStatus | $InternalRegleStatus | $InternalRegleFieldStatus;
|
|
192
|
+
/**
|
|
193
|
+
* @public
|
|
194
|
+
*/
|
|
195
|
+
interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, AllRulesDeclarations> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus {
|
|
196
|
+
$value: TState[TKey];
|
|
138
197
|
readonly $rules: {
|
|
139
198
|
readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
|
|
140
199
|
};
|
|
141
200
|
}
|
|
142
|
-
|
|
201
|
+
/**
|
|
202
|
+
* @internal
|
|
203
|
+
* @reference {@link RegleFieldStatus}
|
|
204
|
+
*/
|
|
205
|
+
interface $InternalRegleFieldStatus extends RegleCommonStatus {
|
|
206
|
+
$value: any;
|
|
207
|
+
$rules: Record<string, $InternalRegleRuleStatus>;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* @public
|
|
211
|
+
*/
|
|
212
|
+
interface RegleCommonStatus {
|
|
143
213
|
readonly $valid: boolean;
|
|
144
214
|
readonly $invalid: boolean;
|
|
145
215
|
readonly $dirty: boolean;
|
|
146
216
|
readonly $anyDirty: boolean;
|
|
147
217
|
readonly $pending: boolean;
|
|
148
|
-
$value: TValue;
|
|
149
218
|
readonly $error: boolean;
|
|
150
|
-
$touch
|
|
151
|
-
$reset
|
|
219
|
+
$touch(): void;
|
|
220
|
+
$reset(): void;
|
|
221
|
+
$validate(): Promise<boolean>;
|
|
222
|
+
$unwatch(): void;
|
|
223
|
+
$watch(): void;
|
|
152
224
|
}
|
|
153
|
-
|
|
225
|
+
/**
|
|
226
|
+
* @public
|
|
227
|
+
*/
|
|
228
|
+
type RegleRuleStatus<TValue = any, TParams extends any[] = any[]> = {
|
|
154
229
|
readonly $type: string;
|
|
155
230
|
readonly $message: string;
|
|
156
|
-
readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
|
|
157
231
|
readonly $active: boolean;
|
|
158
232
|
readonly $valid: boolean;
|
|
159
233
|
readonly $pending: boolean;
|
|
160
|
-
readonly $
|
|
161
|
-
|
|
162
|
-
|
|
234
|
+
readonly $path: string;
|
|
235
|
+
$validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
|
|
236
|
+
$validate(): Promise<boolean>;
|
|
237
|
+
} & ([TParams] extends [[]] ? {} : {
|
|
238
|
+
readonly $params: TParams;
|
|
239
|
+
});
|
|
240
|
+
/**
|
|
241
|
+
* @internal
|
|
242
|
+
* @reference {@link RegleRuleStatus}
|
|
243
|
+
*/
|
|
244
|
+
interface $InternalRegleRuleStatus {
|
|
163
245
|
readonly $type: string;
|
|
164
246
|
readonly $message: string;
|
|
165
|
-
readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
|
|
166
247
|
readonly $active: boolean;
|
|
167
248
|
readonly $valid: boolean;
|
|
168
249
|
readonly $pending: boolean;
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
250
|
+
readonly $path: string;
|
|
251
|
+
$validator(value: any, ...args: any[]): boolean | Promise<boolean>;
|
|
252
|
+
$validate(): Promise<boolean>;
|
|
253
|
+
$unwatch(): void;
|
|
254
|
+
$watch(): void;
|
|
255
|
+
readonly $params?: any[];
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* @public
|
|
259
|
+
*/
|
|
172
260
|
interface RegleCollectionStatus<TRules extends RegleRuleDecl | ReglePartialValidationTree<any>, TState extends any[]> extends RegleFieldStatus<TRules, TState> {
|
|
173
261
|
readonly $each: Array<InferRegleStatusType<NonNullable<TRules>, TState, number>>;
|
|
174
262
|
}
|
|
175
|
-
|
|
176
|
-
|
|
263
|
+
/**
|
|
264
|
+
* @internal
|
|
265
|
+
* @reference {@link RegleCollectionStatus}
|
|
266
|
+
*/
|
|
267
|
+
interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fields'> {
|
|
268
|
+
$each: Array<$InternalRegleStatusType>;
|
|
269
|
+
$rules?: Record<string, $InternalRegleRuleStatus>;
|
|
270
|
+
$unwatch(): void;
|
|
271
|
+
$watch(): void;
|
|
272
|
+
$fields?: {
|
|
273
|
+
[x: string]: $InternalRegleStatusType;
|
|
274
|
+
};
|
|
275
|
+
}
|
|
177
276
|
|
|
178
|
-
declare function createRule<TValue extends any, TParams extends any[] = []>(definition: RegleRuleInit<TValue, TParams>): InferRegleRule<TValue, TParams>;
|
|
277
|
+
declare function createRule<TValue extends any, TParams extends any[] = [], TType extends string = string>(definition: RegleRuleInit<TValue, TParams, TType>): InferRegleRule<TValue, TParams>;
|
|
179
278
|
|
|
180
279
|
/**
|
|
181
280
|
* Root function that allows you to define project-wise all your custom validators or overwrite default ones
|
|
@@ -185,29 +284,13 @@ declare function createRule<TValue extends any, TParams extends any[] = []>(defi
|
|
|
185
284
|
* @param customRules
|
|
186
285
|
*/
|
|
187
286
|
declare function defineCustomValidators<TCustomRules extends Partial<AllRulesDeclarations>>(customRules: () => TCustomRules): {
|
|
188
|
-
useRegle: <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree & {
|
|
189
|
-
maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
|
|
190
|
-
required: RegleRuleDefinition<unknown, []>;
|
|
191
|
-
requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
|
|
192
|
-
} & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules)) => {
|
|
287
|
+
useRegle: <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree & DefaultValidators & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules)) => {
|
|
193
288
|
state: vue.Ref<TState>;
|
|
194
|
-
$regle:
|
|
289
|
+
$regle: RegleStatus<TState, TRules>;
|
|
195
290
|
errors: RegleErrorTree<TRules>;
|
|
291
|
+
resetForm: () => void;
|
|
292
|
+
validateForm: () => Promise<boolean>;
|
|
196
293
|
};
|
|
197
294
|
};
|
|
198
295
|
|
|
199
|
-
|
|
200
|
-
declare function withMessage<TValue extends any, TParams extends any[]>(rule: RegleRuleWithParamsDefinition<TValue, TParams>, newMessage: string | ((value: TValue, ...args: TParams) => string)): RegleRuleWithParamsDefinition<TValue, TParams>;
|
|
201
|
-
declare function withMessage<TValue extends any, TParams extends any[]>(rule: RegleRuleDefinition<TValue, TParams>, newMessage: string | ((value: TValue, ...args: TParams) => string)): RegleRuleDefinition<TValue, TParams>;
|
|
202
|
-
|
|
203
|
-
declare function withAsync<TParams extends (Ref<unknown> | (() => unknown))[]>(rule: InlineRuleDeclaration<any>, depsArray: [...TParams]): RegleRuleDefinition<any>;
|
|
204
|
-
|
|
205
|
-
declare function isEmpty(value: unknown): value is null | undefined;
|
|
206
|
-
|
|
207
|
-
declare const required: RegleRuleDefinition<unknown, []>;
|
|
208
|
-
|
|
209
|
-
declare const maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
|
|
210
|
-
|
|
211
|
-
declare const requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
|
|
212
|
-
|
|
213
|
-
export { AllRulesDeclarations, CustomRulesDeclarationTree, DataType, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, ParamDecl, PossibleRegleErrors, PossibleRegleFieldStatus, PossibleRegleStatus, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleSoftRuleStatus, RegleStatus, RegleUniversalParams, RegleValidationErrors, UnwrapRegleUniversalParams, createRule, defineCustomValidators, isEmpty, maxLength, required, requiredIf, withAsync, withMessage };
|
|
296
|
+
export { $InternalFormPropertyTypes, $InternalRegleCollectionRuleDecl, $InternalRegleCollectionStatus, $InternalRegleFieldStatus, $InternalReglePartialValidationTree, $InternalRegleRuleDecl, $InternalRegleRuleStatus, $InternalRegleStatus, $InternalRegleStatusType, AllRulesDeclarations, CustomRulesDeclarationTree, DataType, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, ParamDecl, PossibleRegleErrors, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleStatus, RegleUniversalParams, RegleValidationErrors, UnwrapRegleUniversalParams, createRule, defineCustomValidators };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as vue from 'vue';
|
|
2
|
-
import { MaybeRef
|
|
2
|
+
import { MaybeRef } from 'vue';
|
|
3
3
|
|
|
4
4
|
type ArrayElement<T> = T extends Array<infer U> ? U : never;
|
|
5
5
|
|
|
@@ -9,19 +9,18 @@ type Maybe<T> = T | null | undefined;
|
|
|
9
9
|
* @argument
|
|
10
10
|
* createRule arguments options
|
|
11
11
|
*/
|
|
12
|
-
interface RegleRuleInit<TValue extends any, TParams extends any[] = []> {
|
|
12
|
+
interface RegleRuleInit<TValue extends any, TParams extends any[] = [], TType extends string = string> {
|
|
13
13
|
validator: (value: Maybe<TValue>, ...args: TParams) => boolean | Promise<boolean>;
|
|
14
14
|
message: string | ((value: Maybe<TValue>, ...args: TParams) => string);
|
|
15
15
|
active?: boolean | ((value: Maybe<TValue>, ...args: TParams) => boolean);
|
|
16
|
-
type:
|
|
16
|
+
type: TType;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
type DefaultValidators = {
|
|
20
20
|
maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
|
|
21
21
|
required: RegleRuleDefinition<unknown, []>;
|
|
22
22
|
requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
|
|
23
23
|
};
|
|
24
|
-
type DefaultValidators = typeof defaultValidators;
|
|
25
24
|
|
|
26
25
|
type ParamDecl<T = any> = MaybeRef<T> | (() => T);
|
|
27
26
|
type CreateFn<T extends any[]> = (...args: T) => any;
|
|
@@ -93,26 +92,62 @@ type RegleCollectionRuleDefinition<TValue = any[], TCustomRules extends AllRules
|
|
|
93
92
|
type CustomRulesDeclarationTree = Record<string, RegleRuleRaw<any, any>>;
|
|
94
93
|
type AllRulesDeclarations = CustomRulesDeclarationTree & DefaultValidators;
|
|
95
94
|
|
|
95
|
+
/**
|
|
96
|
+
* @public
|
|
97
|
+
*/
|
|
96
98
|
type ReglePartialValidationTree<TForm extends Record<string, any>, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
|
|
97
99
|
[TKey in keyof TForm]?: RegleFormPropertyType<TForm[TKey], TCustomRules>;
|
|
98
100
|
};
|
|
101
|
+
/**
|
|
102
|
+
* @internal
|
|
103
|
+
* @reference {@link ReglePartialValidationTree}
|
|
104
|
+
*/
|
|
105
|
+
type $InternalReglePartialValidationTree = {
|
|
106
|
+
[x: string]: $InternalFormPropertyTypes;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
99
111
|
type RegleFormPropertyType<TValue = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = [NonNullable<TValue>] extends [never] ? RegleRuleDecl<unknown, TCustomRules> : NonNullable<TValue> extends Array<any> ? RegleCollectionRuleDecl<TValue, TCustomRules> : NonNullable<TValue> extends Date ? RegleRuleDecl<NonNullable<TValue>, TCustomRules> : NonNullable<TValue> extends File ? RegleRuleDecl<NonNullable<TValue>, TCustomRules> : NonNullable<TValue> extends Record<string, any> ? ReglePartialValidationTree<NonNullable<TValue>, TCustomRules> : RegleRuleDecl<NonNullable<TValue>, TCustomRules>;
|
|
100
112
|
/**
|
|
113
|
+
* @internal
|
|
114
|
+
* @reference {@link RegleFormPropertyType}
|
|
115
|
+
*/
|
|
116
|
+
type $InternalFormPropertyTypes = $InternalRegleRuleDecl | $InternalRegleCollectionRuleDecl | $InternalReglePartialValidationTree;
|
|
117
|
+
/**
|
|
118
|
+
* @public
|
|
101
119
|
* Rule tree for a form property
|
|
102
120
|
*/
|
|
103
121
|
type RegleRuleDecl<TValue extends any = any, TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = {
|
|
104
122
|
[TKey in keyof TCustomRules]?: TCustomRules[TKey] extends RegleRuleWithParamsDefinition<any, infer TParams> ? RegleRuleDefinition<TValue, TParams> : FormRuleDeclaration<TValue, any>;
|
|
105
123
|
};
|
|
124
|
+
/**
|
|
125
|
+
* @internal
|
|
126
|
+
* @reference {@link RegleRuleDecl}
|
|
127
|
+
*/
|
|
128
|
+
type $InternalRegleRuleDecl = Record<string, FormRuleDeclaration<any, any>>;
|
|
129
|
+
/**
|
|
130
|
+
* @public
|
|
131
|
+
*/
|
|
106
132
|
type RegleCollectionRuleDecl<TValue = any[], TCustomRules extends AllRulesDeclarations = AllRulesDeclarations> = (RegleRuleDecl<NonNullable<TValue>, TCustomRules> & {
|
|
107
133
|
$each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
|
|
108
134
|
}) | {
|
|
109
135
|
$each?: RegleFormPropertyType<ArrayElement<NonNullable<TValue>>, TCustomRules>;
|
|
110
136
|
};
|
|
111
137
|
/**
|
|
112
|
-
*
|
|
138
|
+
* @internal
|
|
139
|
+
* @reference {@link RegleCollectionRuleDecl}
|
|
140
|
+
*
|
|
113
141
|
*/
|
|
114
|
-
type
|
|
142
|
+
type $InternalRegleCollectionRuleDecl = $InternalRegleRuleDecl & {
|
|
143
|
+
$each?: $InternalFormPropertyTypes;
|
|
144
|
+
};
|
|
115
145
|
/**
|
|
146
|
+
* @public
|
|
147
|
+
*/
|
|
148
|
+
type InlineRuleDeclaration<TValue extends any = any> = (value: Maybe<TValue>, ...args: any[]) => boolean | Promise<boolean>;
|
|
149
|
+
/**
|
|
150
|
+
* @public
|
|
116
151
|
* Regroup inline and registered rules
|
|
117
152
|
* */
|
|
118
153
|
type FormRuleDeclaration<TValue extends any, TParams extends any[] = []> = InlineRuleDeclaration<TValue> | RegleRuleDefinition<TValue, TParams>;
|
|
@@ -120,7 +155,7 @@ type FormRuleDeclaration<TValue extends any, TParams extends any[] = []> = Inlin
|
|
|
120
155
|
type RegleErrorTree<TRules extends ReglePartialValidationTree<any, any>> = {
|
|
121
156
|
readonly [K in keyof TRules]: RegleValidationErrors<TRules[K]>;
|
|
122
157
|
};
|
|
123
|
-
type RegleValidationErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends
|
|
158
|
+
type RegleValidationErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleCollectionRuleDefinition ? RegleCollectionErrors<TRule['$each']> : TRule extends ReglePartialValidationTree<any, any> ? RegleErrorTree<TRule> : TRule extends RegleRuleDecl<any, any> ? string[] : string[];
|
|
124
159
|
type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undefined = never> = {
|
|
125
160
|
readonly $errors: string[];
|
|
126
161
|
readonly $each: RegleValidationErrors<TRule>[];
|
|
@@ -128,54 +163,118 @@ type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undef
|
|
|
128
163
|
type PossibleRegleErrors = RegleCollectionErrors<any> | string[] | RegleErrorTree<any>;
|
|
129
164
|
type DataType = string | number | Record<string, any> | File | Array<any> | Date | null | undefined;
|
|
130
165
|
|
|
131
|
-
|
|
166
|
+
/**
|
|
167
|
+
* @public
|
|
168
|
+
*/
|
|
169
|
+
interface RegleStatus<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialValidationTree<TState> = Record<string, any>> extends RegleCommonStatus {
|
|
132
170
|
readonly $fields: {
|
|
133
171
|
readonly [TKey in keyof TRules]: InferRegleStatusType<NonNullable<TRules[TKey]>, TState, TKey>;
|
|
134
172
|
};
|
|
135
173
|
}
|
|
136
|
-
|
|
137
|
-
|
|
174
|
+
/**
|
|
175
|
+
* @internal
|
|
176
|
+
* @reference {@link RegleStatus}
|
|
177
|
+
*/
|
|
178
|
+
interface $InternalRegleStatus extends RegleCommonStatus {
|
|
179
|
+
$fields: {
|
|
180
|
+
[x: string]: $InternalRegleStatusType;
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* @public
|
|
185
|
+
*/
|
|
186
|
+
type InferRegleStatusType<TRule extends RegleCollectionRuleDecl | RegleRuleDecl | ReglePartialValidationTree<any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> = TRule extends RegleCollectionRuleDefinition<any, any> ? RegleCollectionStatus<TRule['$each'], TState[TKey]> : TRule extends ReglePartialValidationTree<any> ? TState[TKey] extends Array<any> ? RegleCommonStatus : RegleStatus<TState[TKey], TRule> : RegleFieldStatus<TRule, TState, TKey>;
|
|
187
|
+
/**
|
|
188
|
+
* @internal
|
|
189
|
+
* @reference {@link InferRegleStatusType}
|
|
190
|
+
*/
|
|
191
|
+
type $InternalRegleStatusType = $InternalRegleCollectionStatus | RegleCommonStatus | $InternalRegleStatus | $InternalRegleFieldStatus;
|
|
192
|
+
/**
|
|
193
|
+
* @public
|
|
194
|
+
*/
|
|
195
|
+
interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, AllRulesDeclarations> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus {
|
|
196
|
+
$value: TState[TKey];
|
|
138
197
|
readonly $rules: {
|
|
139
198
|
readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
|
|
140
199
|
};
|
|
141
200
|
}
|
|
142
|
-
|
|
201
|
+
/**
|
|
202
|
+
* @internal
|
|
203
|
+
* @reference {@link RegleFieldStatus}
|
|
204
|
+
*/
|
|
205
|
+
interface $InternalRegleFieldStatus extends RegleCommonStatus {
|
|
206
|
+
$value: any;
|
|
207
|
+
$rules: Record<string, $InternalRegleRuleStatus>;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* @public
|
|
211
|
+
*/
|
|
212
|
+
interface RegleCommonStatus {
|
|
143
213
|
readonly $valid: boolean;
|
|
144
214
|
readonly $invalid: boolean;
|
|
145
215
|
readonly $dirty: boolean;
|
|
146
216
|
readonly $anyDirty: boolean;
|
|
147
217
|
readonly $pending: boolean;
|
|
148
|
-
$value: TValue;
|
|
149
218
|
readonly $error: boolean;
|
|
150
|
-
$touch
|
|
151
|
-
$reset
|
|
219
|
+
$touch(): void;
|
|
220
|
+
$reset(): void;
|
|
221
|
+
$validate(): Promise<boolean>;
|
|
222
|
+
$unwatch(): void;
|
|
223
|
+
$watch(): void;
|
|
152
224
|
}
|
|
153
|
-
|
|
225
|
+
/**
|
|
226
|
+
* @public
|
|
227
|
+
*/
|
|
228
|
+
type RegleRuleStatus<TValue = any, TParams extends any[] = any[]> = {
|
|
154
229
|
readonly $type: string;
|
|
155
230
|
readonly $message: string;
|
|
156
|
-
readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
|
|
157
231
|
readonly $active: boolean;
|
|
158
232
|
readonly $valid: boolean;
|
|
159
233
|
readonly $pending: boolean;
|
|
160
|
-
readonly $
|
|
161
|
-
|
|
162
|
-
|
|
234
|
+
readonly $path: string;
|
|
235
|
+
$validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
|
|
236
|
+
$validate(): Promise<boolean>;
|
|
237
|
+
} & ([TParams] extends [[]] ? {} : {
|
|
238
|
+
readonly $params: TParams;
|
|
239
|
+
});
|
|
240
|
+
/**
|
|
241
|
+
* @internal
|
|
242
|
+
* @reference {@link RegleRuleStatus}
|
|
243
|
+
*/
|
|
244
|
+
interface $InternalRegleRuleStatus {
|
|
163
245
|
readonly $type: string;
|
|
164
246
|
readonly $message: string;
|
|
165
|
-
readonly $validator: (value: TValue, ...args: TParams) => boolean | Promise<boolean>;
|
|
166
247
|
readonly $active: boolean;
|
|
167
248
|
readonly $valid: boolean;
|
|
168
249
|
readonly $pending: boolean;
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
250
|
+
readonly $path: string;
|
|
251
|
+
$validator(value: any, ...args: any[]): boolean | Promise<boolean>;
|
|
252
|
+
$validate(): Promise<boolean>;
|
|
253
|
+
$unwatch(): void;
|
|
254
|
+
$watch(): void;
|
|
255
|
+
readonly $params?: any[];
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* @public
|
|
259
|
+
*/
|
|
172
260
|
interface RegleCollectionStatus<TRules extends RegleRuleDecl | ReglePartialValidationTree<any>, TState extends any[]> extends RegleFieldStatus<TRules, TState> {
|
|
173
261
|
readonly $each: Array<InferRegleStatusType<NonNullable<TRules>, TState, number>>;
|
|
174
262
|
}
|
|
175
|
-
|
|
176
|
-
|
|
263
|
+
/**
|
|
264
|
+
* @internal
|
|
265
|
+
* @reference {@link RegleCollectionStatus}
|
|
266
|
+
*/
|
|
267
|
+
interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fields'> {
|
|
268
|
+
$each: Array<$InternalRegleStatusType>;
|
|
269
|
+
$rules?: Record<string, $InternalRegleRuleStatus>;
|
|
270
|
+
$unwatch(): void;
|
|
271
|
+
$watch(): void;
|
|
272
|
+
$fields?: {
|
|
273
|
+
[x: string]: $InternalRegleStatusType;
|
|
274
|
+
};
|
|
275
|
+
}
|
|
177
276
|
|
|
178
|
-
declare function createRule<TValue extends any, TParams extends any[] = []>(definition: RegleRuleInit<TValue, TParams>): InferRegleRule<TValue, TParams>;
|
|
277
|
+
declare function createRule<TValue extends any, TParams extends any[] = [], TType extends string = string>(definition: RegleRuleInit<TValue, TParams, TType>): InferRegleRule<TValue, TParams>;
|
|
179
278
|
|
|
180
279
|
/**
|
|
181
280
|
* Root function that allows you to define project-wise all your custom validators or overwrite default ones
|
|
@@ -185,29 +284,13 @@ declare function createRule<TValue extends any, TParams extends any[] = []>(defi
|
|
|
185
284
|
* @param customRules
|
|
186
285
|
*/
|
|
187
286
|
declare function defineCustomValidators<TCustomRules extends Partial<AllRulesDeclarations>>(customRules: () => TCustomRules): {
|
|
188
|
-
useRegle: <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree & {
|
|
189
|
-
maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
|
|
190
|
-
required: RegleRuleDefinition<unknown, []>;
|
|
191
|
-
requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
|
|
192
|
-
} & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules)) => {
|
|
287
|
+
useRegle: <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree & DefaultValidators & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules)) => {
|
|
193
288
|
state: vue.Ref<TState>;
|
|
194
|
-
$regle:
|
|
289
|
+
$regle: RegleStatus<TState, TRules>;
|
|
195
290
|
errors: RegleErrorTree<TRules>;
|
|
291
|
+
resetForm: () => void;
|
|
292
|
+
validateForm: () => Promise<boolean>;
|
|
196
293
|
};
|
|
197
294
|
};
|
|
198
295
|
|
|
199
|
-
|
|
200
|
-
declare function withMessage<TValue extends any, TParams extends any[]>(rule: RegleRuleWithParamsDefinition<TValue, TParams>, newMessage: string | ((value: TValue, ...args: TParams) => string)): RegleRuleWithParamsDefinition<TValue, TParams>;
|
|
201
|
-
declare function withMessage<TValue extends any, TParams extends any[]>(rule: RegleRuleDefinition<TValue, TParams>, newMessage: string | ((value: TValue, ...args: TParams) => string)): RegleRuleDefinition<TValue, TParams>;
|
|
202
|
-
|
|
203
|
-
declare function withAsync<TParams extends (Ref<unknown> | (() => unknown))[]>(rule: InlineRuleDeclaration<any>, depsArray: [...TParams]): RegleRuleDefinition<any>;
|
|
204
|
-
|
|
205
|
-
declare function isEmpty(value: unknown): value is null | undefined;
|
|
206
|
-
|
|
207
|
-
declare const required: RegleRuleDefinition<unknown, []>;
|
|
208
|
-
|
|
209
|
-
declare const maxLength: RegleRuleWithParamsDefinition<string, [count: number]>;
|
|
210
|
-
|
|
211
|
-
declare const requiredIf: RegleRuleWithParamsDefinition<unknown, [condition: boolean]>;
|
|
212
|
-
|
|
213
|
-
export { AllRulesDeclarations, CustomRulesDeclarationTree, DataType, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, ParamDecl, PossibleRegleErrors, PossibleRegleFieldStatus, PossibleRegleStatus, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleSoftRuleStatus, RegleStatus, RegleUniversalParams, RegleValidationErrors, UnwrapRegleUniversalParams, createRule, defineCustomValidators, isEmpty, maxLength, required, requiredIf, withAsync, withMessage };
|
|
296
|
+
export { $InternalFormPropertyTypes, $InternalRegleCollectionRuleDecl, $InternalRegleCollectionStatus, $InternalRegleFieldStatus, $InternalReglePartialValidationTree, $InternalRegleRuleDecl, $InternalRegleRuleStatus, $InternalRegleStatus, $InternalRegleStatusType, AllRulesDeclarations, CustomRulesDeclarationTree, DataType, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, ParamDecl, PossibleRegleErrors, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleStatus, RegleUniversalParams, RegleValidationErrors, UnwrapRegleUniversalParams, createRule, defineCustomValidators };
|