Overview
@nest-openapi/validator Automatically validates request/response using your OpenAPI 3.x spec.
- Spec‑driven — The OpenAPI spec is the contract; validation is generated from it.
- Fast by Design — AJV under the hood, with schema caching and optional pre-compilation.
- Full Coverage — Validates request body, route params, query strings, and responses.
- NestJS‑native & Platform Agnostic — Auto validates with per‑route opt‑out and overrides (
@Validate()) and fully compatible with both Express and Fastify adapters.
Install
bash
npm i @nest-openapi/validatorQuick Start
typescript
// app.module.ts
import { OpenAPIValidatorModule } from "@nest-openapi/validator";
import * as openApiSpec from "./openapi.json";
@Module({
imports: [
OpenAPIValidatorModule.forRoot({
specSource: { type: "object", spec: openApiSpec },
}),
],
})
export class AppModule {}That's it! All routes automatically validated.
Next Steps
- Advanced setup — Custom AJV instances, formats, and failure handlers
- Options — Full configuration reference
- Decorators — Per-route validation overrides
- Manual Validation — Programmatic request/response validation
