react-zod-form
Version:
Simple form validation and values transformation.
34 lines (33 loc) • 1.25 kB
TypeScript
import { z } from "zod";
export interface ZodFormOptions {
/**
* If true, no transformation will be applied.
*
* @default false
*/
noTransform?: boolean;
}
export interface ZodFormEvents {
parsed(fieldName: string): void;
parsedAll(): void;
error(error: z.ZodError): void;
}
export type FormFieldElement = HTMLInputElement | HTMLTextAreaElement | RadioNodeList;
export type FormFieldValue = FormFieldValueBasic | FormFieldValueBasic[];
export type FormFieldValueBasic = string | number | boolean | File;
/**
* `type-fest` feature.
*/
export type Simplify<T> = {
[KeyType in keyof T]: T[KeyType];
} & {};
export type ShapeToFields<Shape extends z.ZodRawShape> = {
[K in keyof Shape]: Simplify<Shape[K] extends z.AnyZodObject ? ShapeToFields<Shape[K]["shape"]> : (Shape[K] extends z.ZodOptional<z.AnyZodObject> ? ShapeToFields<ReturnType<Shape[K]["unwrap"]>["shape"]> : string)>;
};
/**
* @see https://stackoverflow.com/a/58436959/12468111
* @see https://stackoverflow.com/questions/58434389/typescript-deep-keyof-of-a-nested-object
*/
export type Leaves<T> = T extends object ? {
[K in keyof T]: `${Exclude<K, symbol>}${Leaves<T[K]> extends never ? "" : `.${Leaves<T[K]>}`}`;
}[keyof T] : never;