@zod-utils/react-hook-form 2.0.1 → 2.0.2

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 CHANGED
@@ -458,6 +458,7 @@ import {
458
458
  type DiscriminatorValue,
459
459
  type InferredFieldValues,
460
460
  type ValidFieldPaths,
461
+ type ValidFieldPathsOfType,
461
462
  } from "@zod-utils/react-hook-form";
462
463
  ```
463
464
 
@@ -525,6 +526,69 @@ Use this when all fields need to accept `null`, not just objects/arrays.
525
526
 
526
527
  ---
527
528
 
529
+ #### `ValidFieldPathsOfType<TSchema, TValueConstraint, TDiscriminatorKey?, TDiscriminatorValue?, TFieldValues?>`
530
+
531
+ Extracts field paths where the value type matches a constraint. Useful for building type-safe form components that only accept paths of specific types.
532
+
533
+ ```typescript
534
+ import type { ValidFieldPathsOfType } from "@zod-utils/react-hook-form";
535
+ import { z } from "zod";
536
+
537
+ const schema = z.object({
538
+ name: z.string(),
539
+ age: z.number(),
540
+ score: z.number().optional(),
541
+ active: z.boolean(),
542
+ });
543
+
544
+ // Only accept number field paths
545
+ type NumberPaths = ValidFieldPathsOfType<typeof schema, number>;
546
+ // "age" | "score"
547
+
548
+ // Only accept boolean field paths
549
+ type BooleanPaths = ValidFieldPathsOfType<typeof schema, boolean>;
550
+ // "active"
551
+ ```
552
+
553
+ **Use case - Type-safe form field components:**
554
+
555
+ ```tsx
556
+ // NumberFormField only accepts paths to number fields
557
+ function NumberFormField<
558
+ TSchema extends z.ZodType,
559
+ TPath extends ValidFieldPathsOfType<TSchema, number>
560
+ >({ schema, name }: { schema: TSchema; name: TPath }) {
561
+ // name is guaranteed to be a path to a number field
562
+ return <input type="number" name={name} />;
563
+ }
564
+
565
+ // ✅ Works - 'age' is a number field
566
+ <NumberFormField schema={schema} name="age" />
567
+
568
+ // ❌ TypeScript error - 'name' is a string field, not number
569
+ <NumberFormField schema={schema} name="name" />
570
+ ```
571
+
572
+ **With discriminated unions:**
573
+
574
+ ```typescript
575
+ const formSchema = z.discriminatedUnion("mode", [
576
+ z.object({ mode: z.literal("create"), name: z.string(), priority: z.number() }),
577
+ z.object({ mode: z.literal("edit"), id: z.number(), rating: z.number() }),
578
+ ]);
579
+
580
+ // Number paths for 'edit' variant
581
+ type EditNumberPaths = ValidFieldPathsOfType<
582
+ typeof formSchema,
583
+ number,
584
+ "mode",
585
+ "edit"
586
+ >;
587
+ // "id" | "rating"
588
+ ```
589
+
590
+ ---
591
+
528
592
  ## Complete Example
529
593
 
530
594
  ```typescript
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { DiscriminatorKey, DiscriminatorValue, Discriminator, ValidPaths, ExtractZodByPath, ZodUnionCheck, Simplify } from '@zod-utils/core';
1
+ import { DiscriminatorKey, DiscriminatorValue, Discriminator, ValidPaths, ExtractZodByPath, ZodUnionCheck, Simplify, ValidPathsOfType } from '@zod-utils/core';
2
2
  export * from '@zod-utils/core';
3
3
  import * as react_jsx_runtime from 'react/jsx-runtime';
4
4
  import { Context, ReactNode } from 'react';
