Aviary
Validation

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.

src/generated/forms.ts (zod)
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.

src/company/company-roster.dto.ts
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([]),
});
src/company/onboard-company.controller.ts
@Post()
onboard(@Body({ schema: onboardCompanyBody }) body: z.infer<typeof onboardCompanyBody>) {}
src/generated/forms.ts
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.ts imports 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.ts is 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.
  • formSchemas maps the route to its body schema, as for any other entry.
  • Watch mode regenerates when a watched controller (or a forms.watch file) changes. Editing the schema itself needs no regeneration, since forms.ts re-exports it. Renaming or un-exporting it in a module outside those globs shows up on the next pass or the next nestjs-codegen codegen run.

On this page