@regle/schemas 1.13.0 → 1.14.0-beta.3
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.d.ts +130 -43
- package/dist/regle-schemas.js +148 -31
- package/package.json +3 -3
package/dist/regle-schemas.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as _regle_core0 from "@regle/core";
|
|
2
|
-
import { ArrayElement, CreateScopedUseRegleOptions, DeepMaybeRef, DeepReactiveState, HasNamedKeys, HaveAnyRequiredProps, JoinDiscriminatedUnions, LocalRegleBehaviourOptions, Maybe, MaybeOutput, NoInferLegacy, PrimitiveTypes, RegleBehaviourOptions, RegleCollectionErrors, RegleCommonStatus, RegleErrorTree, RegleFieldIssue, RegleIssuesTree, RegleRuleStatus, RegleShortcutDefinition, RegleStaticImpl, TupleToPlainObj, UseScopedRegleOptions, useCollectScopeFn } from "@regle/core";
|
|
2
|
+
import { ArrayElement, CreateScopedUseRegleOptions, DeepMaybeRef, DeepPartial, DeepReactiveState, HasNamedKeys, HaveAnyRequiredProps, JoinDiscriminatedUnions, LocalRegleBehaviourOptions, Maybe, MaybeOutput, NoInferLegacy, PrimitiveTypes, RegleBehaviourOptions, RegleCollectionErrors, RegleCommonStatus, RegleErrorTree, RegleFieldIssue, RegleIssuesTree, RegleRuleStatus, RegleShortcutDefinition, RegleStaticImpl, TupleToPlainObj, UseScopedRegleOptions, useCollectScopeFn } from "@regle/core";
|
|
3
3
|
import * as vue0 from "vue";
|
|
4
4
|
import { MaybeRef, Raw, UnwrapNestedRefs } from "vue";
|
|
5
5
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
6
|
-
import { EmptyObject, IsUnion,
|
|
6
|
+
import { EmptyObject, IsUnion, UnionToTuple } from "type-fest";
|
|
7
7
|
type MaybeSchemaVariantStatus<TState extends Record<string, any> | undefined = Record<string, any>, TSchema extends StandardSchemaV1 = StandardSchemaV1, TShortcuts extends RegleShortcutDefinition = {}, TRoot extends boolean = false> = IsUnion<NonNullable<TState>> extends true ? Omit<RegleSchemaStatus<TState, TSchema, TShortcuts, TRoot>, '$fields'> & {
|
|
8
8
|
$fields: ProcessChildrenFields<TState, TShortcuts>[keyof ProcessChildrenFields<TState, TShortcuts>];
|
|
9
9
|
} & (HasNamedKeys<TState> extends true ? ProcessChildrenFields<TState, TShortcuts>[keyof ProcessChildrenFields<TState, TShortcuts>] : {}) : RegleSchemaStatus<TState, TSchema, TShortcuts, TRoot>;
|
|
@@ -29,7 +29,7 @@ type RegleSingleFieldSchema<TState extends Maybe<PrimitiveTypes>, TSchema extend
|
|
|
29
29
|
} & TAdditionalReturnProperties;
|
|
30
30
|
type RegleSchemaResult<TSchema extends unknown> = {
|
|
31
31
|
valid: false;
|
|
32
|
-
data:
|
|
32
|
+
data: DeepPartial<TSchema>;
|
|
33
33
|
issues: RegleIssuesTree<TSchema, true>;
|
|
34
34
|
errors: RegleErrorTree<TSchema, false, true>;
|
|
35
35
|
} | {
|
|
@@ -56,7 +56,7 @@ type RegleSchemaStatus<TState extends Record<string, any> | undefined = Record<s
|
|
|
56
56
|
/** Collection of all the error messages, collected for all children properties. */
|
|
57
57
|
readonly $silentErrors: RegleErrorTree<TState, false, true>;
|
|
58
58
|
/** 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). */
|
|
59
|
-
$extractDirtyFields: (filterNullishValues?: boolean) =>
|
|
59
|
+
$extractDirtyFields: (filterNullishValues?: boolean) => DeepPartial<TState>;
|
|
60
60
|
} & ProcessNestedFields<TState, TShortcuts> & (IsRoot extends true ? {
|
|
61
61
|
/** 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). */
|
|
62
62
|
$validate: (forceValues?: TState extends EmptyObject ? (HasNamedKeys<TState> extends true ? TState : any) : TState) => Promise<RegleSchemaResult<StandardSchemaV1.InferOutput<TSchema>>>;
|
|
@@ -105,7 +105,7 @@ type RegleSchemaFieldStatus<TState = any, TShortcuts extends RegleShortcutDefini
|
|
|
105
105
|
[`~validator`]: RegleRuleStatus<TState, []>;
|
|
106
106
|
};
|
|
107
107
|
/** 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). */
|
|
108
|
-
$extractDirtyFields: (filterNullishValues?: boolean) =>
|
|
108
|
+
$extractDirtyFields: (filterNullishValues?: boolean) => DeepPartial<TState>;
|
|
109
109
|
} & ([TShortcuts['fields']] extends [never] ? {} : { [K in keyof TShortcuts['fields']]: ReturnType<NonNullable<TShortcuts['fields']>[K]> });
|
|
110
110
|
/**
|
|
111
111
|
* @public
|
|
@@ -128,7 +128,7 @@ type RegleSchemaCollectionStatus<TState extends any[], TShortcuts extends RegleS
|
|
|
128
128
|
/** Collection of all the error messages, collected for all children properties and nested forms. */
|
|
129
129
|
readonly $silentErrors: RegleCollectionErrors<TState>;
|
|
130
130
|
/** 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). */
|
|
131
|
-
$extractDirtyFields: (filterNullishValues?: boolean) =>
|
|
131
|
+
$extractDirtyFields: (filterNullishValues?: boolean) => DeepPartial<TState>;
|
|
132
132
|
} & ([TShortcuts['collections']] extends [never] ? {} : { [K in keyof TShortcuts['collections']]: ReturnType<NonNullable<TShortcuts['collections']>[K]> });
|
|
133
133
|
type RegleSchemaBehaviourOptions = {
|
|
134
134
|
/**
|
|
@@ -147,73 +147,137 @@ type RegleSchemaBehaviourOptions = {
|
|
|
147
147
|
};
|
|
148
148
|
type useRegleSchemaFnOptions<TAdditionalOptions extends Record<string, any>> = Omit<Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<Record<string, any>, {}, never>, 'validationGroups' | 'lazy'> & RegleSchemaBehaviourOptions & TAdditionalOptions;
|
|
149
149
|
interface useRegleSchemaFn<TShortcuts extends RegleShortcutDefinition<any> = never, TAdditionalReturnProperties extends Record<string, any> = {}, TAdditionalOptions extends Record<string, any> = {}> {
|
|
150
|
-
<TSchema extends StandardSchemaV1, TState extends StandardSchemaV1.InferInput<TSchema> | undefined>(...params: [state: MaybeRef<
|
|
151
|
-
recurseIntoArrays: true;
|
|
152
|
-
}>> | DeepReactiveState<PartialDeep<NoInferLegacy<TState>, {
|
|
153
|
-
recurseIntoArrays: true;
|
|
154
|
-
}>>, rulesFactory: MaybeRef<TSchema>, ...(HaveAnyRequiredProps<useRegleSchemaFnOptions<TAdditionalOptions>> extends true ? [options: useRegleSchemaFnOptions<TAdditionalOptions>] : [options?: useRegleSchemaFnOptions<TAdditionalOptions>])]): NonNullable<TState> extends PrimitiveTypes ? RegleSingleFieldSchema<NonNullable<TState>, TSchema, TShortcuts, TAdditionalReturnProperties> : RegleSchema<UnwrapNestedRefs<NonNullable<TState>>, TSchema, TShortcuts, TAdditionalReturnProperties>;
|
|
150
|
+
<TSchema extends StandardSchemaV1, TState extends StandardSchemaV1.InferInput<TSchema> | undefined>(...params: [state: MaybeRef<DeepPartial<NoInferLegacy<TState>>> | DeepReactiveState<DeepPartial<NoInferLegacy<TState>>>, rulesFactory: MaybeRef<TSchema>, ...(HaveAnyRequiredProps<useRegleSchemaFnOptions<TAdditionalOptions>> extends true ? [options: useRegleSchemaFnOptions<TAdditionalOptions>] : [options?: useRegleSchemaFnOptions<TAdditionalOptions>])]): NonNullable<TState> extends PrimitiveTypes ? RegleSingleFieldSchema<NonNullable<TState>, TSchema, TShortcuts, TAdditionalReturnProperties> : RegleSchema<UnwrapNestedRefs<NonNullable<TState>>, TSchema, TShortcuts, TAdditionalReturnProperties>;
|
|
155
151
|
}
|
|
156
152
|
/**
|
|
157
|
-
*
|
|
153
|
+
* `useRegleSchema` enables validation using Standard Schema compatible libraries
|
|
154
|
+
* like Zod, Valibot, or ArkType.
|
|
158
155
|
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
* @param
|
|
162
|
-
* @
|
|
163
|
-
* @param modifiers - customize regle behaviour
|
|
156
|
+
* @param state - Your form data (plain object, ref, reactive object, or structure with nested refs)
|
|
157
|
+
* @param schema - A Standard Schema compliant schema (Zod, Valibot, ArkType, etc.)
|
|
158
|
+
* @param modifiers - Optional configuration to customize regle behavior
|
|
159
|
+
* @returns An object containing `r$` - the reactive validation state
|
|
164
160
|
*
|
|
161
|
+
* @example
|
|
165
162
|
* ```ts
|
|
166
163
|
* import { useRegleSchema } from '@regle/schemas';
|
|
167
164
|
* import * as v from 'valibot';
|
|
168
165
|
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
* }
|
|
166
|
+
* // With Valibot
|
|
167
|
+
* const { r$ } = useRegleSchema(
|
|
168
|
+
* { name: '', email: '' },
|
|
169
|
+
* v.object({
|
|
170
|
+
* name: v.pipe(v.string(), v.minLength(3)),
|
|
171
|
+
* email: v.pipe(v.string(), v.email())
|
|
172
|
+
* })
|
|
173
|
+
* );
|
|
174
|
+
*
|
|
175
|
+
* // With Zod
|
|
176
|
+
* import { z } from 'zod';
|
|
177
|
+
*
|
|
178
|
+
* const { r$ } = useRegleSchema(
|
|
179
|
+
* { name: '' },
|
|
180
|
+
* z.object({
|
|
181
|
+
* name: z.string().min(3)
|
|
182
|
+
* })
|
|
183
|
+
* );
|
|
184
|
+
*
|
|
185
|
+
* // Access validation state
|
|
186
|
+
* r$.$valid // Whether all validations pass
|
|
187
|
+
* r$.$value // The current form values
|
|
188
|
+
* r$.name.$errors // Errors for the name field
|
|
172
189
|
* ```
|
|
173
|
-
*
|
|
190
|
+
*
|
|
191
|
+
* @see {@link https://reglejs.dev/integrations/schemas-libraries Documentation}
|
|
174
192
|
*/
|
|
175
193
|
declare const useRegleSchema: useRegleSchemaFn<RegleShortcutDefinition<any>, {}, {}>;
|
|
176
194
|
/**
|
|
195
|
+
* Force a schema validation to re-run when specified dependencies change.
|
|
196
|
+
* Useful when your schema depends on reactive values that aren't automatically tracked.
|
|
197
|
+
*
|
|
198
|
+
* @param schema - The Standard Schema to wrap
|
|
199
|
+
* @param _depsArray - Array of reactive dependencies (their values will trigger re-validation)
|
|
200
|
+
* @returns The same schema (passthrough)
|
|
177
201
|
*
|
|
178
|
-
*
|
|
202
|
+
* @example
|
|
179
203
|
* ```ts
|
|
180
|
-
*
|
|
204
|
+
* import { withDeps, useRegleSchema } from '@regle/schemas';
|
|
205
|
+
* import * as v from 'valibot';
|
|
206
|
+
*
|
|
207
|
+
* const compareValue = ref('');
|
|
181
208
|
*
|
|
182
209
|
* const schema = computed(() => v.object({
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
210
|
+
* name: withDeps(
|
|
211
|
+
* v.pipe(
|
|
212
|
+
* v.string(),
|
|
213
|
+
* v.check((value) => value === compareValue.value)
|
|
214
|
+
* ),
|
|
215
|
+
* [compareValue.value] // Re-validate when this changes
|
|
216
|
+
* )
|
|
217
|
+
* }));
|
|
188
218
|
*
|
|
189
|
-
* useRegleSchema({name: ''}, schema)
|
|
219
|
+
* const { r$ } = useRegleSchema({ name: '' }, schema);
|
|
190
220
|
* ```
|
|
221
|
+
*
|
|
222
|
+
* @see {@link https://reglejs.dev/integrations/schemas-libraries Documentation}
|
|
191
223
|
*/
|
|
192
224
|
declare function withDeps<TSchema extends StandardSchemaV1, TParams extends unknown[] = []>(schema: TSchema, _depsArray: [...TParams]): TSchema;
|
|
193
225
|
interface inferSchemaFn {
|
|
194
|
-
<TSchema extends StandardSchemaV1, TState extends StandardSchemaV1.InferInput<TSchema> | undefined>(state: MaybeRef<
|
|
195
|
-
recurseIntoArrays: true;
|
|
196
|
-
}>> | DeepReactiveState<PartialDeep<TState, {
|
|
197
|
-
recurseIntoArrays: true;
|
|
198
|
-
}>>, rulesFactory: MaybeRef<TSchema>): NoInferLegacy<TSchema>;
|
|
226
|
+
<TSchema extends StandardSchemaV1, TState extends StandardSchemaV1.InferInput<TSchema> | undefined>(state: MaybeRef<DeepPartial<TState>> | DeepReactiveState<DeepPartial<TState>>, rulesFactory: MaybeRef<TSchema>): NoInferLegacy<TSchema>;
|
|
199
227
|
}
|
|
200
228
|
/**
|
|
201
|
-
*
|
|
202
|
-
*
|
|
229
|
+
* Type helper to provide autocomplete and type-checking for your schema.
|
|
230
|
+
* Returns the schema without any processing - useful with computed schemas.
|
|
203
231
|
*
|
|
204
232
|
* @param state - The state reference
|
|
205
|
-
* @param schema - Your
|
|
233
|
+
* @param schema - Your Standard Schema (Zod, Valibot, ArkType, etc.)
|
|
234
|
+
* @returns The schema (passthrough)
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* import { inferSchema, useRegleSchema } from '@regle/schemas';
|
|
239
|
+
* import { z } from 'zod';
|
|
240
|
+
*
|
|
241
|
+
* const state = ref({ name: '' });
|
|
242
|
+
*
|
|
243
|
+
* // inferSchema preserves TypeScript autocompletion
|
|
244
|
+
* const schema = computed(() => {
|
|
245
|
+
* return inferSchema(state, z.object({
|
|
246
|
+
* name: z.string().min(2)
|
|
247
|
+
* }));
|
|
248
|
+
* });
|
|
249
|
+
*
|
|
250
|
+
* const { r$ } = useRegleSchema(state, schema);
|
|
251
|
+
* ```
|
|
252
|
+
*
|
|
253
|
+
* @see {@link https://reglejs.dev/integrations/schemas-libraries Documentation}
|
|
206
254
|
*/
|
|
207
255
|
declare const inferSchema: inferSchemaFn;
|
|
208
256
|
/**
|
|
209
|
-
* Define a global
|
|
210
|
-
*
|
|
211
|
-
*
|
|
257
|
+
* Define a global configuration for `useRegleSchema`.
|
|
258
|
+
*
|
|
259
|
+
* Features:
|
|
260
|
+
* - Define global modifiers (lazy, rewardEarly, etc.)
|
|
261
|
+
* - Define shortcuts for common validation patterns
|
|
262
|
+
*
|
|
263
|
+
* @param options - Configuration options
|
|
264
|
+
* @param options.modifiers - Global behavior modifiers
|
|
265
|
+
* @param options.shortcuts - Reusable validation shortcuts
|
|
266
|
+
* @returns Object containing typed `useRegleSchema` and `inferSchema` functions
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```ts
|
|
270
|
+
* import { defineRegleSchemaConfig } from '@regle/schemas';
|
|
212
271
|
*
|
|
213
|
-
*
|
|
272
|
+
* export const { useRegleSchema, inferSchema } = defineRegleSchemaConfig({
|
|
273
|
+
* modifiers: {
|
|
274
|
+
* lazy: true,
|
|
275
|
+
* rewardEarly: true
|
|
276
|
+
* }
|
|
277
|
+
* });
|
|
278
|
+
* ```
|
|
214
279
|
*
|
|
215
|
-
*
|
|
216
|
-
* - an `inferSchema` helper that can typecheck your custom rules
|
|
280
|
+
* @see {@link https://reglejs.dev/integrations/schemas-libraries Documentation}
|
|
217
281
|
*/
|
|
218
282
|
declare function defineRegleSchemaConfig<TShortcuts extends RegleShortcutDefinition>({
|
|
219
283
|
modifiers,
|
|
@@ -234,6 +298,29 @@ type CreateScopedUseRegleSchemaOptions<TCustomRegle extends useRegleSchemaFn<any
|
|
|
234
298
|
declare const useCollectSchemaScope: <TValue$1 extends Record<string, unknown>[] = Record<string, unknown>[]>(namespace?: vue0.MaybeRefOrGetter<string | string[]>) => {
|
|
235
299
|
r$: _regle_core0.MergedScopedRegles<TValue$1>;
|
|
236
300
|
}, useScopedRegleSchema: useRegleSchemaFn<_regle_core0.RegleShortcutDefinition<any>, {}, {}>;
|
|
301
|
+
/**
|
|
302
|
+
* Create a scoped validation system for schema-based validation.
|
|
303
|
+
* Similar to `createScopedUseRegle` but for use with Standard Schema libraries.
|
|
304
|
+
*
|
|
305
|
+
* @param options - Configuration options
|
|
306
|
+
* @param options.customUseRegle - Custom useRegleSchema instance with your global config
|
|
307
|
+
* @param options.customStore - External ref to store collected instances
|
|
308
|
+
* @param options.asRecord - If true, collect instances in a Record (requires `id` param)
|
|
309
|
+
* @returns Object containing `useScopedRegle` and `useCollectScope` functions
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```ts
|
|
313
|
+
* import { createScopedUseRegleSchema, defineRegleSchemaConfig } from '@regle/schemas';
|
|
314
|
+
*
|
|
315
|
+
* const { useRegleSchema } = defineRegleSchemaConfig({...});
|
|
316
|
+
*
|
|
317
|
+
* export const { useScopedRegle, useCollectScope } = createScopedUseRegleSchema({
|
|
318
|
+
* customUseRegle: useRegleSchema
|
|
319
|
+
* });
|
|
320
|
+
* ```
|
|
321
|
+
*
|
|
322
|
+
* @see {@link https://reglejs.dev/advanced-usage/scoped-validation Documentation}
|
|
323
|
+
*/
|
|
237
324
|
declare const createScopedUseRegleSchema: <TCustomRegle extends useRegleSchemaFn = useRegleSchemaFn, TAsRecord extends boolean = false, TReturnedRegle extends useRegleSchemaFn<any, any, any> = (TCustomRegle extends useRegleSchemaFn<infer S> ? useRegleSchemaFn<S, {
|
|
238
325
|
dispose: () => void;
|
|
239
326
|
register: () => void;
|
package/dist/regle-schemas.js
CHANGED
|
@@ -9,12 +9,34 @@ function isFile(value) {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* Checks if a value is empty in any way (including arrays and objects).
|
|
13
|
+
* This is the inverse of `isFilled`.
|
|
13
14
|
*
|
|
14
|
-
* isEmpty also acts as a type guard.
|
|
15
|
+
* `isEmpty` also acts as a type guard.
|
|
15
16
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
17
|
+
* By default, it considers an empty array as `true`. You can override this behavior with `considerEmptyArrayInvalid`.
|
|
18
|
+
*
|
|
19
|
+
* @param value - The target value to check
|
|
20
|
+
* @param considerEmptyArrayInvalid - When `false`, empty arrays are not considered empty (default: `true`)
|
|
21
|
+
* @returns `true` if the value is empty, `false` otherwise
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { createRule, type Maybe } from '@regle/core';
|
|
26
|
+
* import { isEmpty } from '@regle/rules';
|
|
27
|
+
*
|
|
28
|
+
* const rule = createRule({
|
|
29
|
+
* validator(value: Maybe<string>) {
|
|
30
|
+
* if (isEmpty(value)) {
|
|
31
|
+
* return true;
|
|
32
|
+
* }
|
|
33
|
+
* return check(value);
|
|
34
|
+
* },
|
|
35
|
+
* message: 'Error'
|
|
36
|
+
* })
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/validations-helpers#isempty Documentation}
|
|
18
40
|
*/
|
|
19
41
|
function isEmpty(value, considerEmptyArrayInvalid = true) {
|
|
20
42
|
if (value === void 0 || value === null) return true;
|
|
@@ -290,41 +312,77 @@ function createUseRegleSchemaComposable(options, shortcuts) {
|
|
|
290
312
|
return useRegleSchema$1;
|
|
291
313
|
}
|
|
292
314
|
/**
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
* It accepts the following inputs:
|
|
315
|
+
* `useRegleSchema` enables validation using Standard Schema compatible libraries
|
|
316
|
+
* like Zod, Valibot, or ArkType.
|
|
296
317
|
*
|
|
297
|
-
* @param state -
|
|
298
|
-
* @param schema -
|
|
299
|
-
* @param modifiers - customize regle
|
|
318
|
+
* @param state - Your form data (plain object, ref, reactive object, or structure with nested refs)
|
|
319
|
+
* @param schema - A Standard Schema compliant schema (Zod, Valibot, ArkType, etc.)
|
|
320
|
+
* @param modifiers - Optional configuration to customize regle behavior
|
|
321
|
+
* @returns An object containing `r$` - the reactive validation state
|
|
300
322
|
*
|
|
323
|
+
* @example
|
|
301
324
|
* ```ts
|
|
302
325
|
* import { useRegleSchema } from '@regle/schemas';
|
|
303
326
|
* import * as v from 'valibot';
|
|
304
327
|
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
* }
|
|
328
|
+
* // With Valibot
|
|
329
|
+
* const { r$ } = useRegleSchema(
|
|
330
|
+
* { name: '', email: '' },
|
|
331
|
+
* v.object({
|
|
332
|
+
* name: v.pipe(v.string(), v.minLength(3)),
|
|
333
|
+
* email: v.pipe(v.string(), v.email())
|
|
334
|
+
* })
|
|
335
|
+
* );
|
|
336
|
+
*
|
|
337
|
+
* // With Zod
|
|
338
|
+
* import { z } from 'zod';
|
|
339
|
+
*
|
|
340
|
+
* const { r$ } = useRegleSchema(
|
|
341
|
+
* { name: '' },
|
|
342
|
+
* z.object({
|
|
343
|
+
* name: z.string().min(3)
|
|
344
|
+
* })
|
|
345
|
+
* );
|
|
346
|
+
*
|
|
347
|
+
* // Access validation state
|
|
348
|
+
* r$.$valid // Whether all validations pass
|
|
349
|
+
* r$.$value // The current form values
|
|
350
|
+
* r$.name.$errors // Errors for the name field
|
|
308
351
|
* ```
|
|
309
|
-
*
|
|
352
|
+
*
|
|
353
|
+
* @see {@link https://reglejs.dev/integrations/schemas-libraries Documentation}
|
|
310
354
|
*/
|
|
311
355
|
const useRegleSchema = createUseRegleSchemaComposable();
|
|
312
356
|
|
|
313
357
|
/**
|
|
358
|
+
* Force a schema validation to re-run when specified dependencies change.
|
|
359
|
+
* Useful when your schema depends on reactive values that aren't automatically tracked.
|
|
360
|
+
*
|
|
361
|
+
* @param schema - The Standard Schema to wrap
|
|
362
|
+
* @param _depsArray - Array of reactive dependencies (their values will trigger re-validation)
|
|
363
|
+
* @returns The same schema (passthrough)
|
|
314
364
|
*
|
|
315
|
-
*
|
|
365
|
+
* @example
|
|
316
366
|
* ```ts
|
|
317
|
-
*
|
|
367
|
+
* import { withDeps, useRegleSchema } from '@regle/schemas';
|
|
368
|
+
* import * as v from 'valibot';
|
|
369
|
+
*
|
|
370
|
+
* const compareValue = ref('');
|
|
318
371
|
*
|
|
319
372
|
* const schema = computed(() => v.object({
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
*
|
|
324
|
-
*
|
|
373
|
+
* name: withDeps(
|
|
374
|
+
* v.pipe(
|
|
375
|
+
* v.string(),
|
|
376
|
+
* v.check((value) => value === compareValue.value)
|
|
377
|
+
* ),
|
|
378
|
+
* [compareValue.value] // Re-validate when this changes
|
|
379
|
+
* )
|
|
380
|
+
* }));
|
|
325
381
|
*
|
|
326
|
-
* useRegleSchema({name: ''}, schema)
|
|
382
|
+
* const { r$ } = useRegleSchema({ name: '' }, schema);
|
|
327
383
|
* ```
|
|
384
|
+
*
|
|
385
|
+
* @see {@link https://reglejs.dev/integrations/schemas-libraries Documentation}
|
|
328
386
|
*/
|
|
329
387
|
function withDeps(schema, _depsArray) {
|
|
330
388
|
return schema;
|
|
@@ -337,23 +395,59 @@ function createInferSchemaHelper() {
|
|
|
337
395
|
return inferSchema$1;
|
|
338
396
|
}
|
|
339
397
|
/**
|
|
340
|
-
*
|
|
341
|
-
*
|
|
398
|
+
* Type helper to provide autocomplete and type-checking for your schema.
|
|
399
|
+
* Returns the schema without any processing - useful with computed schemas.
|
|
342
400
|
*
|
|
343
401
|
* @param state - The state reference
|
|
344
|
-
* @param schema - Your
|
|
402
|
+
* @param schema - Your Standard Schema (Zod, Valibot, ArkType, etc.)
|
|
403
|
+
* @returns The schema (passthrough)
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* ```ts
|
|
407
|
+
* import { inferSchema, useRegleSchema } from '@regle/schemas';
|
|
408
|
+
* import { z } from 'zod';
|
|
409
|
+
*
|
|
410
|
+
* const state = ref({ name: '' });
|
|
411
|
+
*
|
|
412
|
+
* // inferSchema preserves TypeScript autocompletion
|
|
413
|
+
* const schema = computed(() => {
|
|
414
|
+
* return inferSchema(state, z.object({
|
|
415
|
+
* name: z.string().min(2)
|
|
416
|
+
* }));
|
|
417
|
+
* });
|
|
418
|
+
*
|
|
419
|
+
* const { r$ } = useRegleSchema(state, schema);
|
|
420
|
+
* ```
|
|
421
|
+
*
|
|
422
|
+
* @see {@link https://reglejs.dev/integrations/schemas-libraries Documentation}
|
|
345
423
|
*/
|
|
346
424
|
const inferSchema = createInferSchemaHelper();
|
|
347
425
|
|
|
348
426
|
/**
|
|
349
|
-
* Define a global
|
|
350
|
-
*
|
|
351
|
-
*
|
|
427
|
+
* Define a global configuration for `useRegleSchema`.
|
|
428
|
+
*
|
|
429
|
+
* Features:
|
|
430
|
+
* - Define global modifiers (lazy, rewardEarly, etc.)
|
|
431
|
+
* - Define shortcuts for common validation patterns
|
|
352
432
|
*
|
|
353
|
-
*
|
|
433
|
+
* @param options - Configuration options
|
|
434
|
+
* @param options.modifiers - Global behavior modifiers
|
|
435
|
+
* @param options.shortcuts - Reusable validation shortcuts
|
|
436
|
+
* @returns Object containing typed `useRegleSchema` and `inferSchema` functions
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* ```ts
|
|
440
|
+
* import { defineRegleSchemaConfig } from '@regle/schemas';
|
|
441
|
+
*
|
|
442
|
+
* export const { useRegleSchema, inferSchema } = defineRegleSchemaConfig({
|
|
443
|
+
* modifiers: {
|
|
444
|
+
* lazy: true,
|
|
445
|
+
* rewardEarly: true
|
|
446
|
+
* }
|
|
447
|
+
* });
|
|
448
|
+
* ```
|
|
354
449
|
*
|
|
355
|
-
*
|
|
356
|
-
* - an `inferSchema` helper that can typecheck your custom rules
|
|
450
|
+
* @see {@link https://reglejs.dev/integrations/schemas-libraries Documentation}
|
|
357
451
|
*/
|
|
358
452
|
function defineRegleSchemaConfig({ modifiers, shortcuts }) {
|
|
359
453
|
return {
|
|
@@ -363,6 +457,29 @@ function defineRegleSchemaConfig({ modifiers, shortcuts }) {
|
|
|
363
457
|
}
|
|
364
458
|
|
|
365
459
|
const { useCollectScope: useCollectSchemaScope, useScopedRegle: useScopedRegleSchema } = createScopedUseRegle({ customUseRegle: useRegleSchema });
|
|
460
|
+
/**
|
|
461
|
+
* Create a scoped validation system for schema-based validation.
|
|
462
|
+
* Similar to `createScopedUseRegle` but for use with Standard Schema libraries.
|
|
463
|
+
*
|
|
464
|
+
* @param options - Configuration options
|
|
465
|
+
* @param options.customUseRegle - Custom useRegleSchema instance with your global config
|
|
466
|
+
* @param options.customStore - External ref to store collected instances
|
|
467
|
+
* @param options.asRecord - If true, collect instances in a Record (requires `id` param)
|
|
468
|
+
* @returns Object containing `useScopedRegle` and `useCollectScope` functions
|
|
469
|
+
*
|
|
470
|
+
* @example
|
|
471
|
+
* ```ts
|
|
472
|
+
* import { createScopedUseRegleSchema, defineRegleSchemaConfig } from '@regle/schemas';
|
|
473
|
+
*
|
|
474
|
+
* const { useRegleSchema } = defineRegleSchemaConfig({...});
|
|
475
|
+
*
|
|
476
|
+
* export const { useScopedRegle, useCollectScope } = createScopedUseRegleSchema({
|
|
477
|
+
* customUseRegle: useRegleSchema
|
|
478
|
+
* });
|
|
479
|
+
* ```
|
|
480
|
+
*
|
|
481
|
+
* @see {@link https://reglejs.dev/advanced-usage/scoped-validation Documentation}
|
|
482
|
+
*/
|
|
366
483
|
const createScopedUseRegleSchema = (options) => {
|
|
367
484
|
const { customStore, customUseRegle = useRegleSchema, asRecord = false } = options ?? {};
|
|
368
485
|
return createScopedUseRegle({
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@regle/schemas",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.0-beta.3",
|
|
4
4
|
"description": "Schemas adapter for Regle",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@standard-schema/spec": "1.0.0",
|
|
7
7
|
"type-fest": "5.2.0",
|
|
8
|
-
"@regle/
|
|
9
|
-
"@regle/
|
|
8
|
+
"@regle/core": "1.14.0-beta.3",
|
|
9
|
+
"@regle/rules": "1.14.0-beta.3"
|
|
10
10
|
},
|
|
11
11
|
"peerDependencies": {
|
|
12
12
|
"valibot": "^1.0.0",
|