@@ -328,6 +328,61 @@ type InferredFieldValues<TSchema extends z.ZodType> = z.input<TSchema> & FieldVa
328
328
  * ```
329
329
  */
330
330
  type ValidFieldPaths<TSchema extends z.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>, TFieldValues extends InferredFieldValues<TSchema> = InferredFieldValues<TSchema>> = ValidPaths<TSchema, TDiscriminatorKey, TDiscriminatorValue> & Path<TFieldValues>;
331
+ /**
332
+ * Type-safe field paths filtered by value type for React Hook Form.
333
+ *
334
+ * Combines `ValidPathsOfType` (filters by value type) with React Hook Form's
335
+ * `Path` type for full compatibility with form field APIs.
336
+ *
337
+ * @template TSchema - The Zod schema type
338
+ * @template TValueConstraint - The value type to filter by
339
+ * @template TDiscriminatorKey - The discriminator key (for discriminated unions)
340
+ * @template TDiscriminatorValue - The discriminator value (for discriminated unions)
341
+ * @template TFieldValues - The form field values type (inferred from schema)
342
+ *
343
+ * @example
344
+ * Get all string field paths for form
345
+ * ```typescript
346
+ * const schema = z.object({
347
+ * name: z.string(),
348
+ * age: z.number(),
349
+ * email: z.string().optional(),
350
+ * });
351
+ *
352
+ * type StringFields = ValidFieldPathsOfType<typeof schema, string>;
353
+ * // "name" | "email" - compatible with react-hook-form Path
354
+ * ```
355
+ *
356
+ * @example
357
+ * Get all number field paths for form
358
+ * ```typescript
359
+ * const schema = z.object({
360
+ * id: z.number(),
361
+ * name: z.string(),
362
+ * count: z.number().optional(),
363
+ * });
364
+ *
365
+ * type NumberFields = ValidFieldPathsOfType<typeof schema, number>;
366
+ * // "id" | "count"
367
+ * ```
368
+ *
369
+ * @example
370
+ * With discriminated union
371
+ * ```typescript
372
+ * const schema = z.discriminatedUnion('mode', [
373
+ * z.object({ mode: z.literal('create'), name: z.string() }),
374
+ * z.object({ mode: z.literal('edit'), id: z.number() }),
375
+ * ]);
376
+ *
377
+ * type EditNumberFields = ValidFieldPathsOfType<typeof schema, number, 'mode', 'edit'>;
378
+ * // "id"
379
+ * ```
380
+ *
381
+ * @see {@link ValidPathsOfType} for the base type without react-hook-form Path intersection
382
+ * @see {@link ValidFieldPaths} for discriminated union field filtering
383
+ * @since 0.5.0
384
+ */
385
+ type ValidFieldPathsOfType<TSchema extends z.ZodType, TValueConstraint, TDiscriminatorKey extends DiscriminatorKey<TSchema> = DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = DiscriminatorValue<TSchema, TDiscriminatorKey>, TFieldValues extends InferredFieldValues<TSchema> = InferredFieldValues<TSchema>> = ValidPathsOfType<TSchema, TValueConstraint, TDiscriminatorKey, TDiscriminatorValue> & Path<TFieldValues>;
331
386
 
332
387
  /**
333
388
  * Type-safe React Hook Form wrapper with automatic Zod v4 schema validation and type transformation.
@@ -501,4 +556,4 @@ declare const useZodForm: <TInput extends FieldValues, TOutput extends FieldValu
501
556
  zodResolverOptions?: Parameters<typeof zodResolver>[1];
502
557
  } & Omit<UseFormProps<TFormInput, unknown, TOutput>, "resolver" | "defaultValues">) => react_hook_form.UseFormReturn<TFormInput, unknown, TOutput>;
503
558
 
504
- export { FormSchemaContext, type FormSchemaContextType, type FormSchemaContextValue, FormSchemaProvider, type InferredFieldValues, type PartialWithAllNullables, type PartialWithNullableObjects, type ValidFieldPaths, isRequiredField, useExtractFieldFromSchema, useFieldChecks, useFormSchema, useIsRequiredField, useZodForm };
559
+ export { FormSchemaContext, type FormSchemaContextType, type FormSchemaContextValue, FormSchemaProvider, type InferredFieldValues, type PartialWithAllNullables, type PartialWithNullableObjects, type ValidFieldPaths, type ValidFieldPathsOfType, isRequiredField, useExtractFieldFromSchema, useFieldChecks, useFormSchema, useIsRequiredField, useZodForm };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { DiscriminatorKey, DiscriminatorValue, Discriminator, ValidPaths, ExtractZodByPath, ZodUnionCheck, Simplify } from '@zod-utils/core';
1
+ import { DiscriminatorKey, DiscriminatorValue, Discriminator, ValidPaths, ExtractZodByPath, ZodUnionCheck, Simplify, ValidPathsOfType } from '@zod-utils/core';
2
2
  export * from '@zod-utils/core';
3
3
  import * as react_jsx_runtime from 'react/jsx-runtime';
4
4
  import { Context, ReactNode } from 'react';
@@ -328,6 +328,61 @@ type InferredFieldValues<TSchema extends z.ZodType> = z.input<TSchema> & FieldVa
328
328
  * ```
