UNPKG

form-atoms

Version:
51 lines (48 loc) 1.92 kB
import { Getter } from 'jotai'; import { parseAsync, ValiError, BaseSchema, BaseIssue, BaseSchemaAsync } from 'valibot'; import { Validate, ValidateOn } from './index.js'; import 'react'; import 'jotai/utils'; /** * Validate your field atoms with Valibot schemas. This function validates * on every "user" and "submit" event, in addition to other events you specify. * * @param schema - Valibot schema or a function that returns a Zod schema * @param config - Configuration options * @example * ```ts * import { } * const nameForm = formAtom({ * name: fieldAtom({ * validate: valibotValidate(string(), { * on: "blur", * when: "dirty", * }) * }) * }) * ``` */ declare function createValibotValidator(parse: typeof parseAsync): <Value>(schema: ((get: Getter) => ValibotSchema) | ValibotSchema, config?: ValibotValidateConfig) => ((state: Parameters<Exclude<Validate<Value>, undefined>>[0]) => Promise<string[] | undefined>) & { or(config: Omit<ValibotValidateConfig, "formatError">): ((state: Parameters<Exclude<Validate<Value>, undefined>>[0]) => Promise<string[] | undefined>) & any; }; type ValibotValidateConfig = { /** * The event or events that triggers validation. */ on?: ValibotValidateOn | ValibotValidateOn[]; /** * Validate if the field is: * - `touched` * - `dirty` */ when?: "touched" | "dirty" | ("touched" | "dirty")[]; /** * Format the error message returned by the validator. * * @param error - A ZodError object */ formatError?: (error: ValiError<ValibotSchema>) => string[]; }; type ValibotSchema = BaseSchema<unknown, unknown, BaseIssue<unknown>> | BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>; type ValibotValidateOn = Exclude<ValidateOn, "user" | "submit">; export { type ValibotSchema, type ValibotValidateConfig, type ValibotValidateOn, createValibotValidator };