@regle/schemas 1.13.1 → 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.
@@ -150,62 +150,134 @@ interface useRegleSchemaFn<TShortcuts extends RegleShortcutDefinition<any> = nev
150
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>;
151
151
  }
152
152
  /**
153
- * useRegle serves as the foundation for validation logic.
153
+ * `useRegleSchema` enables validation using Standard Schema compatible libraries
154
+ * like Zod, Valibot, or ArkType.
154
155
  *
155
- * It accepts the following inputs:
156
- *
157
- * @param state - This can be a plain object, a ref, a reactive object, or a structure containing nested refs.
158
- * @param schema - These should align with the structure of your state.
159
- * @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
160
160
  *
161
+ * @example
161
162
  * ```ts
162
163
  * import { useRegleSchema } from '@regle/schemas';
163
164
  * import * as v from 'valibot';
164
165
  *
165
- * const { r$ } = useRegleSchema({ name: '' }, v.object({
166
- * name: v.pipe(v.string(), v.minLength(3))
167
- * }))
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
168
189
  * ```
169
- * Docs: {@link https://reglejs.dev/integrations/schemas-libraries}
190
+ *
191
+ * @see {@link https://reglejs.dev/integrations/schemas-libraries Documentation}
170
192
  */
171
193
  declare const useRegleSchema: useRegleSchemaFn<RegleShortcutDefinition<any>, {}, {}>;
172
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)
173
201
  *
174
- * Force dependency on any RPC schema
202
+ * @example
175
203
  * ```ts
176
- * const foo = ref('');
204
+ * import { withDeps, useRegleSchema } from '@regle/schemas';
205
+ * import * as v from 'valibot';
206
+ *
207
+ * const compareValue = ref('');
177
208
  *
178
209
  * const schema = computed(() => v.object({
179
- * name: withDeps(
180
- * v.pipe(v.string(), v.check((value) => value === foo.value)),
181
- * [foo.value]
182
- * )
183
- * }))
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
+ * }));
184
218
  *
185
- * useRegleSchema({name: ''}, schema)
219
+ * const { r$ } = useRegleSchema({ name: '' }, schema);
186
220
  * ```
221
+ *
222
+ * @see {@link https://reglejs.dev/integrations/schemas-libraries Documentation}
187
223
  */
188
224
  declare function withDeps<TSchema extends StandardSchemaV1, TParams extends unknown[] = []>(schema: TSchema, _depsArray: [...TParams]): TSchema;
189
225
  interface inferSchemaFn {
190
226
  <TSchema extends StandardSchemaV1, TState extends StandardSchemaV1.InferInput<TSchema> | undefined>(state: MaybeRef<DeepPartial<TState>> | DeepReactiveState<DeepPartial<TState>>, rulesFactory: MaybeRef<TSchema>): NoInferLegacy<TSchema>;
191
227
  }
192
228
  /**
193
- * Rule type helper to provide autocomplete and typecheck to your form rules or part of your form rules
194
- * It will just return the rules without any processing.
229
+ * Type helper to provide autocomplete and type-checking for your schema.
230
+ * Returns the schema without any processing - useful with computed schemas.
195
231
  *
196
232
  * @param state - The state reference
197
- * @param schema - Your schema
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}
198
254
  */
199
255
  declare const inferSchema: inferSchemaFn;
200
256
  /**
201
- * Define a global regle configuration, where you can:
202
- * - Define global modifiers
203
- * - Define shortcuts
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';
204
271
  *
205
- * It will return:
272
+ * export const { useRegleSchema, inferSchema } = defineRegleSchemaConfig({
273
+ * modifiers: {
274
+ * lazy: true,
275
+ * rewardEarly: true
276
+ * }
277
+ * });
278
+ * ```
206
279
  *
207
- * - a `useRegleSchema` composable that can typecheck your custom rules
208
- * - an `inferSchema` helper that can typecheck your custom rules
280
+ * @see {@link https://reglejs.dev/integrations/schemas-libraries Documentation}
209
281
  */
210
282
  declare function defineRegleSchemaConfig<TShortcuts extends RegleShortcutDefinition>({
211
283
  modifiers,
@@ -226,6 +298,29 @@ type CreateScopedUseRegleSchemaOptions<TCustomRegle extends useRegleSchemaFn<any
226
298
  declare const useCollectSchemaScope: <TValue$1 extends Record<string, unknown>[] = Record<string, unknown>[]>(namespace?: vue0.MaybeRefOrGetter<string | string[]>) => {
227
299
  r$: _regle_core0.MergedScopedRegles<TValue$1>;
228
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
+ */
229
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, {
230
325
  dispose: () => void;
231
326
  register: () => void;
@@ -9,12 +9,34 @@ function isFile(value) {
9
9
  }
10
10
 
11
11
  /**
12
- * This is the inverse of isFilled. It will check if the value is in any way empty (including arrays and objects)
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
- * @param value - the target value
17
- * @param [considerEmptyArrayInvalid=true] - will return false if set to `false`. (default: `true`)
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
- * useRegle serves as the foundation for validation logic.
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 - This can be a plain object, a ref, a reactive object, or a structure containing nested refs.
298
- * @param schema - These should align with the structure of your state.
299
- * @param modifiers - customize regle behaviour
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
- * const { r$ } = useRegleSchema({ name: '' }, v.object({
306
- * name: v.pipe(v.string(), v.minLength(3))
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
- * Docs: {@link https://reglejs.dev/integrations/schemas-libraries}
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
- * Force dependency on any RPC schema
365
+ * @example
316
366
  * ```ts
317
- * const foo = ref('');
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
- * name: withDeps(
321
- * v.pipe(v.string(), v.check((value) => value === foo.value)),
322
- * [foo.value]
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
- * Rule type helper to provide autocomplete and typecheck to your form rules or part of your form rules
341
- * It will just return the rules without any processing.
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 schema
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 regle configuration, where you can:
350
- * - Define global modifiers
351
- * - Define shortcuts
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
- * It will return:
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
- * - a `useRegleSchema` composable that can typecheck your custom rules
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.13.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/core": "1.13.1",
9
- "@regle/rules": "1.13.1"
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",