1010 Single source of truth · Drop‑in for NestJS · Fast by design
1111</p >
1212
13- [](https://deepwiki.com/ts-oas/nest-openapi)
14- ![ GitHub License] ( https://img.shields.io/github/license/ts-oas/nest-openapi )
15-
16- ## Overview
17-
18- ` @nest-openapi ` is a modern, modular set of utilities for building OpenAPI‑driven NestJS apps.
19-
20- - ** Single Source of Truth** — The OpenAPI spec is the contract; validation and serialization derive from it.
21- - ** Drop‑in for NestJS** — Works with existing controllers and routes.
22- - ** Fast by Design** — AJV validation and fast-json-stringify serialization with caching and precompilation.
23- - ** Express & Fastify** — Platform‑agnostic support.
24- - ** Fine‑Grained Control** — Per‑route opt‑out and overrides.
25-
26- ### Packages
27-
28- - [ ** ` @nest-openapi/validator ` ** ] ( https://nest-openapi.github.io/validator/ ) — Automatic request/response validation using your OpenAPI specification.
13+ <p align =" center " >
14+ <a href =" https://deepwiki.com/ts-oas/nest-openapi " >
15+ <img src="https://img.shields.io/badge/DeepWiki-ts--oas%2Fnest--openapi-blue.svg?color=teal&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAyCAYAAAAnWDnqAAAAAXNSR0IArs4c6QAAA05JREFUaEPtmUtyEzEQhtWTQyQLHNak2AB7ZnyXZMEjXMGeK/AIi+QuHrMnbChYY7MIh8g01fJoopFb0uhhEqqcbWTp06/uv1saEDv4O3n3dV60RfP947Mm9/SQc0ICFQgzfc4CYZoTPAswgSJCCUJUnAAoRHOAUOcATwbmVLWdGoH//PB8mnKqScAhsD0kYP3j/Yt5LPQe2KvcXmGvRHcDnpxfL2zOYJ1mFwrryWTz0advv1Ut4CJgf5uhDuDj5eUcAUoahrdY/56ebRWeraTjMt/00Sh3UDtjgHtQNHwcRGOC98BJEAEymycmYcWwOprTgcB6VZ5JK5TAJ+fXGLBm3FDAmn6oPPjR4rKCAoJCal2eAiQp2x0vxTPB3ALO2CRkwmDy5WohzBDwSEFKRwPbknEggCPB/imwrycgxX2NzoMCHhPkDwqYMr9tRcP5qNrMZHkVnOjRMWwLCcr8ohBVb1OMjxLwGCvjTikrsBOiA6fNyCrm8V1rP93iVPpwaE+gO0SsWmPiXB+jikdf6SizrT5qKasx5j8ABbHpFTx+vFXp9EnYQmLx02h1QTTrl6eDqxLnGjporxl3NL3agEvXdT0WmEost648sQOYAeJS9Q7bfUVoMGnjo4AZdUMQku50McDcMWcBPvr0SzbTAFDfvJqwLzgxwATnCgnp4wDl6Aa+Ax283gghmj+vj7feE2KBBRMW3FzOpLOADl0Isb5587h/U4gGvkt5v60Z1VLG8BhYjbzRwyQZemwAd6cCR5/XFWLYZRIMpX39AR0tjaGGiGzLVyhse5C9RKC6ai42ppXPKiBagOvaYk8lO7DajerabOZP46Lby5wKjw1HCRx7p9sVMOWGzb/vA1hwiWc6jm3MvQDTogQkiqIhJV0nBQBTU+3okKCFDy9WwferkHjtxib7t3xIUQtHxnIwtx4mpg26/HfwVNVDb4oI9RHmx5WGelRVlrtiw43zboCLaxv46AZeB3IlTkwouebTr1y2NjSpHz68WNFjHvupy3q8TFn3Hos2IAk4Ju5dCo8B3wP7VPr/FGaKiG+T+v+TQqIrOqMTL1VdWV1DdmcbO8KXBz6esmYWYKPwDL5b5FA1a0hwapHiom0r/cKaoqr+27/XcrS5UwSMbQAAAABJRU5ErkJggg==" alt="DeepWiki" />
16+ </a >
17+ <a href =" https://github.com/ts-oas/nest-openapi/blob/main/LICENSE " >
18+ <img src="https://img.shields.io/github/license/ts-oas/nest-openapi" alt="License" />
19+ </a >
20+ </p >
2921
30- - [ ** ` @nest-openapi/serializer ` ** ] ( https://nest-openapi.github.io/serializer/ ) — High‑performance response serialization based on your OpenAPI 3.x specification.
22+ ---
3123
32- - [ ** ` @nest-openapi/mock ` ** ] ( https://nest-openapi.github.io/mock/ ) — Spec-driven mock server for generating realistic mock responses from your OpenAPI specification.
24+ ## Features
3325
34- ---
26+ - ** 🎯 Single Source of Truth** — Your OpenAPI spec drives validation, serialization, and mocking.
27+ - ** ⚡ Fast by Design** — AJV validation and ` fast-json-stringify ` serialization with caching and precompilation.
28+ - ** 🔌 Drop-in Integration** — Works with existing NestJS controllers and routes
29+ - ** 🎛️ Fine-Grained Control** — Per-route opt-out and custom schema overrides
30+ - ** 🚀 Platform Agnostic** — Supports both Express and Fastify adapters
3531
36- ## Get Started
32+ ## Packages
3733
38- ### @nest-openapi/validator
34+ | Package | Description | Version | Docs |
35+ | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- |
36+ | [ ` @nest-openapi/validator ` ] ( https://www.npmjs.com/package/@nest-openapi/validator ) | Automatic request/response validation using your OpenAPI spec | [ ![ npm] ( https://img.shields.io/npm/v/@nest-openapi/validator.svg )] ( https://www.npmjs.com/package/@nest-openapi/validator ) | [ 📖 Docs] ( https://nest-openapi.github.io/validator/ ) |
37+ | [ ` @nest-openapi/serializer ` ] ( https://www.npmjs.com/package/@nest-openapi/serializer ) | High-performance response serialization based on your OpenAPI spec | [ ![ npm] ( https://img.shields.io/npm/v/@nest-openapi/serializer.svg )] ( https://www.npmjs.com/package/@nest-openapi/serializer ) | [ 📖 Docs] ( https://nest-openapi.github.io/serializer/ ) |
38+ | [ ` @nest-openapi/mock ` ] ( https://www.npmjs.com/package/@nest-openapi/mock ) | Spec-driven mock server for generating realistic mock responses | [ ![ npm] ( https://img.shields.io/npm/v/@nest-openapi/mock.svg )] ( https://www.npmjs.com/package/@nest-openapi/mock ) | [ 📖 Docs] ( https://nest-openapi.github.io/mock/ ) |
3939
40- [ ![ NPM – validator] ( https://img.shields.io/npm/v/%40nest-openapi%2Fvalidator.svg )] ( https://www.npmjs.com/package/%40nest-openapi%2Fvalidator )
41- ![ NPM Unpacked Size] ( https://img.shields.io/npm/unpacked-size/%40nest-openapi%2Fvalidator.svg )
40+ ## Quick Start
4241
43- Install:
42+ ### Validator
4443
4544``` bash
4645npm i @nest-openapi/validator
4746```
4847
49- Minimal setup:
50-
5148``` typescript
52- // app.module.ts
5349import { Module } from " @nestjs/common" ;
5450import { OpenAPIValidatorModule } from " @nest-openapi/validator" ;
5551import * as openApiSpec from " ./openapi.json" ;
@@ -64,23 +60,15 @@ import * as openApiSpec from "./openapi.json";
6460export class AppModule {}
6561```
6662
67- All routes are automatically validated. ** For advanced configuration, see [ the docs ] ( https://nest-openapi.github.io/validator/ ) ** .
63+ ** All routes are automatically validated.** See [ full documentation ] ( https://nest-openapi.github.io/validator/ ) for advanced configuration .
6864
69- ### @nest-openapi/serializer
70-
71- [ ![ NPM – serializer] ( https://img.shields.io/npm/v/%40nest-openapi%2Fserializer.svg )] ( https://www.npmjs.com/package/%40nest-openapi%2Fserializer )
72- ![ NPM Unpacked Size] ( https://img.shields.io/npm/unpacked-size/%40nest-openapi%2Fserializer.svg )
73-
74- Install:
65+ ### Serializer
7566
7667``` bash
7768npm i @nest-openapi/serializer
7869```
7970
80- Minimal setup:
81-
8271``` typescript
83- // app.module.ts
8472import { Module } from " @nestjs/common" ;
8573import { OpenAPISerializerModule } from " @nest-openapi/serializer" ;
8674import * as openApiSpec from " ./openapi.json" ;
@@ -96,23 +84,15 @@ import * as openApiSpec from "./openapi.json";
9684export class AppModule {}
9785```
9886
99- Successful responses are automatically serialized. ** For advanced configuration, see [ the docs ] ( https://nest-openapi.github.io/serializer/ ) ** .
87+ ** Responses are automatically serialized.** See [ full documentation ] ( https://nest-openapi.github.io/serializer/ ) for advanced configuration .
10088
101- ### @nest-openapi/mock
102-
103- [ ![ NPM – mock] ( https://img.shields.io/npm/v/%40nest-openapi%2Fmock.svg )] ( https://www.npmjs.com/package/%40nest-openapi%2Fmock )
104- ![ NPM Unpacked Size] ( https://img.shields.io/npm/unpacked-size/%40nest-openapi%2Fmock.svg )
105-
106- Install:
89+ ### Mock
10790
10891``` bash
10992npm i @nest-openapi/mock
11093```
11194
112- Minimal setup:
113-
11495``` typescript
115- // app.module.ts
11696import { Module } from " @nestjs/common" ;
11797import { OpenAPIMockModule } from " @nest-openapi/mock" ;
11898import * as openApiSpec from " ./openapi.json" ;
@@ -123,16 +103,58 @@ import * as openApiSpec from "./openapi.json";
123103 specSource: { type: " object" , spec: openApiSpec },
124104 enable: process .env .NODE_ENV === " development" ,
125105 mockByDefault: true ,
126- strategyOrder: [" examples" , " jsf" ],
127106 }),
128107 ],
129108})
130109export class AppModule {}
131110```
132111
133- Routes return mocked responses when enabled. ** For advanced configuration, see [ the docs ] ( https://nest-openapi.github.io/mock/ ) ** .
112+ ** Routes return mocked responses when enabled.** See [ full documentation ] ( https://nest-openapi.github.io/mock/ ) for advanced configuration .
134113
135- ---
114+ ## Usage Examples
115+
116+ ### Manual Validation
117+
118+ ``` typescript
119+ import { Inject , Injectable } from " @nestjs/common" ;
120+ import {
121+ OPENAPI_VALIDATOR ,
122+ OpenAPIValidatorService ,
123+ } from " @nest-openapi/validator" ;
124+
125+ @Injectable ()
126+ export class MyService {
127+ constructor (
128+ @Inject (OPENAPI_VALIDATOR )
129+ private readonly validator : OpenAPIValidatorService
130+ ) {}
131+
132+ validate(ctx : HttpArgumentsHost ) {
133+ const errors = this .validator .validateRequest (ctx , { body: true });
134+ if (errors .length > 0 ) {
135+ // Handle validation errors
136+ }
137+ }
138+ }
139+ ```
140+
141+ ### Per-Route Overrides
142+
143+ ``` typescript
144+ import { Controller , Post } from " @nestjs/common" ;
145+ import { Validate } from " @nest-openapi/validator" ;
146+ import { Serialize } from " @nest-openapi/serializer" ;
147+
148+ @Controller (" books" )
149+ export class BooksController {
150+ @Post ()
151+ @Validate ({ request: { query: false }, response: true })
152+ @Serialize ({ disable: true })
153+ create(@Body () dto : CreateBookDto ): Book {
154+ return this .booksService .create (dto );
155+ }
156+ }
157+ ```
136158
137159## Compatibility
138160
@@ -145,4 +167,4 @@ Issues and PRs are welcome. Please check the package folders and docs before ope
145167
146168## License
147169
148- MIT © ` @nest-openapi `
170+ MIT © [ @nest-openapi ] ( https://github.com/ts-oas/nest-openapi )
0 commit comments