zod-form-data
Version:
Validation helpers for [zod](https://github.com/colinhacks/zod) specifically for parsing `FormData` or `URLSearchParams`. This is particularly useful when using [Remix](https://github.com/remix-run/remix) and combos well with [remix-validated-form](https:
100 lines (96 loc) • 4.45 kB
TypeScript
import * as z from 'zod/v4';
import { ZodString, ZodNumber, ZodArray, ZodType, ZodPipe, ZodTransform, ZodObject } from 'zod/v4';
type InputType<DefaultType extends ZodType> = {
(): ZodPipe<ZodTransform, DefaultType>;
<ProvidedType extends ZodType>(schema: ProvidedType): ZodPipe<ZodTransform, ProvidedType>;
};
/**
* Transforms any empty strings to `undefined` before validating.
* This makes it so empty strings will fail required checks,
* allowing you to use `optional` for optional fields instead of `nonempty` for required fields.
* If you call `zfd.text` with no arguments, it will assume the field is a required string by default.
* If you want to customize the schema, you can pass that as an argument.
*/
declare const text: InputType<ZodString>;
/**
* Coerces numerical strings to numbers transforms empty strings to `undefined` before validating.
* If you call `zfd.number` with no arguments,
* it will assume the field is a required number by default.
* If you want to customize the schema, you can pass that as an argument.
*/
declare const numeric: InputType<ZodNumber>;
type CheckboxOpts = {
trueValue?: string;
};
/**
* Turns the value from a checkbox field into a boolean,
* but does not require the checkbox to be checked.
* For checkboxes with a `value` attribute, you can pass that as the `trueValue` option.
*
* @example
* ```ts
* const schema = zfd.formData({
* defaultCheckbox: zfd.checkbox(),
* checkboxWithValue: zfd.checkbox({ trueValue: "true" }),
* mustBeTrue: zfd
* .checkbox()
* .refine((val) => val, "Please check this box"),
* });
* });
* ```
*/
declare const checkbox: ({ trueValue }?: CheckboxOpts) => z.ZodUnion<readonly [z.ZodPipe<z.ZodLiteral<string>, z.ZodTransform<boolean, string>>, z.ZodPipe<z.ZodLiteral<undefined>, z.ZodTransform<boolean, undefined>>]>;
declare const file: InputType<z.ZodType<File>>;
/**
* Preprocesses a field where you expect multiple values could be present for the same field name
* and transforms the value of that field to always be an array.
* If you don't provide a schema, it will assume the field is an array of zfd.text fields
* and will not require any values to be present.
*/
declare const repeatable: InputType<ZodArray<any>>;
/**
* A convenience wrapper for repeatable.
* Instead of passing the schema for an entire array, you pass in the schema for the item type.
*/
declare const repeatableOfType: <T extends ZodType>(schema: T) => ZodPipe<ZodTransform, ZodArray<T>>;
type FormDataLikeInput = {
[Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>;
entries(): IterableIterator<[string, FormDataEntryValue]>;
};
type FormDataType = {
<T extends z.core.$ZodShape>(shape: T): ZodPipe<ZodTransform<ZodObject<T>, FormData | FormDataLikeInput | z.input<ZodObject<T>>>, ZodObject<T>>;
<T extends ZodType>(shape: T): ZodPipe<ZodTransform<T, FormData | FormDataLikeInput | z.input<T>>, T>;
};
declare const json: <T extends ZodType>(schema: T) => ZodPipe<ZodTransform, T>;
declare const preprocessFormData: (formData: unknown) => Record<string, unknown>;
/**
* This helper takes the place of the `z.object` at the root of your schema.
* It wraps your schema in a `z.preprocess` that extracts all the data out of a `FormData`
* and transforms it into a regular object.
* If the `FormData` contains multiple entries with the same field name,
* it will automatically turn that field into an array.
*/
declare const formData: FormDataType;
declare const helpers_checkbox: typeof checkbox;
declare const helpers_file: typeof file;
declare const helpers_formData: typeof formData;
declare const helpers_json: typeof json;
declare const helpers_numeric: typeof numeric;
declare const helpers_preprocessFormData: typeof preprocessFormData;
declare const helpers_repeatable: typeof repeatable;
declare const helpers_repeatableOfType: typeof repeatableOfType;
declare const helpers_text: typeof text;
declare namespace helpers {
export {
helpers_checkbox as checkbox,
helpers_file as file,
helpers_formData as formData,
helpers_json as json,
helpers_numeric as numeric,
helpers_preprocessFormData as preprocessFormData,
helpers_repeatable as repeatable,
helpers_repeatableOfType as repeatableOfType,
helpers_text as text,
};
}
export { checkbox, file, formData, json, numeric, preprocessFormData, repeatable, repeatableOfType, text, helpers as zfd };