#zod: sharing schemas with the front-end
1 messages · Page 1 of 1 (latest)
According to the docs nestjs-zod/z is soon to be deprecated. We have been using just plain zod schmas for the original zod library, and that have been working create. It is possible to combine this with all the other helper functions and decorators from nest-zod
both can use the standard zod lib
only difficulty is sharing the schemas, one way could be to have a monorepo setup where you have some shared space for frontend and backend. others could be an extra repo or package both can depend on
hey @sage surge I am sharing zod schemas across frontend and backend in my project. I have a monorepo setup with 3 packages:
- backend
- frontend
- common
In the common package I define the zod schemas using the zod package:
import { z } from 'zod';
export const zodBaseNodeAction = z.object({
type: z.string(),
payload: z.object({
id: z.string(),
}),
});
export type ZodBaseNodeAction = z.infer<typeof zodBaseNodeAction>;
In the frontend and backend I import the ZodBaseNodeAction interface. If you want to use the nestjs-zod/z package to create DTOs you should be able to do something like this:
import { createZodDto } from 'nestjs-zod';
import { ZodBaseNodeAction } from '@namespace/common`;
// class is required for using DTO as a type
class ZodBaseNodeActionDto extends createZodDto(ZodBaseNodeAction) {}
I am using a simple NPM workspace setup for the monorepo