@conform-to/zod
Version:
Conform helpers for integrating with Zod
112 lines • 4.18 kB
TypeScript
import type { $ZodType, input, output } from 'zod/v4/core';
import type { ZodType } from 'zod/v4';
/**
* Creates configured coercion functions for form value parsing.
*
* **Example:**
*
* ```tsx
* import { configureCoercion } from '@conform-to/zod/v4/future';
* import { z } from 'zod';
*
* const { coerceFormValue, coerceStructure } = configureCoercion({
* // Trim whitespace and treat whitespace-only as empty
* stripEmptyString: (value) => {
* const trimmed = value.trim();
* return trimmed === '' ? undefined : trimmed;
* },
* type: {
* // Custom number parsing: strip commas
* number: (text) => Number(text.replace(/,/g, '')),
* },
* });
*
* const schema = z.object({ age: z.number(), name: z.string() });
* const validationSchema = coerceFormValue(schema);
* const structuralSchema = coerceStructure(schema);
* ```
*/
export declare function configureCoercion(config?: {
/**
* Validation only: determines what string values are "empty" → undefined.
* Receives a raw string and returns the string (possibly transformed) or
* `undefined` to indicate empty. Empty files are always stripped at the
* system level.
*
* @default (value) => value === '' ? undefined : value
*/
stripEmptyString?: (value: string) => string | undefined;
/**
* Type-specific string → typed value conversion functions.
* Shared between validation and structural modes. The system handles
* non-string passthrough and per-mode empty handling.
*
* Defaults: number via `Number()`, boolean checks `'on'`, date via
* `new Date()`, bigint via `BigInt()` (not overridable here, use
* `customize` instead).
*/
type?: {
number?: (text: string) => number;
boolean?: (text: string) => boolean;
date?: (text: string) => Date;
};
/**
* Per-schema escape hatch. Return a coercion function to override
* the default for a specific schema, or `null` to use the default.
* The coercion function receives the raw form value (string, File,
* array, etc.) and neither `stripEmptyString` nor `coerceString`
* is applied automatically.
*/
customize?: (type: $ZodType) => ((value: unknown) => unknown) | null;
}): {
/**
* Enhances a schema to coerce form values and strip empty values before validation.
* This configured helper uses the options passed to `configureCoercion`.
*
* Results are cached per schema, so this can be called inline.
*
* **Example:**
*
* ```tsx
* const schema = coerceFormValue(z.object({
* age: z.number().optional(),
* subscribe: z.boolean(),
* }));
*
* schema.parse({ age: '', subscribe: 'on' });
* // { age: undefined, subscribe: true }
* ```
*/
coerceFormValue<Schema extends $ZodType>(type: Schema): ZodType<output<Schema>, input<Schema>>;
/**
* Enhances a schema to coerce form values without running validation.
* This configured helper is useful for reading current form values as typed data.
*
* It skips validation, defaults, transforms, and refinements, and does not strip
* empty strings to `undefined`.
*
* For number, boolean, date, and bigint schemas, empty strings and other failed
* string coercions still become fallback values:
*
* - `z.number()` -> `NaN`
* - `z.boolean()` -> `false`
* - `z.date()` -> `Invalid Date`
* - `z.bigint()` -> `0n`
*
* Results are cached per schema, so this can be called inline.
*
* **Example:**
*
* ```tsx
* const schema = coerceStructure(z.object({
* age: z.number().min(10),
* }));
*
* schema.parse({ age: '3' });
* // { age: 3 }
* ```
*/
coerceStructure<Schema extends $ZodType>(type: Schema): ZodType<input<Schema>, input<Schema>>;
};
export declare const coerceFormValue: <Schema extends $ZodType>(type: Schema) => ZodType<output<Schema>, input<Schema>>, coerceStructure: <Schema extends $ZodType>(type: Schema) => ZodType<input<Schema>, input<Schema>>;
//# sourceMappingURL=coercion.d.ts.map