Getting started: NestJS
Try it first: the playground with NestJS runs this backend live, with React, Angular, Vue or a plain form.
Write cross-field rules once with the TypeScript builders. A class decorator carries them, a pipe enforces them and a post-processor publishes them in the @nestjs/swagger document. Needs Node 20+ and @nestjs/common and @nestjs/swagger 11 or later.
1. Install
npm install validonce @nestjs/common @nestjs/core @nestjs/platform-express @nestjs/swagger reflect-metadata rxjs
2. Write the rules and the class
import { Body, Controller, Post } from "@nestjs/common";
import { ApiBody, ApiProperty } from "@nestjs/swagger";
import { defineRules, fieldRefs, when } from "validonce/rules";
import { ValidonceRules, ValidonceValidationPipe } from "validonce/nestjs";
const fields = ["plan", "company", "email", "start", "end", "quantity"] as const;
const F = fieldRefs(...fields);
const rules = defineRules(
"Signup",
[
when(F.plan.eq("pro")).require(F.company, { key: "rules.company_required" }),
F.end.ge(F.start, { key: "rules.end_after_start", type: "date" }),
F.quantity.ge(1, { key: "rules.quantity_min", params: { min: 1 }, type: "number" }),
],
{ fields },
);
@ValidonceRules(rules)
export class Signup {
@ApiProperty({ enum: ["free", "pro"] }) plan!: "free" | "pro";
@ApiProperty({ nullable: true, required: false }) company?: string | null;
@ApiProperty({ minLength: 1 }) email!: string;
@ApiProperty({ format: "date" }) start!: string;
@ApiProperty({ format: "date", nullable: true, required: false }) end?: string | null;
@ApiProperty({ type: Number }) quantity!: number;
}
@Controller("signups")
export class SignupsController {
@Post()
@ApiBody({ type: Signup })
create(@Body(new ValidonceValidationPipe(Signup)) body: Signup) {
return { ok: true };
}
}
Then in main.ts:
import { SwaggerModule, DocumentBuilder } from "@nestjs/swagger";
import { addValidonceToDocument, ValidonceExceptionFilter } from "validonce/nestjs";
app.useGlobalFilters(new ValidonceExceptionFilter()); // optional: application/problem+json
const document = addValidonceToDocument(
SwaggerModule.createDocument(app, new DocumentBuilder().setTitle("API").build()),
[Signup],
);
SwaggerModule.setup("docs", app, document);
Other builders: atLeastOneOf, exactlyOneOf, allOrNone, and the condition helpers allOf, anyOf, not. Each takes an options object with key, and optionally params, id, server_only and path.
3. What you get
The document has the rules under components.schemas.Signup["x-validonce"]. A request that breaks a rule gets 422 problem details with a top-level validonce member (see Transport). One response lists every failing rule. In the browser, issuesFromProblemDetails or issuesFromResponse from validonce/server-errors turns the body back into issues.
Gotchas
- Two pipes, in this order. Nest's
ValidationPipe(class-validator decorators on the class) goes first and answers 400 for a bad field;ValidonceValidationPipegoes second and answers 422 for broken rules, which assume well-typed fields. As globals:app.useGlobalPipes(new ValidationPipe(), new ValidonceValidationPipe()); per parameter:@Body(new ValidationPipe(), new ValidonceValidationPipe(Signup)). See examples/nestjs-app for the esbuild variant. new ValidonceValidationPipe(Signup)names the class. WithemitDecoratorMetadata(tsc, SWC) a globalnew ValidonceValidationPipe()finds it from the parameter type; esbuild and Vite emit no metadata, so name it. Likewise those builds need@ApiProperty({ type: … })and@ApiBody({ type }).- Startup errors.
defineRulesthrowsValidonceDefinitionErrorfor an unknown field (when you passfields), a duplicate id or an unsupported compare type;addValidonceToDocumentthrows when a class has no rules or no schema in the document. - Schema name is the class name, so
@ValidonceRulesonSignuplands oncomponents.schemas.Signup. - Without
ValidonceExceptionFilterthe 422 body is the same but its content type isapplication/json.
Migrate with an agent
If your cross-field checks are class-validator decorators or custom validators on a DTO, an agent can move them. The class-validator guide maps each check to @ValidonceRules and ValidonceValidationPipe, and generate_code with target typescript-rules and framework: "nestjs" writes the DTO and rules. The old DTO runs on generated inputs through the Node runner's class-validator kind. These need validonce-mcp 0.2.0 or later. See Migrating with an agent.
Next: the React guide or Angular guide. Runnable example: examples/nestjs-app.