@regle/schemas 0.7.5 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/regle-schemas.cjs +74 -513
- package/dist/regle-schemas.d.cts +26 -94
- package/dist/regle-schemas.d.ts +26 -94
- package/dist/regle-schemas.min.cjs +1 -1
- package/dist/regle-schemas.min.mjs +1 -1
- package/dist/regle-schemas.mjs +75 -491
- package/package.json +17 -7
package/dist/regle-schemas.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RegleShortcutDefinition, RegleCommonStatus, JoinDiscriminatedUnions, RegleErrorTree, RegleRuleStatus, RegleCollectionErrors, MismatchInfo, DeepReactiveState, DeepMaybeRef, RegleBehaviourOptions, LocalRegleBehaviourOptions, NoInferLegacy, PrimitiveTypes } from '@regle/core';
|
|
2
2
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
-
import { Raw, UnwrapNestedRefs, MaybeRef
|
|
3
|
+
import { Raw, UnwrapNestedRefs, MaybeRef } from 'vue';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
|
|
@@ -53,38 +53,6 @@ Unfortunately, `Record<string, never>`, `Record<keyof any, never>` and `Record<n
|
|
|
53
53
|
*/
|
|
54
54
|
type EmptyObject = {[emptyObjectSymbol]?: never};
|
|
55
55
|
|
|
56
|
-
/**
|
|
57
|
-
Returns a boolean for whether the two given types are equal.
|
|
58
|
-
|
|
59
|
-
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
|
|
60
|
-
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
|
|
61
|
-
|
|
62
|
-
Use-cases:
|
|
63
|
-
- If you want to make a conditional branch based on the result of a comparison of two types.
|
|
64
|
-
|
|
65
|
-
@example
|
|
66
|
-
```
|
|
67
|
-
import type {IsEqual} from 'type-fest';
|
|
68
|
-
|
|
69
|
-
// This type returns a boolean for whether the given array includes the given item.
|
|
70
|
-
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
|
|
71
|
-
type Includes<Value extends readonly any[], Item> =
|
|
72
|
-
Value extends readonly [Value[0], ...infer rest]
|
|
73
|
-
? IsEqual<Value[0], Item> extends true
|
|
74
|
-
? true
|
|
75
|
-
: Includes<rest, Item>
|
|
76
|
-
: false;
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
@category Type Guard
|
|
80
|
-
@category Utilities
|
|
81
|
-
*/
|
|
82
|
-
type IsEqual<A, B> =
|
|
83
|
-
(<G>() => G extends A ? 1 : 2) extends
|
|
84
|
-
(<G>() => G extends B ? 1 : 2)
|
|
85
|
-
? true
|
|
86
|
-
: false;
|
|
87
|
-
|
|
88
56
|
/**
|
|
89
57
|
Extract the element of an array that also works for array union.
|
|
90
58
|
|
|
@@ -94,30 +62,6 @@ It creates a type-safe way to access the element type of `unknown` type.
|
|
|
94
62
|
*/
|
|
95
63
|
type ArrayElement<T> = T extends readonly unknown[] ? T[0] : never;
|
|
96
64
|
|
|
97
|
-
/**
|
|
98
|
-
Returns a boolean for whether either of two given types are true.
|
|
99
|
-
|
|
100
|
-
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
|
|
101
|
-
|
|
102
|
-
@example
|
|
103
|
-
```
|
|
104
|
-
import type {Or} from 'type-fest';
|
|
105
|
-
|
|
106
|
-
Or<true, false>;
|
|
107
|
-
//=> true
|
|
108
|
-
|
|
109
|
-
Or<false, false>;
|
|
110
|
-
//=> false
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
@see {@link And}
|
|
114
|
-
*/
|
|
115
|
-
type Or<A extends boolean, B extends boolean> = [A, B][number] extends false
|
|
116
|
-
? false
|
|
117
|
-
: true extends [IsEqual<A, true>, IsEqual<B, true>][number]
|
|
118
|
-
? true
|
|
119
|
-
: never;
|
|
120
|
-
|
|
121
65
|
/**
|
|
122
66
|
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
123
67
|
*/
|
|
@@ -231,19 +175,13 @@ type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOpt
|
|
|
231
175
|
[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType], Options>
|
|
232
176
|
};
|
|
233
177
|
|
|
234
|
-
|
|
235
|
-
type isModeRules<T> = T extends 'rules' ? true : false;
|
|
236
|
-
type RegleSchemaModeOptions<T extends RegleSchemaMode = 'rules'> = {
|
|
237
|
-
mode?: T;
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
interface RegleSchema<TState extends Record<string, any>, TSchema extends Record<string, any>, TMode extends RegleSchemaMode = 'rules', TShortcuts extends RegleShortcutDefinition = {}> {
|
|
178
|
+
interface RegleSchema<TState extends Record<string, any>, TSchema extends Record<string, any>, TShortcuts extends RegleShortcutDefinition = {}> {
|
|
241
179
|
/**
|
|
242
180
|
* r$ is a reactive object containing the values, errors, dirty state and all the necessary validations properties you'll need to display informations.
|
|
243
181
|
*
|
|
244
182
|
* To see the list of properties: {@link https://reglejs.dev/core-concepts/validation-properties}
|
|
245
183
|
*/
|
|
246
|
-
r$: Raw<RegleSchemaStatus<TState, TSchema,
|
|
184
|
+
r$: Raw<RegleSchemaStatus<TState, TSchema, TShortcuts, true>>;
|
|
247
185
|
}
|
|
248
186
|
type RegleSchemaResult<TSchema extends unknown> = {
|
|
249
187
|
result: false;
|
|
@@ -255,12 +193,12 @@ type RegleSchemaResult<TSchema extends unknown> = {
|
|
|
255
193
|
/**
|
|
256
194
|
* @public
|
|
257
195
|
*/
|
|
258
|
-
type RegleSchemaStatus<TState extends Record<string, any> = Record<string, any>, TSchema extends Record<string, any> = Record<string, any>,
|
|
196
|
+
type RegleSchemaStatus<TState extends Record<string, any> = Record<string, any>, TSchema extends Record<string, any> = Record<string, any>, TShortcuts extends RegleShortcutDefinition = {}, IsRoot extends boolean = false> = Omit<RegleCommonStatus<TState>, IsRoot extends false ? '$pending' : ''> & {
|
|
259
197
|
/** Represents all the children of your object. You can access any nested child at any depth to get the relevant data you need for your form. */
|
|
260
198
|
readonly $fields: {
|
|
261
|
-
readonly [TKey in keyof JoinDiscriminatedUnions<TState>]: TKey extends keyof JoinDiscriminatedUnions<TSchema> ? InferRegleSchemaStatusType<NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]>, JoinDiscriminatedUnions<TState>[TKey],
|
|
199
|
+
readonly [TKey in keyof JoinDiscriminatedUnions<TState>]: TKey extends keyof JoinDiscriminatedUnions<TSchema> ? InferRegleSchemaStatusType<NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]>, JoinDiscriminatedUnions<TState>[TKey], TShortcuts> : never;
|
|
262
200
|
} & {
|
|
263
|
-
readonly [TKey in keyof JoinDiscriminatedUnions<TState> as TKey extends keyof JoinDiscriminatedUnions<TSchema> ? JoinDiscriminatedUnions<TSchema>[TKey] extends NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]> ? TKey : never : never]-?: TKey extends keyof JoinDiscriminatedUnions<TSchema> ? InferRegleSchemaStatusType<NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]>, JoinDiscriminatedUnions<TState>[TKey],
|
|
201
|
+
readonly [TKey in keyof JoinDiscriminatedUnions<TState> as TKey extends keyof JoinDiscriminatedUnions<TSchema> ? JoinDiscriminatedUnions<TSchema>[TKey] extends NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]> ? TKey : never : never]-?: TKey extends keyof JoinDiscriminatedUnions<TSchema> ? InferRegleSchemaStatusType<NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]>, JoinDiscriminatedUnions<TState>[TKey], TShortcuts> : never;
|
|
264
202
|
};
|
|
265
203
|
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
266
204
|
*
|
|
@@ -270,7 +208,7 @@ type RegleSchemaStatus<TState extends Record<string, any> = Record<string, any>,
|
|
|
270
208
|
readonly $silentErrors: RegleErrorTree<TState>;
|
|
271
209
|
/** Will return a copy of your state with only the fields that are dirty. By default it will filter out nullish values or objects, but you can override it with the first parameter $extractDirtyFields(false). */
|
|
272
210
|
$extractDirtyFields: (filterNullishValues?: boolean) => PartialDeep<TState>;
|
|
273
|
-
} & (
|
|
211
|
+
} & (IsRoot extends true ? {
|
|
274
212
|
/** Sets all properties as dirty, triggering all rules. It returns a promise that will either resolve to false or a type safe copy of your form state. Values that had the required rule will be transformed into a non-nullable value (type only). */
|
|
275
213
|
$validate: () => Promise<RegleSchemaResult<TSchema>>;
|
|
276
214
|
} : {}) & ([TShortcuts['nested']] extends [never] ? {} : {
|
|
@@ -279,11 +217,11 @@ type RegleSchemaStatus<TState extends Record<string, any> = Record<string, any>,
|
|
|
279
217
|
/**
|
|
280
218
|
* @public
|
|
281
219
|
*/
|
|
282
|
-
type InferRegleSchemaStatusType<TSchema extends unknown, TState extends unknown,
|
|
220
|
+
type InferRegleSchemaStatusType<TSchema extends unknown, TState extends unknown, TShortcuts extends RegleShortcutDefinition = {}> = NonNullable<TSchema> extends Array<infer A extends Record<string, any>> ? RegleSchemaCollectionStatus<A, TState extends Array<any> ? TState : [], TShortcuts> : NonNullable<TState> extends Date | File ? RegleSchemaFieldStatus<TSchema, TState, TShortcuts> : unknown extends TState ? RegleSchemaFieldStatus<TSchema extends EmptyObject ? unknown : TSchema, TState, TShortcuts> : NonNullable<TSchema> extends Record<string, any> ? RegleSchemaStatus<NonNullable<TState> extends Record<string, any> ? NonNullable<TState> : {}, NonNullable<TSchema>, TShortcuts> : RegleSchemaFieldStatus<TSchema, TState, TShortcuts>;
|
|
283
221
|
/**
|
|
284
222
|
* @public
|
|
285
223
|
*/
|
|
286
|
-
type RegleSchemaFieldStatus<TSchema extends unknown, TState = any,
|
|
224
|
+
type RegleSchemaFieldStatus<TSchema extends unknown, TState = any, TShortcuts extends RegleShortcutDefinition = {}> = Omit<RegleCommonStatus<TState>, '$pending'> & {
|
|
287
225
|
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
288
226
|
*
|
|
289
227
|
* Only contains errors from properties where $dirty equals true. */
|
|
@@ -300,20 +238,17 @@ type RegleSchemaFieldStatus<TSchema extends unknown, TState = any, TMode extends
|
|
|
300
238
|
};
|
|
301
239
|
/** Will return a copy of your state with only the fields that are dirty. By default it will filter out nullish values or objects, but you can override it with the first parameter $extractDirtyFields(false). */
|
|
302
240
|
$extractDirtyFields: (filterNullishValues?: boolean) => PartialDeep<TState>;
|
|
303
|
-
} & (
|
|
304
|
-
/** Sets the property as dirty, triggering all rules. It returns a promise that will either resolve to false or a type safe copy of your form state. Values that had the required rule will be transformed into a non-nullable value (type only). */
|
|
305
|
-
$validate: () => Promise<RegleSchemaResult<TSchema>>;
|
|
306
|
-
} : {}) & ([TShortcuts['fields']] extends [never] ? {} : {
|
|
241
|
+
} & ([TShortcuts['fields']] extends [never] ? {} : {
|
|
307
242
|
[K in keyof TShortcuts['fields']]: ReturnType<NonNullable<TShortcuts['fields']>[K]>;
|
|
308
243
|
});
|
|
309
244
|
/**
|
|
310
245
|
* @public
|
|
311
246
|
*/
|
|
312
|
-
type RegleSchemaCollectionStatus<TSchema extends Record<string, any>, TState extends any[],
|
|
247
|
+
type RegleSchemaCollectionStatus<TSchema extends Record<string, any>, TState extends any[], TShortcuts extends RegleShortcutDefinition = {}> = Omit<RegleSchemaFieldStatus<TSchema, TState, TShortcuts>, '$errors' | '$silentErrors' | '$validate'> & {
|
|
313
248
|
/** Collection of status of every item in your collection. Each item will be a field you can access, or map on it to display your elements. */
|
|
314
|
-
readonly $each: Array<InferRegleSchemaStatusType<NonNullable<TSchema>, ArrayElement<TState>,
|
|
249
|
+
readonly $each: Array<InferRegleSchemaStatusType<NonNullable<TSchema>, ArrayElement<TState>, TShortcuts>>;
|
|
315
250
|
/** Represents the status of the collection itself. You can have validation rules on the array like minLength, this field represents the isolated status of the collection. */
|
|
316
|
-
readonly $self: RegleSchemaFieldStatus<TSchema, TState,
|
|
251
|
+
readonly $self: RegleSchemaFieldStatus<TSchema, TState, TShortcuts>;
|
|
317
252
|
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
318
253
|
*
|
|
319
254
|
* Only contains errors from properties where $dirty equals true. */
|
|
@@ -322,20 +257,15 @@ type RegleSchemaCollectionStatus<TSchema extends Record<string, any>, TState ext
|
|
|
322
257
|
readonly $silentErrors: RegleCollectionErrors<TSchema>;
|
|
323
258
|
/** Will return a copy of your state with only the fields that are dirty. By default it will filter out nullish values or objects, but you can override it with the first parameter $extractDirtyFields(false). */
|
|
324
259
|
$extractDirtyFields: (filterNullishValues?: boolean) => PartialDeep<TState>;
|
|
325
|
-
} & (
|
|
326
|
-
/** Sets the property as dirty, triggering all rules. It returns a promise that will either resolve to false or a type safe copy of your form state. Values that had the required rule will be transformed into a non-nullable value (type only). */
|
|
327
|
-
$validate: () => Promise<RegleSchemaResult<TSchema>>;
|
|
328
|
-
} : {}) & ([TShortcuts['collections']] extends [never] ? {} : {
|
|
260
|
+
} & ([TShortcuts['collections']] extends [never] ? {} : {
|
|
329
261
|
[K in keyof TShortcuts['collections']]: ReturnType<NonNullable<TShortcuts['collections']>[K]>;
|
|
330
262
|
});
|
|
331
263
|
|
|
332
|
-
type useRegleSchemaFn<TShortcuts extends RegleShortcutDefinition<any> = never> = <TState extends Record<string, any>, TSchema extends StandardSchemaV1<Record<string, any>> & TValid,
|
|
264
|
+
type useRegleSchemaFn<TShortcuts extends RegleShortcutDefinition<any> = never> = <TState extends Record<string, any>, TSchema extends StandardSchemaV1<Record<string, any>> & TValid, TValid = StandardSchemaV1.InferInput<TSchema> extends PartialDeep<UnwrapNestedRefs<TState>, {
|
|
333
265
|
recurseIntoArrays: true;
|
|
334
266
|
}> ? {} : MismatchInfo<UnwrapNestedRefs<TState>, PartialDeep<StandardSchemaV1.InferInput<TSchema>, {
|
|
335
267
|
recurseIntoArrays: true;
|
|
336
|
-
}>>>(state: MaybeRef<TState> | DeepReactiveState<TState>, schema: MaybeRef<TSchema>, options?: Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<UnwrapNestedRefs<TState>, {}, never>
|
|
337
|
-
TMode
|
|
338
|
-
] extends [never] ? 'rules' : TMode, TShortcuts>;
|
|
268
|
+
}>>>(state: MaybeRef<TState> | DeepReactiveState<TState>, schema: MaybeRef<TSchema>, options?: Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<UnwrapNestedRefs<TState>, {}, never>) => RegleSchema<UnwrapNestedRefs<TState>, StandardSchemaV1.InferInput<TSchema>, TShortcuts>;
|
|
339
269
|
/**
|
|
340
270
|
* useRegle serves as the foundation for validation logic.
|
|
341
271
|
*
|
|
@@ -358,20 +288,22 @@ type useRegleSchemaFn<TShortcuts extends RegleShortcutDefinition<any> = never> =
|
|
|
358
288
|
declare const useRegleSchema: useRegleSchemaFn<RegleShortcutDefinition<any>>;
|
|
359
289
|
|
|
360
290
|
/**
|
|
361
|
-
* ⚠️ Not compatible with `schema` mode
|
|
362
291
|
*
|
|
363
292
|
* Force dependency on any RPC schema
|
|
364
293
|
* ```ts
|
|
365
294
|
* const foo = ref('');
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
* )
|
|
295
|
+
*
|
|
296
|
+
* const schema = computed(() => v.object({
|
|
297
|
+
* name: withDeps(
|
|
298
|
+
* v.pipe(v.string(), v.check((value) => value === foo.value)),
|
|
299
|
+
* [foo.value]
|
|
300
|
+
* )
|
|
301
|
+
* }))
|
|
302
|
+
*
|
|
303
|
+
* useRegleSchema({name: ''}, schema)
|
|
370
304
|
* ```
|
|
371
305
|
*/
|
|
372
|
-
declare function withDeps<TSchema extends StandardSchemaV1, TParams extends
|
|
373
|
-
__depsArray: TParams;
|
|
374
|
-
};
|
|
306
|
+
declare function withDeps<TSchema extends StandardSchemaV1, TParams extends unknown[] = []>(schema: TSchema, depsArray: [...TParams]): TSchema;
|
|
375
307
|
|
|
376
308
|
interface inferValibotSchemaFn {
|
|
377
309
|
<TState extends Record<string, any>, TSchema extends StandardSchemaV1<Record<string, any>> & TValid, TValid = UnwrapNestedRefs<TState> extends PartialDeep<StandardSchemaV1.InferInput<TSchema>, {
|
package/dist/regle-schemas.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RegleShortcutDefinition, RegleCommonStatus, JoinDiscriminatedUnions, RegleErrorTree, RegleRuleStatus, RegleCollectionErrors, MismatchInfo, DeepReactiveState, DeepMaybeRef, RegleBehaviourOptions, LocalRegleBehaviourOptions, NoInferLegacy, PrimitiveTypes } from '@regle/core';
|
|
2
2
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
-
import { Raw, UnwrapNestedRefs, MaybeRef
|
|
3
|
+
import { Raw, UnwrapNestedRefs, MaybeRef } from 'vue';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
|
|
@@ -53,38 +53,6 @@ Unfortunately, `Record<string, never>`, `Record<keyof any, never>` and `Record<n
|
|
|
53
53
|
*/
|
|
54
54
|
type EmptyObject = {[emptyObjectSymbol]?: never};
|
|
55
55
|
|
|
56
|
-
/**
|
|
57
|
-
Returns a boolean for whether the two given types are equal.
|
|
58
|
-
|
|
59
|
-
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
|
|
60
|
-
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
|
|
61
|
-
|
|
62
|
-
Use-cases:
|
|
63
|
-
- If you want to make a conditional branch based on the result of a comparison of two types.
|
|
64
|
-
|
|
65
|
-
@example
|
|
66
|
-
```
|
|
67
|
-
import type {IsEqual} from 'type-fest';
|
|
68
|
-
|
|
69
|
-
// This type returns a boolean for whether the given array includes the given item.
|
|
70
|
-
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
|
|
71
|
-
type Includes<Value extends readonly any[], Item> =
|
|
72
|
-
Value extends readonly [Value[0], ...infer rest]
|
|
73
|
-
? IsEqual<Value[0], Item> extends true
|
|
74
|
-
? true
|
|
75
|
-
: Includes<rest, Item>
|
|
76
|
-
: false;
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
@category Type Guard
|
|
80
|
-
@category Utilities
|
|
81
|
-
*/
|
|
82
|
-
type IsEqual<A, B> =
|
|
83
|
-
(<G>() => G extends A ? 1 : 2) extends
|
|
84
|
-
(<G>() => G extends B ? 1 : 2)
|
|
85
|
-
? true
|
|
86
|
-
: false;
|
|
87
|
-
|
|
88
56
|
/**
|
|
89
57
|
Extract the element of an array that also works for array union.
|
|
90
58
|
|
|
@@ -94,30 +62,6 @@ It creates a type-safe way to access the element type of `unknown` type.
|
|
|
94
62
|
*/
|
|
95
63
|
type ArrayElement<T> = T extends readonly unknown[] ? T[0] : never;
|
|
96
64
|
|
|
97
|
-
/**
|
|
98
|
-
Returns a boolean for whether either of two given types are true.
|
|
99
|
-
|
|
100
|
-
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
|
|
101
|
-
|
|
102
|
-
@example
|
|
103
|
-
```
|
|
104
|
-
import type {Or} from 'type-fest';
|
|
105
|
-
|
|
106
|
-
Or<true, false>;
|
|
107
|
-
//=> true
|
|
108
|
-
|
|
109
|
-
Or<false, false>;
|
|
110
|
-
//=> false
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
@see {@link And}
|
|
114
|
-
*/
|
|
115
|
-
type Or<A extends boolean, B extends boolean> = [A, B][number] extends false
|
|
116
|
-
? false
|
|
117
|
-
: true extends [IsEqual<A, true>, IsEqual<B, true>][number]
|
|
118
|
-
? true
|
|
119
|
-
: never;
|
|
120
|
-
|
|
121
65
|
/**
|
|
122
66
|
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
123
67
|
*/
|
|
@@ -231,19 +175,13 @@ type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOpt
|
|
|
231
175
|
[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType], Options>
|
|
232
176
|
};
|
|
233
177
|
|
|
234
|
-
|
|
235
|
-
type isModeRules<T> = T extends 'rules' ? true : false;
|
|
236
|
-
type RegleSchemaModeOptions<T extends RegleSchemaMode = 'rules'> = {
|
|
237
|
-
mode?: T;
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
interface RegleSchema<TState extends Record<string, any>, TSchema extends Record<string, any>, TMode extends RegleSchemaMode = 'rules', TShortcuts extends RegleShortcutDefinition = {}> {
|
|
178
|
+
interface RegleSchema<TState extends Record<string, any>, TSchema extends Record<string, any>, TShortcuts extends RegleShortcutDefinition = {}> {
|
|
241
179
|
/**
|
|
242
180
|
* r$ is a reactive object containing the values, errors, dirty state and all the necessary validations properties you'll need to display informations.
|
|
243
181
|
*
|
|
244
182
|
* To see the list of properties: {@link https://reglejs.dev/core-concepts/validation-properties}
|
|
245
183
|
*/
|
|
246
|
-
r$: Raw<RegleSchemaStatus<TState, TSchema,
|
|
184
|
+
r$: Raw<RegleSchemaStatus<TState, TSchema, TShortcuts, true>>;
|
|
247
185
|
}
|
|
248
186
|
type RegleSchemaResult<TSchema extends unknown> = {
|
|
249
187
|
result: false;
|
|
@@ -255,12 +193,12 @@ type RegleSchemaResult<TSchema extends unknown> = {
|
|
|
255
193
|
/**
|
|
256
194
|
* @public
|
|
257
195
|
*/
|
|
258
|
-
type RegleSchemaStatus<TState extends Record<string, any> = Record<string, any>, TSchema extends Record<string, any> = Record<string, any>,
|
|
196
|
+
type RegleSchemaStatus<TState extends Record<string, any> = Record<string, any>, TSchema extends Record<string, any> = Record<string, any>, TShortcuts extends RegleShortcutDefinition = {}, IsRoot extends boolean = false> = Omit<RegleCommonStatus<TState>, IsRoot extends false ? '$pending' : ''> & {
|
|
259
197
|
/** Represents all the children of your object. You can access any nested child at any depth to get the relevant data you need for your form. */
|
|
260
198
|
readonly $fields: {
|
|
261
|
-
readonly [TKey in keyof JoinDiscriminatedUnions<TState>]: TKey extends keyof JoinDiscriminatedUnions<TSchema> ? InferRegleSchemaStatusType<NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]>, JoinDiscriminatedUnions<TState>[TKey],
|
|
199
|
+
readonly [TKey in keyof JoinDiscriminatedUnions<TState>]: TKey extends keyof JoinDiscriminatedUnions<TSchema> ? InferRegleSchemaStatusType<NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]>, JoinDiscriminatedUnions<TState>[TKey], TShortcuts> : never;
|
|
262
200
|
} & {
|
|
263
|
-
readonly [TKey in keyof JoinDiscriminatedUnions<TState> as TKey extends keyof JoinDiscriminatedUnions<TSchema> ? JoinDiscriminatedUnions<TSchema>[TKey] extends NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]> ? TKey : never : never]-?: TKey extends keyof JoinDiscriminatedUnions<TSchema> ? InferRegleSchemaStatusType<NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]>, JoinDiscriminatedUnions<TState>[TKey],
|
|
201
|
+
readonly [TKey in keyof JoinDiscriminatedUnions<TState> as TKey extends keyof JoinDiscriminatedUnions<TSchema> ? JoinDiscriminatedUnions<TSchema>[TKey] extends NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]> ? TKey : never : never]-?: TKey extends keyof JoinDiscriminatedUnions<TSchema> ? InferRegleSchemaStatusType<NonNullable<JoinDiscriminatedUnions<TSchema>[TKey]>, JoinDiscriminatedUnions<TState>[TKey], TShortcuts> : never;
|
|
264
202
|
};
|
|
265
203
|
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
266
204
|
*
|
|
@@ -270,7 +208,7 @@ type RegleSchemaStatus<TState extends Record<string, any> = Record<string, any>,
|
|
|
270
208
|
readonly $silentErrors: RegleErrorTree<TState>;
|
|
271
209
|
/** Will return a copy of your state with only the fields that are dirty. By default it will filter out nullish values or objects, but you can override it with the first parameter $extractDirtyFields(false). */
|
|
272
210
|
$extractDirtyFields: (filterNullishValues?: boolean) => PartialDeep<TState>;
|
|
273
|
-
} & (
|
|
211
|
+
} & (IsRoot extends true ? {
|
|
274
212
|
/** Sets all properties as dirty, triggering all rules. It returns a promise that will either resolve to false or a type safe copy of your form state. Values that had the required rule will be transformed into a non-nullable value (type only). */
|
|
275
213
|
$validate: () => Promise<RegleSchemaResult<TSchema>>;
|
|
276
214
|
} : {}) & ([TShortcuts['nested']] extends [never] ? {} : {
|
|
@@ -279,11 +217,11 @@ type RegleSchemaStatus<TState extends Record<string, any> = Record<string, any>,
|
|
|
279
217
|
/**
|
|
280
218
|
* @public
|
|
281
219
|
*/
|
|
282
|
-
type InferRegleSchemaStatusType<TSchema extends unknown, TState extends unknown,
|
|
220
|
+
type InferRegleSchemaStatusType<TSchema extends unknown, TState extends unknown, TShortcuts extends RegleShortcutDefinition = {}> = NonNullable<TSchema> extends Array<infer A extends Record<string, any>> ? RegleSchemaCollectionStatus<A, TState extends Array<any> ? TState : [], TShortcuts> : NonNullable<TState> extends Date | File ? RegleSchemaFieldStatus<TSchema, TState, TShortcuts> : unknown extends TState ? RegleSchemaFieldStatus<TSchema extends EmptyObject ? unknown : TSchema, TState, TShortcuts> : NonNullable<TSchema> extends Record<string, any> ? RegleSchemaStatus<NonNullable<TState> extends Record<string, any> ? NonNullable<TState> : {}, NonNullable<TSchema>, TShortcuts> : RegleSchemaFieldStatus<TSchema, TState, TShortcuts>;
|
|
283
221
|
/**
|
|
284
222
|
* @public
|
|
285
223
|
*/
|
|
286
|
-
type RegleSchemaFieldStatus<TSchema extends unknown, TState = any,
|
|
224
|
+
type RegleSchemaFieldStatus<TSchema extends unknown, TState = any, TShortcuts extends RegleShortcutDefinition = {}> = Omit<RegleCommonStatus<TState>, '$pending'> & {
|
|
287
225
|
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
288
226
|
*
|
|
289
227
|
* Only contains errors from properties where $dirty equals true. */
|
|
@@ -300,20 +238,17 @@ type RegleSchemaFieldStatus<TSchema extends unknown, TState = any, TMode extends
|
|
|
300
238
|
};
|
|
301
239
|
/** Will return a copy of your state with only the fields that are dirty. By default it will filter out nullish values or objects, but you can override it with the first parameter $extractDirtyFields(false). */
|
|
302
240
|
$extractDirtyFields: (filterNullishValues?: boolean) => PartialDeep<TState>;
|
|
303
|
-
} & (
|
|
304
|
-
/** Sets the property as dirty, triggering all rules. It returns a promise that will either resolve to false or a type safe copy of your form state. Values that had the required rule will be transformed into a non-nullable value (type only). */
|
|
305
|
-
$validate: () => Promise<RegleSchemaResult<TSchema>>;
|
|
306
|
-
} : {}) & ([TShortcuts['fields']] extends [never] ? {} : {
|
|
241
|
+
} & ([TShortcuts['fields']] extends [never] ? {} : {
|
|
307
242
|
[K in keyof TShortcuts['fields']]: ReturnType<NonNullable<TShortcuts['fields']>[K]>;
|
|
308
243
|
});
|
|
309
244
|
/**
|
|
310
245
|
* @public
|
|
311
246
|
*/
|
|
312
|
-
type RegleSchemaCollectionStatus<TSchema extends Record<string, any>, TState extends any[],
|
|
247
|
+
type RegleSchemaCollectionStatus<TSchema extends Record<string, any>, TState extends any[], TShortcuts extends RegleShortcutDefinition = {}> = Omit<RegleSchemaFieldStatus<TSchema, TState, TShortcuts>, '$errors' | '$silentErrors' | '$validate'> & {
|
|
313
248
|
/** Collection of status of every item in your collection. Each item will be a field you can access, or map on it to display your elements. */
|
|
314
|
-
readonly $each: Array<InferRegleSchemaStatusType<NonNullable<TSchema>, ArrayElement<TState>,
|
|
249
|
+
readonly $each: Array<InferRegleSchemaStatusType<NonNullable<TSchema>, ArrayElement<TState>, TShortcuts>>;
|
|
315
250
|
/** Represents the status of the collection itself. You can have validation rules on the array like minLength, this field represents the isolated status of the collection. */
|
|
316
|
-
readonly $self: RegleSchemaFieldStatus<TSchema, TState,
|
|
251
|
+
readonly $self: RegleSchemaFieldStatus<TSchema, TState, TShortcuts>;
|
|
317
252
|
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
318
253
|
*
|
|
319
254
|
* Only contains errors from properties where $dirty equals true. */
|
|
@@ -322,20 +257,15 @@ type RegleSchemaCollectionStatus<TSchema extends Record<string, any>, TState ext
|
|
|
322
257
|
readonly $silentErrors: RegleCollectionErrors<TSchema>;
|
|
323
258
|
/** Will return a copy of your state with only the fields that are dirty. By default it will filter out nullish values or objects, but you can override it with the first parameter $extractDirtyFields(false). */
|
|
324
259
|
$extractDirtyFields: (filterNullishValues?: boolean) => PartialDeep<TState>;
|
|
325
|
-
} & (
|
|
326
|
-
/** Sets the property as dirty, triggering all rules. It returns a promise that will either resolve to false or a type safe copy of your form state. Values that had the required rule will be transformed into a non-nullable value (type only). */
|
|
327
|
-
$validate: () => Promise<RegleSchemaResult<TSchema>>;
|
|
328
|
-
} : {}) & ([TShortcuts['collections']] extends [never] ? {} : {
|
|
260
|
+
} & ([TShortcuts['collections']] extends [never] ? {} : {
|
|
329
261
|
[K in keyof TShortcuts['collections']]: ReturnType<NonNullable<TShortcuts['collections']>[K]>;
|
|
330
262
|
});
|
|
331
263
|
|
|
332
|
-
type useRegleSchemaFn<TShortcuts extends RegleShortcutDefinition<any> = never> = <TState extends Record<string, any>, TSchema extends StandardSchemaV1<Record<string, any>> & TValid,
|
|
264
|
+
type useRegleSchemaFn<TShortcuts extends RegleShortcutDefinition<any> = never> = <TState extends Record<string, any>, TSchema extends StandardSchemaV1<Record<string, any>> & TValid, TValid = StandardSchemaV1.InferInput<TSchema> extends PartialDeep<UnwrapNestedRefs<TState>, {
|
|
333
265
|
recurseIntoArrays: true;
|
|
334
266
|
}> ? {} : MismatchInfo<UnwrapNestedRefs<TState>, PartialDeep<StandardSchemaV1.InferInput<TSchema>, {
|
|
335
267
|
recurseIntoArrays: true;
|
|
336
|
-
}>>>(state: MaybeRef<TState> | DeepReactiveState<TState>, schema: MaybeRef<TSchema>, options?: Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<UnwrapNestedRefs<TState>, {}, never>
|
|
337
|
-
TMode
|
|
338
|
-
] extends [never] ? 'rules' : TMode, TShortcuts>;
|
|
268
|
+
}>>>(state: MaybeRef<TState> | DeepReactiveState<TState>, schema: MaybeRef<TSchema>, options?: Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<UnwrapNestedRefs<TState>, {}, never>) => RegleSchema<UnwrapNestedRefs<TState>, StandardSchemaV1.InferInput<TSchema>, TShortcuts>;
|
|
339
269
|
/**
|
|
340
270
|
* useRegle serves as the foundation for validation logic.
|
|
341
271
|
*
|
|
@@ -358,20 +288,22 @@ type useRegleSchemaFn<TShortcuts extends RegleShortcutDefinition<any> = never> =
|
|
|
358
288
|
declare const useRegleSchema: useRegleSchemaFn<RegleShortcutDefinition<any>>;
|
|
359
289
|
|
|
360
290
|
/**
|
|
361
|
-
* ⚠️ Not compatible with `schema` mode
|
|
362
291
|
*
|
|
363
292
|
* Force dependency on any RPC schema
|
|
364
293
|
* ```ts
|
|
365
294
|
* const foo = ref('');
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
* )
|
|
295
|
+
*
|
|
296
|
+
* const schema = computed(() => v.object({
|
|
297
|
+
* name: withDeps(
|
|
298
|
+
* v.pipe(v.string(), v.check((value) => value === foo.value)),
|
|
299
|
+
* [foo.value]
|
|
300
|
+
* )
|
|
301
|
+
* }))
|
|
302
|
+
*
|
|
303
|
+
* useRegleSchema({name: ''}, schema)
|
|
370
304
|
* ```
|
|
371
305
|
*/
|
|
372
|
-
declare function withDeps<TSchema extends StandardSchemaV1, TParams extends
|
|
373
|
-
__depsArray: TParams;
|
|
374
|
-
};
|
|
306
|
+
declare function withDeps<TSchema extends StandardSchemaV1, TParams extends unknown[] = []>(schema: TSchema, depsArray: [...TParams]): TSchema;
|
|
375
307
|
|
|
376
308
|
interface inferValibotSchemaFn {
|
|
377
309
|
<TState extends Record<string, any>, TSchema extends StandardSchemaV1<Record<string, any>> & TValid, TValid = UnwrapNestedRefs<TState> extends PartialDeep<StandardSchemaV1.InferInput<TSchema>, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var core=require('@regle/core'),vue=require('vue')
|
|
1
|
+
'use strict';var core=require('@regle/core'),vue=require('vue');function F(e){if(typeof e.source.flags=="string")return e.source.flags;{let t=[];return e.global&&t.push("g"),e.ignoreCase&&t.push("i"),e.multiline&&t.push("m"),e.sticky&&t.push("y"),e.unicode&&t.push("u"),t.join("")}}function l(e){let t=e,r={}.toString.call(e).slice(8,-1);if(r=="Set"&&(t=new Set([...e].map(a=>l(a)))),r=="Map"&&(t=new Map([...e].map(a=>[l(a[0]),l(a[1])]))),r=="Date"&&(t=new Date(e.getTime())),r=="RegExp"&&(t=RegExp(e.source,F(e))),r=="Array"||r=="Object"){t=Array.isArray(e)?[]:{};for(let a in e)t[a]=l(e[a]);}return t}function w(e,t,r,a){var n,o;if(Array.isArray(t)&&(n=t.slice(0)),typeof t=="string"&&(n=t.split(".")),typeof t=="symbol"&&(n=[t]),!Array.isArray(n))throw new Error("props arg must be an array, a string or a symbol");if(o=n.pop(),!o)return false;x(o);for(var i;i=n.shift();)if(x(i),isNaN(parseInt(i))?(typeof e[i]>"u"&&(e[i]={}),e=e[i]):((e.$each??=[])[i]={},e=e.$each[i]),!e||typeof e!="object")return false;return a?e[o]?e[o].$self=(e[o].$self??=[]).concat(r):e[o]={...e[o],$self:r}:Array.isArray(e[o])?e[o]=e[o].concat(r):e[o]=r,true}function I(e,t,r){if(!e)return r;var a,n;if(Array.isArray(t)&&(a=t.slice(0)),typeof t=="string"&&(a=t.split(".")),typeof t=="symbol"&&(a=[t]),!Array.isArray(a))throw new Error("props arg must be an array, a string or a symbol");for(;a.length;)if(n=a.shift(),!e||!n||(e=e[n],e===undefined))return r;return e}function x(e){if(e=="__proto__"||e=="constructor"||e=="prototype")throw new Error("setting of prototype values not supported")}function h(e,t){let r={autoDirty:e?.autoDirty,lazy:e?.lazy,rewardEarly:e?.rewardEarly,clearExternalErrorsOnChange:e?.clearExternalErrorsOnChange};function a(n,o,i){let O=vue.ref({}),m=vue.computed(()=>vue.unref(o)),M={...r,...i},f=vue.isRef(n)?n:vue.ref(n),E=vue.ref({...l(f.value)}),d=vue.ref({}),R;if(!m.value?.["~standard"])throw new Error('Only "standard-schema" compatible libraries are supported');function N(s){let T={};return s.issues&&s.issues.map(c=>{let D=c.path?.map(y=>typeof y=="object"?y.key:y.toString()).join(".")??"",u=c.path?.[c.path.length-1],A=(typeof u=="object"&&"value"in u?Array.isArray(u.value):false)||("type"in c?c.type==="array":false)||Array.isArray(I(f.value,D));return {path:D,message:c.message,isArray:A}}).forEach(c=>{w(T,c.path,[c.message],c.isArray);}),T}async function g(){let s=m.value["~standard"].validate(f.value);return s instanceof Promise&&(s=await s),d.value=N(s),s}return vue.watch([f,m],g,{deep:true,immediate:true}),R=async()=>{try{return {result:!(await g()).issues?.length,data:f.value}}catch(s){return Promise.reject(s)}},{r$:core.useRootStorage({scopeRules:O,state:f,options:M,schemaErrors:d,initialState:E,shortcuts:t,schemaMode:true,onValidate:R}).regle}}return a}var v=h();function P(e,t){return e}function S(){function e(t,r){return r}return e}var V=S();function b({modifiers:e,shortcuts:t}){let r=h(e,t),a=S();return {useRegleSchema:r,inferSchema:a}}exports.defineRegleSchemaConfig=b;exports.inferSchema=V;exports.useRegleSchema=v;exports.withDeps=P;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import {useRootStorage}from'@regle/core';import {ref,computed,unref,isRef,watch
|
|
1
|
+
import {useRootStorage}from'@regle/core';import {ref,computed,unref,isRef,watch}from'vue';function F(e){if(typeof e.source.flags=="string")return e.source.flags;{let t=[];return e.global&&t.push("g"),e.ignoreCase&&t.push("i"),e.multiline&&t.push("m"),e.sticky&&t.push("y"),e.unicode&&t.push("u"),t.join("")}}function l(e){let t=e,r={}.toString.call(e).slice(8,-1);if(r=="Set"&&(t=new Set([...e].map(a=>l(a)))),r=="Map"&&(t=new Map([...e].map(a=>[l(a[0]),l(a[1])]))),r=="Date"&&(t=new Date(e.getTime())),r=="RegExp"&&(t=RegExp(e.source,F(e))),r=="Array"||r=="Object"){t=Array.isArray(e)?[]:{};for(let a in e)t[a]=l(e[a]);}return t}function w(e,t,r,a){var n,o;if(Array.isArray(t)&&(n=t.slice(0)),typeof t=="string"&&(n=t.split(".")),typeof t=="symbol"&&(n=[t]),!Array.isArray(n))throw new Error("props arg must be an array, a string or a symbol");if(o=n.pop(),!o)return false;x(o);for(var i;i=n.shift();)if(x(i),isNaN(parseInt(i))?(typeof e[i]>"u"&&(e[i]={}),e=e[i]):((e.$each??=[])[i]={},e=e.$each[i]),!e||typeof e!="object")return false;return a?e[o]?e[o].$self=(e[o].$self??=[]).concat(r):e[o]={...e[o],$self:r}:Array.isArray(e[o])?e[o]=e[o].concat(r):e[o]=r,true}function I(e,t,r){if(!e)return r;var a,n;if(Array.isArray(t)&&(a=t.slice(0)),typeof t=="string"&&(a=t.split(".")),typeof t=="symbol"&&(a=[t]),!Array.isArray(a))throw new Error("props arg must be an array, a string or a symbol");for(;a.length;)if(n=a.shift(),!e||!n||(e=e[n],e===undefined))return r;return e}function x(e){if(e=="__proto__"||e=="constructor"||e=="prototype")throw new Error("setting of prototype values not supported")}function h(e,t){let r={autoDirty:e?.autoDirty,lazy:e?.lazy,rewardEarly:e?.rewardEarly,clearExternalErrorsOnChange:e?.clearExternalErrorsOnChange};function a(n,o,i){let O=ref({}),m=computed(()=>unref(o)),M={...r,...i},f=isRef(n)?n:ref(n),E=ref({...l(f.value)}),d=ref({}),R;if(!m.value?.["~standard"])throw new Error('Only "standard-schema" compatible libraries are supported');function N(s){let T={};return s.issues&&s.issues.map(c=>{let D=c.path?.map(y=>typeof y=="object"?y.key:y.toString()).join(".")??"",u=c.path?.[c.path.length-1],A=(typeof u=="object"&&"value"in u?Array.isArray(u.value):false)||("type"in c?c.type==="array":false)||Array.isArray(I(f.value,D));return {path:D,message:c.message,isArray:A}}).forEach(c=>{w(T,c.path,[c.message],c.isArray);}),T}async function g(){let s=m.value["~standard"].validate(f.value);return s instanceof Promise&&(s=await s),d.value=N(s),s}return watch([f,m],g,{deep:true,immediate:true}),R=async()=>{try{return {result:!(await g()).issues?.length,data:f.value}}catch(s){return Promise.reject(s)}},{r$:useRootStorage({scopeRules:O,state:f,options:M,schemaErrors:d,initialState:E,shortcuts:t,schemaMode:true,onValidate:R}).regle}}return a}var v=h();function P(e,t){return e}function S(){function e(t,r){return r}return e}var V=S();function b({modifiers:e,shortcuts:t}){let r=h(e,t),a=S();return {useRegleSchema:r,inferSchema:a}}export{b as defineRegleSchemaConfig,V as inferSchema,v as useRegleSchema,P as withDeps};
|