Forms
forms.ts — a validation schema per validated endpoint.
For every route whose @Body()/@Query() is a validated DTO, a defineContract
schema, or a zod schema on the parameter, the codegen emits a schema into forms.ts, plus a formSchemas map keyed by
route name.
import { z } from 'zod';
export const LoginBodySchema = z.object({
email: z.string().email(),
password: z.string().min(8),
});
export type LoginBody = z.infer<typeof LoginBodySchema>;
export const formSchemas = {
'auth.login': LoginBodySchema,
} as const;Using it with react-hook-form
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { LoginBodySchema, type LoginBody } from '../generated/forms';
import { api } from '../lib/api';
import { useMutation } from '@tanstack/react-query';
function LoginForm() {
const form = useForm<LoginBody>({ resolver: zodResolver(LoginBodySchema) });
const login = useMutation(api.auth.login().mutationOptions());
return (
<form onSubmit={form.handleSubmit((body) => login.mutate({ body }))}>
<input {...form.register('email')} />
<input type="password" {...form.register('password')} />
</form>
);
}The same schema validates on the client and matches what the server enforces — one source of truth, derived from your DTO.
Switch the validation lib in config and forms.ts is rendered
with that library instead. See Pluggable validation.
Zod schemas on the parameter
A route that validates with a zod schema on the parameter decorator, the NestJS 12 way
(@Body({ schema }), @Query({ schema })) or through a schema pipe
(@Body(new ZodPipe(schema))), gets its entry too. The schema is not translated: forms.ts
re-exports it from the module that declares it, so refinements, transforms, defaults and
custom messages are the very ones the server runs.
import { z } from 'zod';
export const onboardCompanyBody = z.object({
name: z.string().trim().min(1, 'Company name is required'),
people: z.array(z.object({ name: z.string(), email: z.email() })).default([]),
});@Post()
onboard(@Body({ schema: onboardCompanyBody }) body: z.infer<typeof onboardCompanyBody>) {}import { z } from 'zod';
import { onboardCompanyBody } from '../company/company-roster.dto';
export const OnboardBodySchema = onboardCompanyBody;
export type OnboardBody = z.infer<typeof OnboardBodySchema>;The import is relative to the output directory, so it resolves in any front-end bundle that can reach your source tree, whatever its tsconfig or path aliases.
- The schema module must be browser-safe. The front end now loads it, and everything it imports. Keep request schemas in a module that imports zod and other browser-safe code only, never services, the database layer or Node built-ins.
- The schema must be an exported
const, or a member of one (contract.body), declared in a module with no@Controller. It may be reached through an import alias, a barrel or a namespace import (ns.schema), and the shorthand{ schema }works;forms.tsimports it from the declaring module under the name that module exports it as. A schema that is not exported, is only a default export, or is declared next to a controller is left out with a// warning:line naming it. An inline schema (@Body({ schema: z.object(...) })) or one built by a call (base.extend({ ... })) has no name to import and is left out silently. - Zod adapter only. Re-exporting a zod schema only makes sense when
forms.tsis zod. Under another adapter the route gets a// warning:line and no export. A schema from another library (valibot, arktype) on the parameter is not exported. formSchemasmaps the route to its body schema, as for any other entry.- Watch mode regenerates when a watched controller (or a
forms.watchfile) changes. Editing the schema itself needs no regeneration, sinceforms.tsre-exports it. Renaming or un-exporting it in a module outside those globs shows up on the next pass or the nextnestjs-codegen codegenrun.