Getting zod schema from client or contract #1298
-
|
I am working on contract first approach with OpenAPI handler. In frontend, is there anyway I can get the input schema (zod) so that I can use it in react-hook-form? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
Hi @ryantsangai! I'm Dosu and I’m helping the orpc team. You can't get the Zod schema from the client instance or from the contract JSON in the frontend—this is by design. Zod schemas are not serializable, so when you export your contract (for example, using The recommended way in a monorepo is to define your Zod schemas in a shared contract package and import them directly in both backend and frontend. That way, you can use the exact same schema for both API validation and react-hook-form integration reference. Example: // shared/contract.ts
import { z } from "zod";
export const CreatePostSchema = z.object({
title: z.string(),
content: z.string(),
});
// apps/frontend/CreatePostForm.tsx
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { CreatePostSchema } from "@workspace/contract";
const form = useForm({
resolver: zodResolver(CreatePostSchema),
defaultValues: { title: "", content: "" },
});If you can't use a shared package, you'd need to duplicate the schema or convert JSON Schema back to Zod (which is non-trivial and lossy). But in a monorepo, sharing the schema module is the best practice reference. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
Maybe this can help |
Beta Was this translation helpful? Give feedback.
contractnotclient