@nest-openapi/validator 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +194 -111
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +26 -28
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 nest-openapi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,111 +1,194 @@
|
|
|
1
|
-
<p align="center">
|
|
2
|
-
<img src="./docs/public/nest-openapi-logo.png" alt="nest-openapi-logo" height="84" />
|
|
3
|
-
</p>
|
|
4
|
-
|
|
5
|
-
<h1 align="center">@nest-openapi</h1>
|
|
6
|
-
|
|
7
|
-
<p align="center"><strong>OpenAPI‑first utilities for NestJS</strong></p>
|
|
8
|
-
|
|
9
|
-
<p align="center">
|
|
10
|
-
Single source of truth · Drop‑in for NestJS · Fast by design
|
|
11
|
-
</p>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
[ | Automatic request/response validation using your OpenAPI spec | [](https://www.npmjs.com/package/@nest-openapi/validator) |
|
|
35
|
+
| [`@nest-openapi/serializer`](https://nest-openapi.github.io/serializer/) | High-performance response serialization based on your OpenAPI spec | [](https://www.npmjs.com/package/@nest-openapi/serializer) |
|
|
36
|
+
| [`@nest-openapi/mock`](https://nest-openapi.github.io/mock/) | Spec-driven mock server for generating realistic mock responses | [](https://www.npmjs.com/package/@nest-openapi/mock) |
|
|
37
|
+
| [`@nest-openapi/mcp`](https://nest-openapi.github.io/mcp/) | Spec-driven MCP server for exposing OpenAPI operations as tools | [](https://www.npmjs.com/package/@nest-openapi/mcp) |
|
|
38
|
+
|
|
39
|
+
## Quick Start
|
|
40
|
+
|
|
41
|
+
### Validator
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm i @nest-openapi/validator
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { Module } from "@nestjs/common";
|
|
49
|
+
import { OpenAPIValidatorModule } from "@nest-openapi/validator";
|
|
50
|
+
import * as openApiSpec from "./openapi.json";
|
|
51
|
+
|
|
52
|
+
@Module({
|
|
53
|
+
imports: [
|
|
54
|
+
OpenAPIValidatorModule.forRoot({
|
|
55
|
+
specSource: { type: "object", spec: openApiSpec },
|
|
56
|
+
}),
|
|
57
|
+
],
|
|
58
|
+
})
|
|
59
|
+
export class AppModule {}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**All routes are automatically validated.** See [full documentation](https://nest-openapi.github.io/validator/) for advanced configuration.
|
|
63
|
+
|
|
64
|
+
### Serializer
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
npm i @nest-openapi/serializer
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { Module } from "@nestjs/common";
|
|
72
|
+
import { OpenAPISerializerModule } from "@nest-openapi/serializer";
|
|
73
|
+
import * as openApiSpec from "./openapi.json";
|
|
74
|
+
|
|
75
|
+
@Module({
|
|
76
|
+
imports: [
|
|
77
|
+
OpenAPISerializerModule.forRoot({
|
|
78
|
+
specSource: { type: "object", spec: openApiSpec },
|
|
79
|
+
responseSerialization: { enable: true, skipErrorResponses: true },
|
|
80
|
+
}),
|
|
81
|
+
],
|
|
82
|
+
})
|
|
83
|
+
export class AppModule {}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**Responses are automatically serialized.** See [full documentation](https://nest-openapi.github.io/serializer/) for advanced configuration.
|
|
87
|
+
|
|
88
|
+
### Mock
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
npm i @nest-openapi/mock
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { Module } from "@nestjs/common";
|
|
96
|
+
import { OpenAPIMockModule } from "@nest-openapi/mock";
|
|
97
|
+
import * as openApiSpec from "./openapi.json";
|
|
98
|
+
|
|
99
|
+
@Module({
|
|
100
|
+
imports: [
|
|
101
|
+
OpenAPIMockModule.forRoot({
|
|
102
|
+
specSource: { type: "object", spec: openApiSpec },
|
|
103
|
+
enable: process.env.NODE_ENV === "development",
|
|
104
|
+
mockByDefault: true,
|
|
105
|
+
}),
|
|
106
|
+
],
|
|
107
|
+
})
|
|
108
|
+
export class AppModule {}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**Routes return mocked responses when enabled.** See [full documentation](https://nest-openapi.github.io/mock/) for advanced configuration.
|
|
112
|
+
|
|
113
|
+
### MCP
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
npm i @nest-openapi/mcp
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { Module } from "@nestjs/common";
|
|
121
|
+
import { OpenAPIMcpModule } from "@nest-openapi/mcp";
|
|
122
|
+
import * as openApiSpec from "./openapi.json";
|
|
123
|
+
|
|
124
|
+
@Module({
|
|
125
|
+
imports: [
|
|
126
|
+
OpenAPIMcpModule.forRoot({
|
|
127
|
+
specSource: { type: "object", spec: openApiSpec },
|
|
128
|
+
http: { path: "/mcp" },
|
|
129
|
+
executor: { baseUrl: "http://127.0.0.1:3000" },
|
|
130
|
+
}),
|
|
131
|
+
],
|
|
132
|
+
})
|
|
133
|
+
export class AppModule {}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**Expose OpenAPI operations as MCP tools.** See [full documentation](https://nest-openapi.github.io/mcp/) for advanced configuration.
|
|
137
|
+
|
|
138
|
+
## Usage Examples
|
|
139
|
+
|
|
140
|
+
### Manual Validation
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
144
|
+
import {
|
|
145
|
+
OPENAPI_VALIDATOR,
|
|
146
|
+
OpenAPIValidatorService,
|
|
147
|
+
} from "@nest-openapi/validator";
|
|
148
|
+
|
|
149
|
+
@Injectable()
|
|
150
|
+
export class MyService {
|
|
151
|
+
constructor(
|
|
152
|
+
@Inject(OPENAPI_VALIDATOR)
|
|
153
|
+
private readonly validator: OpenAPIValidatorService,
|
|
154
|
+
) {}
|
|
155
|
+
|
|
156
|
+
validate(ctx: HttpArgumentsHost) {
|
|
157
|
+
const errors = this.validator.validateRequest(ctx, { body: true });
|
|
158
|
+
if (errors.length > 0) {
|
|
159
|
+
// Handle validation errors
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Per-Route Overrides
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import { Controller, Post } from "@nestjs/common";
|
|
169
|
+
import { Validate } from "@nest-openapi/validator";
|
|
170
|
+
import { Serialize } from "@nest-openapi/serializer";
|
|
171
|
+
|
|
172
|
+
@Controller("books")
|
|
173
|
+
export class BooksController {
|
|
174
|
+
@Post()
|
|
175
|
+
@Validate({ request: { query: false }, response: true })
|
|
176
|
+
@Serialize({ disable: true })
|
|
177
|
+
create(@Body() dto: CreateBookDto): Book {
|
|
178
|
+
return this.booksService.create(dto);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Compatibility
|
|
184
|
+
|
|
185
|
+
- Works with NestJS v9+
|
|
186
|
+
- Supports Express and Fastify adapters
|
|
187
|
+
|
|
188
|
+
## Contributing
|
|
189
|
+
|
|
190
|
+
Issues and PRs are welcome. Please check the package folders and docs before opening an issue.
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
MIT © [@nest-openapi](https://github.com/ts-oas/nest-openapi)
|
package/dist/index.cjs
CHANGED
|
@@ -301,7 +301,7 @@ ${JSON.stringify(data, null, 2)}`);
|
|
|
301
301
|
});
|
|
302
302
|
continue;
|
|
303
303
|
}
|
|
304
|
-
if (
|
|
304
|
+
if (param.schema) {
|
|
305
305
|
const schema = this.runtime.schemaResolver.resolveSchema(param.schema);
|
|
306
306
|
if (schema) {
|
|
307
307
|
const paramValue = { value };
|
package/dist/index.js
CHANGED
|
@@ -265,7 +265,7 @@ ${JSON.stringify(data, null, 2)}`);
|
|
|
265
265
|
});
|
|
266
266
|
continue;
|
|
267
267
|
}
|
|
268
|
-
if (
|
|
268
|
+
if (param.schema) {
|
|
269
269
|
const schema = this.runtime.schemaResolver.resolveSchema(param.schema);
|
|
270
270
|
if (schema) {
|
|
271
271
|
const paramValue = { value };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nest-openapi/validator",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Automatic request/response validation for NestJS using your OpenAPI specifications",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"keywords": [
|
|
@@ -13,6 +13,19 @@
|
|
|
13
13
|
"typescript",
|
|
14
14
|
"schema"
|
|
15
15
|
],
|
|
16
|
+
"homepage": "https://nest-openapi.github.io/",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/ts-oas/nest-openapi.git"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"author": "Alireza Malek",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/ts-oas/nest-openapi/issues"
|
|
28
|
+
},
|
|
16
29
|
"type": "module",
|
|
17
30
|
"main": "./dist/index.cjs",
|
|
18
31
|
"types": "./dist/index.d.ts",
|
|
@@ -27,29 +40,6 @@
|
|
|
27
40
|
"README.md",
|
|
28
41
|
"LICENSE"
|
|
29
42
|
],
|
|
30
|
-
"scripts": {
|
|
31
|
-
"build": "tsup src/index.ts --format esm,cjs --dts --target node20 --outDir dist --clean --external rxjs --external ajv --external @nestjs/common --external @nestjs/core",
|
|
32
|
-
"test": "jest",
|
|
33
|
-
"test:watch": "jest --watch",
|
|
34
|
-
"test:cov": "jest --coverage",
|
|
35
|
-
"prepack": "node -e \"const fs=require('fs');const path=require('path');fs.copyFileSync(path.resolve(__dirname,'..','..','README.md'), path.resolve(__dirname,'README.md'));\"",
|
|
36
|
-
"postpack": "node -e \"const fs=require('fs');const path=require('path');try{fs.unlinkSync(path.resolve(__dirname,'README.md'))}catch(_){}\"",
|
|
37
|
-
"lint": "eslint .",
|
|
38
|
-
"lint:fix": "eslint . --fix"
|
|
39
|
-
},
|
|
40
|
-
"homepage": "https://nest-openapi.github.io/",
|
|
41
|
-
"repository": {
|
|
42
|
-
"type": "git",
|
|
43
|
-
"url": "https://github.com/ts-oas/nest-openapi.git"
|
|
44
|
-
},
|
|
45
|
-
"publishConfig": {
|
|
46
|
-
"access": "public"
|
|
47
|
-
},
|
|
48
|
-
"author": "Alireza Malek",
|
|
49
|
-
"license": "MIT",
|
|
50
|
-
"bugs": {
|
|
51
|
-
"url": "https://github.com/ts-oas/nest-openapi/issues"
|
|
52
|
-
},
|
|
53
43
|
"peerDependencies": {
|
|
54
44
|
"@nestjs/common": "^9.0.0 || ^10.0.0 || ^11.0.0",
|
|
55
45
|
"@nestjs/core": "^9.0.0 || ^10.0.0 || ^11.0.0",
|
|
@@ -62,12 +52,11 @@
|
|
|
62
52
|
}
|
|
63
53
|
},
|
|
64
54
|
"dependencies": {
|
|
65
|
-
"@nest-openapi/runtime": "^0.1.
|
|
55
|
+
"@nest-openapi/runtime": "^0.1.2",
|
|
66
56
|
"ajv": "^8.0.0",
|
|
67
57
|
"rxjs": "^7.2.0"
|
|
68
58
|
},
|
|
69
59
|
"devDependencies": {
|
|
70
|
-
"@nest-openapi/runtime": "workspace:^",
|
|
71
60
|
"@nestjs/common": "^11.0.0",
|
|
72
61
|
"@nestjs/core": "^11.0.0",
|
|
73
62
|
"@nestjs/platform-express": "^11.1.3",
|
|
@@ -82,7 +71,8 @@
|
|
|
82
71
|
"supertest": "^7.1.0",
|
|
83
72
|
"ts-jest": "^29.0.0",
|
|
84
73
|
"tsup": "^8.5.0",
|
|
85
|
-
"typescript": "^5.9.0"
|
|
74
|
+
"typescript": "^5.9.0",
|
|
75
|
+
"@nest-openapi/runtime": "^0.1.2"
|
|
86
76
|
},
|
|
87
77
|
"jest": {
|
|
88
78
|
"moduleFileExtensions": [
|
|
@@ -101,5 +91,13 @@
|
|
|
101
91
|
],
|
|
102
92
|
"coverageDirectory": "coverage",
|
|
103
93
|
"testEnvironment": "node"
|
|
94
|
+
},
|
|
95
|
+
"scripts": {
|
|
96
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --target node20 --outDir dist --clean --external rxjs --external ajv --external @nestjs/common --external @nestjs/core",
|
|
97
|
+
"test": "jest",
|
|
98
|
+
"test:watch": "jest --watch",
|
|
99
|
+
"test:cov": "jest --coverage",
|
|
100
|
+
"lint": "eslint .",
|
|
101
|
+
"lint:fix": "eslint . --fix"
|
|
104
102
|
}
|
|
105
|
-
}
|
|
103
|
+
}
|