@nest-openapi/validator 0.1.0
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 +326 -0
- package/dist/index.cjs +636 -0
- package/dist/index.d.cts +178 -0
- package/dist/index.d.ts +178 -0
- package/dist/index.js +609 -0
- package/package.json +103 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import * as _nestjs_common from '@nestjs/common';
|
|
2
|
+
import { ExecutionContext, DynamicModule, OnApplicationBootstrap } from '@nestjs/common';
|
|
3
|
+
import Ajv, { ErrorObject, Options } from 'ajv';
|
|
4
|
+
import { SpecSource, OpenAPISpec, OpenApiRuntimeService } from '@nest-openapi/runtime';
|
|
5
|
+
import { HttpArgumentsHost } from '@nestjs/common/interfaces';
|
|
6
|
+
|
|
7
|
+
interface ValidationError extends ErrorObject {
|
|
8
|
+
validationType: 'path' | 'query' | 'body' | 'response';
|
|
9
|
+
}
|
|
10
|
+
interface ValidationErrorResponse {
|
|
11
|
+
message: string;
|
|
12
|
+
errors: ValidationError[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface ValidatorOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Provide your OpenAPI spec as an object, or point to it via URL or file path.
|
|
18
|
+
*
|
|
19
|
+
* Examples:
|
|
20
|
+
* ```
|
|
21
|
+
* { type: "object", spec: {...} }
|
|
22
|
+
* { type: "url", spec: "https://…" }
|
|
23
|
+
* { type: "file", spec: "./openapi.json" }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
specSource: SpecSource;
|
|
27
|
+
requestValidation?: {
|
|
28
|
+
/**
|
|
29
|
+
* Enable request validation
|
|
30
|
+
* @default true
|
|
31
|
+
*/
|
|
32
|
+
enable?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Transform request data after validation
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
37
|
+
transform?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Custom error handler for request validation failures.
|
|
40
|
+
*
|
|
41
|
+
* You can:
|
|
42
|
+
* - Transform errors and throw a custom exception
|
|
43
|
+
* - Log and Ignore errors (return without throwing)
|
|
44
|
+
*
|
|
45
|
+
* If not provided, throws `BadRequestException` with validation errors
|
|
46
|
+
*/
|
|
47
|
+
onValidationFailed?: (context: ExecutionContext, errors: ValidationError[]) => void | never;
|
|
48
|
+
};
|
|
49
|
+
responseValidation?: {
|
|
50
|
+
/**
|
|
51
|
+
* Enable response validation
|
|
52
|
+
* @default false
|
|
53
|
+
*/
|
|
54
|
+
enable?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Skip validation for error responses (4xx and 5xx status codes).
|
|
57
|
+
* @default true
|
|
58
|
+
*/
|
|
59
|
+
skipErrorResponses?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Custom error handler for response validation failures.
|
|
62
|
+
*
|
|
63
|
+
* You can:
|
|
64
|
+
* - Transform errors and throw a custom exception
|
|
65
|
+
* - Log and ignore errors (return without throwing)
|
|
66
|
+
*
|
|
67
|
+
* If not provided, warns and throws `InternalServerErrorException` without validation errors
|
|
68
|
+
*/
|
|
69
|
+
onValidationFailed?: (context: ExecutionContext, errors: ValidationError[]) => void | never;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Override the default ajv instance or configure it
|
|
73
|
+
*/
|
|
74
|
+
ajv?: Ajv | {
|
|
75
|
+
options?: Options;
|
|
76
|
+
configure?: (ajv: Ajv) => void;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Compile every request/parameter schema during application bootstrap.
|
|
80
|
+
* This removes the first-request latency at the cost of longer start-up time.
|
|
81
|
+
* @default false
|
|
82
|
+
*/
|
|
83
|
+
precompileSchemas?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Verbose logs for troubleshooting
|
|
86
|
+
* @default false
|
|
87
|
+
*/
|
|
88
|
+
debug?: boolean;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
declare class OpenApiValidatorModule {
|
|
92
|
+
/**
|
|
93
|
+
* Configure the OpenAPI validator module with static options
|
|
94
|
+
*/
|
|
95
|
+
static forRoot(options: ValidatorOptions): DynamicModule;
|
|
96
|
+
/**
|
|
97
|
+
* Configure the OpenAPI validator module with async options
|
|
98
|
+
*/
|
|
99
|
+
static forRootAsync(options: {
|
|
100
|
+
imports?: any[];
|
|
101
|
+
useFactory?: (...args: any[]) => ValidatorOptions | Promise<ValidatorOptions>;
|
|
102
|
+
inject?: any[];
|
|
103
|
+
}): DynamicModule;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
declare const OPENAPI_VALIDATOR = "OPENAPI_VALIDATOR";
|
|
107
|
+
declare class OpenApiValidatorService implements OnApplicationBootstrap {
|
|
108
|
+
private readonly runtime;
|
|
109
|
+
private readonly options;
|
|
110
|
+
private readonly logger;
|
|
111
|
+
private ajv;
|
|
112
|
+
openApiSpec: OpenAPISpec;
|
|
113
|
+
private debugLog;
|
|
114
|
+
constructor(runtime: OpenApiRuntimeService, options: ValidatorOptions);
|
|
115
|
+
onApplicationBootstrap(): Promise<void>;
|
|
116
|
+
private get isSpecLoaded();
|
|
117
|
+
get validationOptions(): ValidatorOptions;
|
|
118
|
+
/**
|
|
119
|
+
* Register all component schemas with AJV so it can resolve $ref references
|
|
120
|
+
*/
|
|
121
|
+
private registerComponentSchemas;
|
|
122
|
+
/**
|
|
123
|
+
* Walk the OpenAPI spec and eagerly compile all requestBody and parameter
|
|
124
|
+
* schemas. This is triggered when `options.performance.precompileSchemas === true` and
|
|
125
|
+
* eliminates runtime compilation overhead.
|
|
126
|
+
*/
|
|
127
|
+
private precompileSchemas;
|
|
128
|
+
private findOperation;
|
|
129
|
+
private validateWithSchema;
|
|
130
|
+
validateRequest(httpContext: HttpArgumentsHost, options?: {
|
|
131
|
+
body?: boolean;
|
|
132
|
+
params?: boolean;
|
|
133
|
+
query?: boolean;
|
|
134
|
+
}): ValidationError[];
|
|
135
|
+
private validateParameters;
|
|
136
|
+
validateResponse(httpContext: HttpArgumentsHost, statusCode: number, responseBody: any): ValidationError[];
|
|
137
|
+
private getParameterSchema;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
interface ValidateOptions {
|
|
141
|
+
request?: boolean | {
|
|
142
|
+
params?: boolean;
|
|
143
|
+
query?: boolean;
|
|
144
|
+
body?: boolean;
|
|
145
|
+
};
|
|
146
|
+
response?: boolean;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Decorator to configure validation behavior for a specific route
|
|
150
|
+
*
|
|
151
|
+
* @param options - Validation configuration options
|
|
152
|
+
* @param options.request - Request validation settings:
|
|
153
|
+
* - `true`: Validate all request parts (params, query, body)
|
|
154
|
+
* - `false`: Skip request validation entirely
|
|
155
|
+
* - `{ params?, query?, body? }`: Validate only specified request parts
|
|
156
|
+
* - `undefined`: Use default configuration
|
|
157
|
+
* @param options.response - Response validation settings:
|
|
158
|
+
* - `true`: Enable response validation
|
|
159
|
+
* - `false`: Skip response validation
|
|
160
|
+
* - `undefined`: Use default configuration
|
|
161
|
+
*
|
|
162
|
+
* @example In controller:
|
|
163
|
+
*
|
|
164
|
+
* ```typescript
|
|
165
|
+
* @Controller('users')
|
|
166
|
+
* export class UsersController {
|
|
167
|
+
* // Validate body and response
|
|
168
|
+
* @Put(':id')
|
|
169
|
+
* @Validate({ request: { body: true }, response: true })
|
|
170
|
+
* update(@Param('id') id: string, @Body() user: UpdateUserDto): User {
|
|
171
|
+
* return this.usersService.update(id, user);
|
|
172
|
+
* }
|
|
173
|
+
* }
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
declare const Validate: (options?: ValidateOptions) => _nestjs_common.CustomDecorator<string>;
|
|
177
|
+
|
|
178
|
+
export { OPENAPI_VALIDATOR, OpenApiValidatorModule, OpenApiValidatorService, Validate, type ValidateOptions, type ValidationError, type ValidationErrorResponse, type ValidatorOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import * as _nestjs_common from '@nestjs/common';
|
|
2
|
+
import { ExecutionContext, DynamicModule, OnApplicationBootstrap } from '@nestjs/common';
|
|
3
|
+
import Ajv, { ErrorObject, Options } from 'ajv';
|
|
4
|
+
import { SpecSource, OpenAPISpec, OpenApiRuntimeService } from '@nest-openapi/runtime';
|
|
5
|
+
import { HttpArgumentsHost } from '@nestjs/common/interfaces';
|
|
6
|
+
|
|
7
|
+
interface ValidationError extends ErrorObject {
|
|
8
|
+
validationType: 'path' | 'query' | 'body' | 'response';
|
|
9
|
+
}
|
|
10
|
+
interface ValidationErrorResponse {
|
|
11
|
+
message: string;
|
|
12
|
+
errors: ValidationError[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface ValidatorOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Provide your OpenAPI spec as an object, or point to it via URL or file path.
|
|
18
|
+
*
|
|
19
|
+
* Examples:
|
|
20
|
+
* ```
|
|
21
|
+
* { type: "object", spec: {...} }
|
|
22
|
+
* { type: "url", spec: "https://…" }
|
|
23
|
+
* { type: "file", spec: "./openapi.json" }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
specSource: SpecSource;
|
|
27
|
+
requestValidation?: {
|
|
28
|
+
/**
|
|
29
|
+
* Enable request validation
|
|
30
|
+
* @default true
|
|
31
|
+
*/
|
|
32
|
+
enable?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Transform request data after validation
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
37
|
+
transform?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Custom error handler for request validation failures.
|
|
40
|
+
*
|
|
41
|
+
* You can:
|
|
42
|
+
* - Transform errors and throw a custom exception
|
|
43
|
+
* - Log and Ignore errors (return without throwing)
|
|
44
|
+
*
|
|
45
|
+
* If not provided, throws `BadRequestException` with validation errors
|
|
46
|
+
*/
|
|
47
|
+
onValidationFailed?: (context: ExecutionContext, errors: ValidationError[]) => void | never;
|
|
48
|
+
};
|
|
49
|
+
responseValidation?: {
|
|
50
|
+
/**
|
|
51
|
+
* Enable response validation
|
|
52
|
+
* @default false
|
|
53
|
+
*/
|
|
54
|
+
enable?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Skip validation for error responses (4xx and 5xx status codes).
|
|
57
|
+
* @default true
|
|
58
|
+
*/
|
|
59
|
+
skipErrorResponses?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Custom error handler for response validation failures.
|
|
62
|
+
*
|
|
63
|
+
* You can:
|
|
64
|
+
* - Transform errors and throw a custom exception
|
|
65
|
+
* - Log and ignore errors (return without throwing)
|
|
66
|
+
*
|
|
67
|
+
* If not provided, warns and throws `InternalServerErrorException` without validation errors
|
|
68
|
+
*/
|
|
69
|
+
onValidationFailed?: (context: ExecutionContext, errors: ValidationError[]) => void | never;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Override the default ajv instance or configure it
|
|
73
|
+
*/
|
|
74
|
+
ajv?: Ajv | {
|
|
75
|
+
options?: Options;
|
|
76
|
+
configure?: (ajv: Ajv) => void;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Compile every request/parameter schema during application bootstrap.
|
|
80
|
+
* This removes the first-request latency at the cost of longer start-up time.
|
|
81
|
+
* @default false
|
|
82
|
+
*/
|
|
83
|
+
precompileSchemas?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Verbose logs for troubleshooting
|
|
86
|
+
* @default false
|
|
87
|
+
*/
|
|
88
|
+
debug?: boolean;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
declare class OpenApiValidatorModule {
|
|
92
|
+
/**
|
|
93
|
+
* Configure the OpenAPI validator module with static options
|
|
94
|
+
*/
|
|
95
|
+
static forRoot(options: ValidatorOptions): DynamicModule;
|
|
96
|
+
/**
|
|
97
|
+
* Configure the OpenAPI validator module with async options
|
|
98
|
+
*/
|
|
99
|
+
static forRootAsync(options: {
|
|
100
|
+
imports?: any[];
|
|
101
|
+
useFactory?: (...args: any[]) => ValidatorOptions | Promise<ValidatorOptions>;
|
|
102
|
+
inject?: any[];
|
|
103
|
+
}): DynamicModule;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
declare const OPENAPI_VALIDATOR = "OPENAPI_VALIDATOR";
|
|
107
|
+
declare class OpenApiValidatorService implements OnApplicationBootstrap {
|
|
108
|
+
private readonly runtime;
|
|
109
|
+
private readonly options;
|
|
110
|
+
private readonly logger;
|
|
111
|
+
private ajv;
|
|
112
|
+
openApiSpec: OpenAPISpec;
|
|
113
|
+
private debugLog;
|
|
114
|
+
constructor(runtime: OpenApiRuntimeService, options: ValidatorOptions);
|
|
115
|
+
onApplicationBootstrap(): Promise<void>;
|
|
116
|
+
private get isSpecLoaded();
|
|
117
|
+
get validationOptions(): ValidatorOptions;
|
|
118
|
+
/**
|
|
119
|
+
* Register all component schemas with AJV so it can resolve $ref references
|
|
120
|
+
*/
|
|
121
|
+
private registerComponentSchemas;
|
|
122
|
+
/**
|
|
123
|
+
* Walk the OpenAPI spec and eagerly compile all requestBody and parameter
|
|
124
|
+
* schemas. This is triggered when `options.performance.precompileSchemas === true` and
|
|
125
|
+
* eliminates runtime compilation overhead.
|
|
126
|
+
*/
|
|
127
|
+
private precompileSchemas;
|
|
128
|
+
private findOperation;
|
|
129
|
+
private validateWithSchema;
|
|
130
|
+
validateRequest(httpContext: HttpArgumentsHost, options?: {
|
|
131
|
+
body?: boolean;
|
|
132
|
+
params?: boolean;
|
|
133
|
+
query?: boolean;
|
|
134
|
+
}): ValidationError[];
|
|
135
|
+
private validateParameters;
|
|
136
|
+
validateResponse(httpContext: HttpArgumentsHost, statusCode: number, responseBody: any): ValidationError[];
|
|
137
|
+
private getParameterSchema;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
interface ValidateOptions {
|
|
141
|
+
request?: boolean | {
|
|
142
|
+
params?: boolean;
|
|
143
|
+
query?: boolean;
|
|
144
|
+
body?: boolean;
|
|
145
|
+
};
|
|
146
|
+
response?: boolean;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Decorator to configure validation behavior for a specific route
|
|
150
|
+
*
|
|
151
|
+
* @param options - Validation configuration options
|
|
152
|
+
* @param options.request - Request validation settings:
|
|
153
|
+
* - `true`: Validate all request parts (params, query, body)
|
|
154
|
+
* - `false`: Skip request validation entirely
|
|
155
|
+
* - `{ params?, query?, body? }`: Validate only specified request parts
|
|
156
|
+
* - `undefined`: Use default configuration
|
|
157
|
+
* @param options.response - Response validation settings:
|
|
158
|
+
* - `true`: Enable response validation
|
|
159
|
+
* - `false`: Skip response validation
|
|
160
|
+
* - `undefined`: Use default configuration
|
|
161
|
+
*
|
|
162
|
+
* @example In controller:
|
|
163
|
+
*
|
|
164
|
+
* ```typescript
|
|
165
|
+
* @Controller('users')
|
|
166
|
+
* export class UsersController {
|
|
167
|
+
* // Validate body and response
|
|
168
|
+
* @Put(':id')
|
|
169
|
+
* @Validate({ request: { body: true }, response: true })
|
|
170
|
+
* update(@Param('id') id: string, @Body() user: UpdateUserDto): User {
|
|
171
|
+
* return this.usersService.update(id, user);
|
|
172
|
+
* }
|
|
173
|
+
* }
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
declare const Validate: (options?: ValidateOptions) => _nestjs_common.CustomDecorator<string>;
|
|
177
|
+
|
|
178
|
+
export { OPENAPI_VALIDATOR, OpenApiValidatorModule, OpenApiValidatorService, Validate, type ValidateOptions, type ValidationError, type ValidationErrorResponse, type ValidatorOptions };
|