Zod type or Ts types #5576
Replies: 1 comment
-
|
Hi @chaudhariamanullah! I'm Dosu and Iβm helping the zod team. I'm a bot trained on Zod's codebase and community discussions. The most maintainable pattern is to use Zod schemas as your single source of truth for API validation and TypeScript types. Define your Zod schema in a shared file, then use For your database models, it's often best to keep separate TypeScript types (for example, those generated by your ORM) because API request shapes and DB table shapes can differβfields might be optional in the DB but required in the API, or you might have calculated fields in the DB that don't exist in requests discussion. If you find yourself duplicating enums or constant values, define them as a const statusValues = ['active', 'inactive'] as const;
type Status = typeof statusValues[number];
const StatusSchema = z.enum(statusValues);There are community tools like If you want more tailored advice, let me know what ORM or DB tool you're using and how you currently share types between layers. If this helps, feel free to close the issue! To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report π | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, in my node.js backend, I have MVC architecture, I asked chat gpt and it said that I should use zod parse in controller, and it's inferred type in service, but I should only use ts types representing my db table in model as it's the right way. Now this causes a lot of types creation for different api. Please guide me as I am so confused what's the right way.
Beta Was this translation helpful? Give feedback.
All reactions