{"version":3,"file":"zod-resolver.cjs","names":[],"sources":["../../src/forms/zod-resolver.ts"],"sourcesContent":["import type {\n    FieldErrors,\n    FieldValues,\n    Resolver,\n    ResolverOptions,\n    ResolverResult,\n} from \"react-hook-form\";\nimport type { z } from \"zod\";\n\ninterface ResolverError {\n    type: string;\n    message: string;\n}\n\n/**\n * Minimal `react-hook-form` resolver built on top of zod. Mirrors the shape\n * produced by `@hookform/resolvers/zod` so it can be passed straight to\n * `useForm({ resolver })`.\n *\n * Typed with react-hook-form's own `Resolver`, not a local look-alike: a\n * structurally similar type of our own compiled fine here and was then rejected\n * at the `useForm({ resolver })` call site — the only place a resolver is ever\n * used — which is why `useZodForm` had to cast its way past it.\n *\n * Of the resolver options only `criteriaMode` is read; the rest of\n * `ResolverOptions` is accepted and ignored, because react-hook-form always\n * passes the whole object.\n *\n * @example\n * const form = useForm<LoginForm>({ resolver: zodResolver(loginSchema) });\n */\nexport function zodResolver<TSchema extends z.ZodTypeAny>(\n    schema: TSchema,\n): Resolver<z.infer<TSchema> & FieldValues> {\n    type Values = z.infer<TSchema> & FieldValues;\n\n    return async (\n        values: Values,\n        _context: unknown,\n        options: ResolverOptions<Values>,\n    ): Promise<ResolverResult<Values>> => {\n        const result = schema.safeParse(values);\n        if (result.success) {\n            return { values: result.data as Values, errors: {} };\n        }\n\n        const errors: Record<string, ResolverError | object> = {};\n        const criteriaMode = options.criteriaMode ?? \"firstError\";\n\n        for (const issue of result.error.issues) {\n            const path = issue.path.length === 0 ? \"_root\" : issue.path.join(\".\");\n            if (criteriaMode === \"firstError\" && errors[path]) continue;\n            errors[path] = { type: issue.code, message: issue.message };\n        }\n\n        return { values: {}, errors: errors as FieldErrors<Values> };\n    };\n}\n"],"mappings":"AA+BA,SAAgB,EACZ,EACwC,CAGxC,OAAO,MACH,EACA,EACA,IACkC,CAClC,IAAM,EAAS,EAAO,UAAU,CAAM,EACtC,GAAI,EAAO,QACP,MAAO,CAAE,OAAQ,EAAO,KAAgB,OAAQ,CAAC,CAAE,EAGvD,IAAM,EAAiD,CAAC,EAClD,EAAe,EAAQ,cAAgB,aAE7C,IAAK,IAAM,KAAS,EAAO,MAAM,OAAQ,CACrC,IAAM,EAAO,EAAM,KAAK,SAAW,EAAI,QAAU,EAAM,KAAK,KAAK,GAAG,EAChE,IAAiB,cAAgB,EAAO,KAC5C,EAAO,GAAQ,CAAE,KAAM,EAAM,KAAM,QAAS,EAAM,OAAQ,EAC9D,CAEA,MAAO,CAAE,OAAQ,CAAC,EAAW,QAA8B,CAC/D,CACJ"}