@nest-openapi/validator
v0.1.3
Published
Automatic request/response validation for NestJS using your OpenAPI specifications
Maintainers
Readme
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
npm i @nest-openapi/validatorQuick Start
// app.module.ts
import { Module } from "@nestjs/common";
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.
Per-route control (skip/override)
Use @Validate to enable/disable validation per-route:
import { Controller, Post, Get, Body, Param } from "@nestjs/common";
import { Validate } from "@nest-openapi/validator";
@Controller("users")
export class UsersController {
@Post()
@Validate({ request: false }) // Skip request validation for this route
createUser(@Body() userData: any) {
return this.usersService.create(userData);
}
@Get(":id")
@Validate({ request: { param: false }, response: false }) // Skip param and response validation for this route
getUser(@Param("id") id: string) {
return this.usersService.findById(id);
}
}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