329
329
  */
330
330
  type ValidFieldPaths<TSchema extends z.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>, TFieldValues extends InferredFieldValues<TSchema> = InferredFieldValues<TSchema>> = ValidPaths<TSchema, TDiscriminatorKey, TDiscriminatorValue> & Path<TFieldValues>;
331
+ /**
332
+ * Type-safe field paths filtered by value type for React Hook Form.
333
+ *
334
+ * Combines `ValidPathsOfType` (filters by value type) with React Hook Form's
335
+ * `Path` type for full compatibility with form field APIs.
336
+ *
337
+ * @template TSchema - The Zod schema type
338
+ * @template TValueConstraint - The value type to filter by
339
+ * @template TDiscriminatorKey - The discriminator key (for discriminated unions)
340
+ * @template TDiscriminatorValue - The discriminator value (for discriminated unions)
341
+ * @template TFieldValues - The form field values type (inferred from schema)
342
+ *
343
+ * @example
344
+ * Get all string field paths for form
345
+ * ```typescript
346
+ * const schema = z.object({
347
+ * name: z.string(),
348
+ * age: z.number(),
349
+ * email: z.string().optional(),
350
+ * });
351
+ *
352
+ * type StringFields = ValidFieldPathsOfType<typeof schema, string>;
353
+ * // "name" | "email" - compatible with react-hook-form Path
354
+ * ```
355
+ *
356
+ * @example
357
+ * Get all number field paths for form
358
+ * ```typescript
359
+ * const schema = z.object({
360
+ * id: z.number(),
361
+ * name: z.string(),
362
+ * count: z.number().optional(),
363
+ * });
364
+ *
365
+ * type NumberFields = ValidFieldPathsOfType<typeof schema, number>;
366
+ * // "id" | "count"
367
+ * ```
368
+ *
369
+ * @example
370
+ * With discriminated union
371
+ * ```typescript
372
+ * const schema = z.discriminatedUnion('mode', [
373
+ * z.object({ mode: z.literal('create'), name: z.string() }),
374
+ * z.object({ mode: z.literal('edit'), id: z.number() }),
375
+ * ]);
376
+ *
377
+ * type EditNumberFields = ValidFieldPathsOfType<typeof schema, number, 'mode', 'edit'>;
378
+ * // "id"
379
+ * ```
380
+ *
381
+ * @see {@link ValidPathsOfType} for the base type without react-hook-form Path intersection
382
+ * @see {@link ValidFieldPaths} for discriminated union field filtering
383
+ * @since 0.5.0
384
+ */
385
+ type ValidFieldPathsOfType<TSchema extends z.ZodType, TValueConstraint, TDiscriminatorKey extends DiscriminatorKey<TSchema> = DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = DiscriminatorValue<TSchema, TDiscriminatorKey>, TFieldValues extends InferredFieldValues<TSchema> = InferredFieldValues<TSchema>> = ValidPathsOfType<TSchema, TValueConstraint, TDiscriminatorKey, TDiscriminatorValue> & Path<TFieldValues>;
331
386
 
332
387
  /**
333
388
  * Type-safe React Hook Form wrapper with automatic Zod v4 schema validation and type transformation.
@@ -501,4 +556,4 @@ declare const useZodForm: <TInput extends FieldValues, TOutput extends FieldValu
501
556
  zodResolverOptions?: Parameters<typeof zodResolver>[1];
502
557
  } & Omit<UseFormProps<TFormInput, unknown, TOutput>, "resolver" | "defaultValues">) => react_hook_form.UseFormReturn<TFormInput, unknown, TOutput>;
503
558
 
504
- export { FormSchemaContext, type FormSchemaContextType, type FormSchemaContextValue, FormSchemaProvider, type InferredFieldValues, type PartialWithAllNullables, type PartialWithNullableObjects, type ValidFieldPaths, isRequiredField, useExtractFieldFromSchema, useFieldChecks, useFormSchema, useIsRequiredField, useZodForm };
559
+ export { FormSchemaContext, type FormSchemaContextType, type FormSchemaContextValue, FormSchemaProvider, type InferredFieldValues, type PartialWithAllNullables, type PartialWithNullableObjects, type ValidFieldPaths, type ValidFieldPathsOfType, isRequiredField, useExtractFieldFromSchema, useFieldChecks, useFormSchema, useIsRequiredField, useZodForm };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zod-utils/react-hook-form",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "React Hook Form integration and utilities for Zod schemas",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",