@stlmpp/nestjs-zod 1.0.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.
@@ -0,0 +1,228 @@
1
+ import { a as UnknownSchema, i as RequiredBy, n as createZodDto, t as ZodDto } from "./dto-CMhDHMJh.cjs";
2
+ import { input, output } from "zod/v4/core";
3
+ import { BadRequestException, CallHandler, CanActivate, ExecutionContext, InternalServerErrorException, PipeTransform } from "@nestjs/common";
4
+ import { z } from "zod/v3";
5
+ import { OpenAPIObject } from "@nestjs/swagger";
6
+ import { Observable } from "rxjs";
7
+
8
+ //#region src/config.d.ts
9
+ interface ZodDtoConfig {
10
+ /**
11
+ * Default value for `createZodDto`'s `codec` option when it isn't passed
12
+ * explicitly for a given DTO.
13
+ */
14
+ codec: boolean;
15
+ }
16
+ /**
17
+ * Overrides the library-wide defaults used by `createZodDto`. Any option
18
+ * not passed here keeps its previous value. Call this once, e.g. during
19
+ * app bootstrap, before any `createZodDto` calls that should observe it.
20
+ */
21
+ declare function configureZodDto(config: Partial<ZodDtoConfig>): void;
22
+ //#endregion
23
+ //#region src/exception.d.ts
24
+ declare class ZodValidationException extends BadRequestException {
25
+ private error;
26
+ constructor(error: unknown);
27
+ getZodError(): unknown;
28
+ }
29
+ declare class ZodSerializationException extends InternalServerErrorException {
30
+ private error;
31
+ constructor(error: unknown);
32
+ getZodError(): unknown;
33
+ }
34
+ type ZodExceptionCreator = (error: unknown) => Error;
35
+ //#endregion
36
+ //#region src/guard.d.ts
37
+ type Source = 'body' | 'query' | 'params';
38
+ interface ZodBodyGuardOptions {
39
+ createValidationException?: ZodExceptionCreator;
40
+ }
41
+ type ZodGuardClass = new (source: Source, schemaOrDto: UnknownSchema | ZodDto) => CanActivate;
42
+ /**
43
+ * @deprecated `createZodGuard` will be removed in a future version, since
44
+ * guards are not intended for validation purposes.
45
+ */
46
+ declare function createZodGuard({
47
+ createValidationException
48
+ }?: ZodBodyGuardOptions): ZodGuardClass;
49
+ /**
50
+ * @deprecated `ZodGuard` will be removed in a future version, since guards
51
+ * are not intended for validation purposes.
52
+ */
53
+ declare const ZodGuard: ZodGuardClass;
54
+ /**
55
+ * @deprecated `UseZodGuard` will be removed in a future version, since guards
56
+ * are not intended for validation purposes.
57
+ */
58
+ declare const UseZodGuard: (source: Source, schemaOrDto: UnknownSchema | ZodDto) => MethodDecorator & ClassDecorator;
59
+ //#endregion
60
+ //#region src/zodV3ToOpenApi.d.ts
61
+ type SchemaObject = Exclude<Exclude<Exclude<OpenAPIObject['components'], undefined>['schemas'], undefined>[string], {
62
+ $ref: string;
63
+ }>;
64
+ interface ExtendedSchemaObject extends SchemaObject {
65
+ [key: `x-${string}`]: any;
66
+ }
67
+ /**
68
+ * @deprecated `zodToOpenAPI` will be removed in a future version, since zod
69
+ * v4 adds built-in support for generating OpenAPI schemas from zod schemas.
70
+ */
71
+ declare function zodV3ToOpenAPI(zodType: z.ZodTypeAny, visited?: Set<any>): ExtendedSchemaObject;
72
+ //#endregion
73
+ //#region src/pipe.d.ts
74
+ interface ZodValidationPipeOptions {
75
+ /**
76
+ * Use this to customize the exception that is thrown when validation fails
77
+ */
78
+ createValidationException?: ZodExceptionCreator;
79
+ /**
80
+ * If `true`, then an error will be thrown if the pipe tries to validate a
81
+ * value that is not typed with a nestjs-zod DTO
82
+ *
83
+ * It's recommended to set this to `true`, since it will catch cases where
84
+ * we're not properly validating data
85
+ */
86
+ strictSchemaDeclaration?: boolean;
87
+ }
88
+ type ZodValidationPipeClass = new (schemaOrDto?: UnknownSchema | ZodDto) => PipeTransform;
89
+ declare class ZodSchemaDeclarationException extends InternalServerErrorException {
90
+ constructor();
91
+ }
92
+ declare function createZodValidationPipe({
93
+ createValidationException,
94
+ strictSchemaDeclaration
95
+ }?: ZodValidationPipeOptions): ZodValidationPipeClass;
96
+ declare const ZodValidationPipe: ZodValidationPipeClass;
97
+ //#endregion
98
+ //#region src/serializer.d.ts
99
+ declare function ZodSerializerDto(dto: ZodDto | UnknownSchema | [ZodDto] | [UnknownSchema]): import("@nestjs/common").CustomDecorator<"ZOD_SERIALIZER_DTO_OPTIONS">;
100
+ interface ZodSerializerInterceptorOptions {
101
+ reportInput?: boolean;
102
+ }
103
+ type ZodSerializerInterceptorClass = new (...args: unknown[]) => {
104
+ intercept(context: ExecutionContext, next: CallHandler): Observable<unknown>;
105
+ };
106
+ declare function createZodSerializerInterceptor({
107
+ reportInput
108
+ }?: ZodSerializerInterceptorOptions): ZodSerializerInterceptorClass;
109
+ declare const ZodSerializerInterceptor: ZodSerializerInterceptorClass;
110
+ //#endregion
111
+ //#region src/validate.d.ts
112
+ /**
113
+ * @deprecated `validate` will be removed in a future version. It is
114
+ * recommended to use `.parse` directly
115
+ */
116
+ declare function validate<TSchema extends UnknownSchema>(value: unknown, schemaOrDto: TSchema | ZodDto<TSchema, boolean>, createValidationException?: ZodExceptionCreator): ReturnType<TSchema['parse']>;
117
+ //#endregion
118
+ //#region src/cleanupOpenApiDoc.d.ts
119
+ /**
120
+ * This function performs some post-processing on the OpenAPI document. It
121
+ * should only touch parts of the document that were generated from nestjs-zod
122
+ * DTOs.
123
+ *
124
+ * Specifically, this function:
125
+ * 1. Removes empty `type` fields
126
+ * 2. Renames OpenAPI schemas that have an explicit `id` field to match that
127
+ * `id`, instead of using the DTO class name
128
+ * 3. If the DTO's schema references another zod schema, it adds that zod
129
+ * schema's OpenAPI representation to `components.schemas`
130
+ * 4. If a DTO is created directly with an array zod schema, it ensures the
131
+ * OpenAPI schema is generated properly
132
+ * 5. Handles recursive zod schemas
133
+ * 6. Handles `null` properly based on the OpenAPI version
134
+ *
135
+ * @param doc - The OpenAPI document that is generated by `SwaggerModule.createDocument`
136
+ * @param options.version - The version of OpenAPI to use. Defaults to `auto`,
137
+ * which will use the version of the OpenAPI object passed in. Note if the
138
+ * version is `3.1` then it impacts how `null` is handled. In `3.0`, `nullable:
139
+ * true` is used, while in `3.1` `anyOf: [..., { type: 'null' }]` is used
140
+ * instead
141
+ * @returns A cleaned up OpenAPI document
142
+ */
143
+ declare function cleanupOpenApiDoc(doc: OpenAPIObject, {
144
+ version: versionParam
145
+ }?: {
146
+ version?: '3.1' | '3.0' | 'auto';
147
+ }): OpenAPIObject;
148
+ //#endregion
149
+ //#region src/response.d.ts
150
+ /**
151
+ * `@ZodResponse` can be used to set the response information for a method.
152
+ * This is the recommended way to handle responses, since it applies a few
153
+ * related decorators at once to keep them in sync:
154
+ *
155
+ * 1. Uses `@ZodSerializerDto` to serialize the return value of the method. This
156
+ * means the return value of the method will be parsed by the DTO's schema.
157
+ * 2. Uses `@ApiResponse` to set the response DTO for the method. This means
158
+ * the OpenAPI documentation will be updated to reflect the DTO's schema.
159
+ * Note that by default ZodResponse automatically uses the output version of
160
+ * the DTO, so there is no need to use DTO.Output. If the `codec` option is
161
+ * set to `true` on the DTO (`createZodDto(z.object({}), { codec: true })`),
162
+ * then the response schema will be the input version of the DTO.
163
+ * 3. Uses `@HttpCode` to set the HTTP status code for the response if `status`
164
+ * is provided
165
+ * 4. Lastly, by default it also throws a typescript error if the return value
166
+ * of the method does not match the DTO's input schema. However, if the
167
+ * `codec` option is set to `true` on the DTO, it throws a typescript error
168
+ * unless the return value matches the DTO's output schema instead.
169
+ *
170
+ * `@ZodResponse` is powerful because it keeps the run-time, compile-time, and
171
+ * docs-time response representations in sync
172
+ *
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * @Get()
177
+ * @ZodResponse({ status: 200, description: 'Get book', type: BookDto })
178
+ * getBook() {
179
+ * return { id: '1' };
180
+ * }
181
+ * ```
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * @Get()
186
+ * @ZodResponse({ status: 200, description: 'Get books', type: [BookDto] })
187
+ * getBooks() {
188
+ * return [{ id: '1' }, { id: '2' }];
189
+ * }
190
+ */
191
+ declare function ZodResponse<TSchema extends UnknownSchema>({
192
+ status,
193
+ description,
194
+ type
195
+ }: {
196
+ status?: number;
197
+ description?: string;
198
+ type: ZodDto<TSchema, true, 'input' | 'output'>;
199
+ }): (target: object, propertyKey?: string | symbol, descriptor?: Pick<TypedPropertyDescriptor<(...args: any[]) => output<TSchema> | Promise<output<TSchema>>>, 'value'>) => void;
200
+ declare function ZodResponse<TSchema extends RequiredBy<UnknownSchema, 'array'>>({
201
+ status,
202
+ description,
203
+ type
204
+ }: {
205
+ status?: number;
206
+ description?: string;
207
+ type: [ZodDto<TSchema, true, 'input' | 'output'>];
208
+ }): (target: object, propertyKey?: string | symbol, descriptor?: Pick<TypedPropertyDescriptor<(...args: any[]) => Array<output<TSchema>> | Promise<Array<output<TSchema>>>>, 'value'>) => void;
209
+ declare function ZodResponse<TSchema extends UnknownSchema>({
210
+ status,
211
+ description,
212
+ type
213
+ }: {
214
+ status?: number;
215
+ description?: string;
216
+ type: ZodDto<TSchema, false, 'input' | 'output'>;
217
+ }): (target: object, propertyKey?: string | symbol, descriptor?: Pick<TypedPropertyDescriptor<(...args: any[]) => input<TSchema> | Promise<input<TSchema>>>, 'value'>) => void;
218
+ declare function ZodResponse<TSchema extends RequiredBy<UnknownSchema, 'array'>>({
219
+ status,
220
+ description,
221
+ type
222
+ }: {
223
+ status?: number;
224
+ description?: string;
225
+ type: [ZodDto<TSchema, false, 'input' | 'output'>];
226
+ }): (target: object, propertyKey?: string | symbol, descriptor?: Pick<TypedPropertyDescriptor<(...args: any[]) => Array<input<TSchema>> | Promise<Array<input<TSchema>>>>, 'value'>) => void;
227
+ //#endregion
228
+ export { UseZodGuard, type ZodDto, ZodGuard, ZodResponse, ZodSchemaDeclarationException, ZodSerializationException, ZodSerializerDto, ZodSerializerInterceptor, ZodValidationException, ZodValidationPipe, cleanupOpenApiDoc, configureZodDto, createZodDto, createZodGuard, createZodSerializerInterceptor, createZodValidationPipe, validate, zodV3ToOpenAPI };
@@ -0,0 +1,228 @@
1
+ import { a as UnknownSchema, i as RequiredBy, n as createZodDto, t as ZodDto } from "./dto-CMhDHMJh.mjs";
2
+ import { input, output } from "zod/v4/core";
3
+ import { z } from "zod/v3";
4
+ import { BadRequestException, CallHandler, CanActivate, ExecutionContext, InternalServerErrorException, PipeTransform } from "@nestjs/common";
5
+ import { Observable } from "rxjs";
6
+ import { OpenAPIObject } from "@nestjs/swagger";
7
+
8
+ //#region src/config.d.ts
9
+ interface ZodDtoConfig {
10
+ /**
11
+ * Default value for `createZodDto`'s `codec` option when it isn't passed
12
+ * explicitly for a given DTO.
13
+ */
14
+ codec: boolean;
15
+ }
16
+ /**
17
+ * Overrides the library-wide defaults used by `createZodDto`. Any option
18
+ * not passed here keeps its previous value. Call this once, e.g. during
19
+ * app bootstrap, before any `createZodDto` calls that should observe it.
20
+ */
21
+ declare function configureZodDto(config: Partial<ZodDtoConfig>): void;
22
+ //#endregion
23
+ //#region src/exception.d.ts
24
+ declare class ZodValidationException extends BadRequestException {
25
+ private error;
26
+ constructor(error: unknown);
27
+ getZodError(): unknown;
28
+ }
29
+ declare class ZodSerializationException extends InternalServerErrorException {
30
+ private error;
31
+ constructor(error: unknown);
32
+ getZodError(): unknown;
33
+ }
34
+ type ZodExceptionCreator = (error: unknown) => Error;
35
+ //#endregion
36
+ //#region src/guard.d.ts
37
+ type Source = 'body' | 'query' | 'params';
38
+ interface ZodBodyGuardOptions {
39
+ createValidationException?: ZodExceptionCreator;
40
+ }
41
+ type ZodGuardClass = new (source: Source, schemaOrDto: UnknownSchema | ZodDto) => CanActivate;
42
+ /**
43
+ * @deprecated `createZodGuard` will be removed in a future version, since
44
+ * guards are not intended for validation purposes.
45
+ */
46
+ declare function createZodGuard({
47
+ createValidationException
48
+ }?: ZodBodyGuardOptions): ZodGuardClass;
49
+ /**
50
+ * @deprecated `ZodGuard` will be removed in a future version, since guards
51
+ * are not intended for validation purposes.
52
+ */
53
+ declare const ZodGuard: ZodGuardClass;
54
+ /**
55
+ * @deprecated `UseZodGuard` will be removed in a future version, since guards
56
+ * are not intended for validation purposes.
57
+ */
58
+ declare const UseZodGuard: (source: Source, schemaOrDto: UnknownSchema | ZodDto) => MethodDecorator & ClassDecorator;
59
+ //#endregion
60
+ //#region src/zodV3ToOpenApi.d.ts
61
+ type SchemaObject = Exclude<Exclude<Exclude<OpenAPIObject['components'], undefined>['schemas'], undefined>[string], {
62
+ $ref: string;
63
+ }>;
64
+ interface ExtendedSchemaObject extends SchemaObject {
65
+ [key: `x-${string}`]: any;
66
+ }
67
+ /**
68
+ * @deprecated `zodToOpenAPI` will be removed in a future version, since zod
69
+ * v4 adds built-in support for generating OpenAPI schemas from zod schemas.
70
+ */
71
+ declare function zodV3ToOpenAPI(zodType: z.ZodTypeAny, visited?: Set<any>): ExtendedSchemaObject;
72
+ //#endregion
73
+ //#region src/pipe.d.ts
74
+ interface ZodValidationPipeOptions {
75
+ /**
76
+ * Use this to customize the exception that is thrown when validation fails
77
+ */
78
+ createValidationException?: ZodExceptionCreator;
79
+ /**
80
+ * If `true`, then an error will be thrown if the pipe tries to validate a
81
+ * value that is not typed with a nestjs-zod DTO
82
+ *
83
+ * It's recommended to set this to `true`, since it will catch cases where
84
+ * we're not properly validating data
85
+ */
86
+ strictSchemaDeclaration?: boolean;
87
+ }
88
+ type ZodValidationPipeClass = new (schemaOrDto?: UnknownSchema | ZodDto) => PipeTransform;
89
+ declare class ZodSchemaDeclarationException extends InternalServerErrorException {
90
+ constructor();
91
+ }
92
+ declare function createZodValidationPipe({
93
+ createValidationException,
94
+ strictSchemaDeclaration
95
+ }?: ZodValidationPipeOptions): ZodValidationPipeClass;
96
+ declare const ZodValidationPipe: ZodValidationPipeClass;
97
+ //#endregion
98
+ //#region src/serializer.d.ts
99
+ declare function ZodSerializerDto(dto: ZodDto | UnknownSchema | [ZodDto] | [UnknownSchema]): import("@nestjs/common").CustomDecorator<"ZOD_SERIALIZER_DTO_OPTIONS">;
100
+ interface ZodSerializerInterceptorOptions {
101
+ reportInput?: boolean;
102
+ }
103
+ type ZodSerializerInterceptorClass = new (...args: unknown[]) => {
104
+ intercept(context: ExecutionContext, next: CallHandler): Observable<unknown>;
105
+ };
106
+ declare function createZodSerializerInterceptor({
107
+ reportInput
108
+ }?: ZodSerializerInterceptorOptions): ZodSerializerInterceptorClass;
109
+ declare const ZodSerializerInterceptor: ZodSerializerInterceptorClass;
110
+ //#endregion
111
+ //#region src/validate.d.ts
112
+ /**
113
+ * @deprecated `validate` will be removed in a future version. It is
114
+ * recommended to use `.parse` directly
115
+ */
116
+ declare function validate<TSchema extends UnknownSchema>(value: unknown, schemaOrDto: TSchema | ZodDto<TSchema, boolean>, createValidationException?: ZodExceptionCreator): ReturnType<TSchema['parse']>;
117
+ //#endregion
118
+ //#region src/cleanupOpenApiDoc.d.ts
119
+ /**
120
+ * This function performs some post-processing on the OpenAPI document. It
121
+ * should only touch parts of the document that were generated from nestjs-zod
122
+ * DTOs.
123
+ *
124
+ * Specifically, this function:
125
+ * 1. Removes empty `type` fields
126
+ * 2. Renames OpenAPI schemas that have an explicit `id` field to match that
127
+ * `id`, instead of using the DTO class name
128
+ * 3. If the DTO's schema references another zod schema, it adds that zod
129
+ * schema's OpenAPI representation to `components.schemas`
130
+ * 4. If a DTO is created directly with an array zod schema, it ensures the
131
+ * OpenAPI schema is generated properly
132
+ * 5. Handles recursive zod schemas
133
+ * 6. Handles `null` properly based on the OpenAPI version
134
+ *
135
+ * @param doc - The OpenAPI document that is generated by `SwaggerModule.createDocument`
136
+ * @param options.version - The version of OpenAPI to use. Defaults to `auto`,
137
+ * which will use the version of the OpenAPI object passed in. Note if the
138
+ * version is `3.1` then it impacts how `null` is handled. In `3.0`, `nullable:
139
+ * true` is used, while in `3.1` `anyOf: [..., { type: 'null' }]` is used
140
+ * instead
141
+ * @returns A cleaned up OpenAPI document
142
+ */
143
+ declare function cleanupOpenApiDoc(doc: OpenAPIObject, {
144
+ version: versionParam
145
+ }?: {
146
+ version?: '3.1' | '3.0' | 'auto';
147
+ }): OpenAPIObject;
148
+ //#endregion
149
+ //#region src/response.d.ts
150
+ /**
151
+ * `@ZodResponse` can be used to set the response information for a method.
152
+ * This is the recommended way to handle responses, since it applies a few
153
+ * related decorators at once to keep them in sync:
154
+ *
155
+ * 1. Uses `@ZodSerializerDto` to serialize the return value of the method. This
156
+ * means the return value of the method will be parsed by the DTO's schema.
157
+ * 2. Uses `@ApiResponse` to set the response DTO for the method. This means
158
+ * the OpenAPI documentation will be updated to reflect the DTO's schema.
159
+ * Note that by default ZodResponse automatically uses the output version of
160
+ * the DTO, so there is no need to use DTO.Output. If the `codec` option is
161
+ * set to `true` on the DTO (`createZodDto(z.object({}), { codec: true })`),
162
+ * then the response schema will be the input version of the DTO.
163
+ * 3. Uses `@HttpCode` to set the HTTP status code for the response if `status`
164
+ * is provided
165
+ * 4. Lastly, by default it also throws a typescript error if the return value
166
+ * of the method does not match the DTO's input schema. However, if the
167
+ * `codec` option is set to `true` on the DTO, it throws a typescript error
168
+ * unless the return value matches the DTO's output schema instead.
169
+ *
170
+ * `@ZodResponse` is powerful because it keeps the run-time, compile-time, and
171
+ * docs-time response representations in sync
172
+ *
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * @Get()
177
+ * @ZodResponse({ status: 200, description: 'Get book', type: BookDto })
178
+ * getBook() {
179
+ * return { id: '1' };
180
+ * }
181
+ * ```
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * @Get()
186
+ * @ZodResponse({ status: 200, description: 'Get books', type: [BookDto] })
187
+ * getBooks() {
188
+ * return [{ id: '1' }, { id: '2' }];
189
+ * }
190
+ */
191
+ declare function ZodResponse<TSchema extends UnknownSchema>({
192
+ status,
193
+ description,
194
+ type
195
+ }: {
196
+ status?: number;
197
+ description?: string;
198
+ type: ZodDto<TSchema, true, 'input' | 'output'>;
199
+ }): (target: object, propertyKey?: string | symbol, descriptor?: Pick<TypedPropertyDescriptor<(...args: any[]) => output<TSchema> | Promise<output<TSchema>>>, 'value'>) => void;
200
+ declare function ZodResponse<TSchema extends RequiredBy<UnknownSchema, 'array'>>({
201
+ status,
202
+ description,
203
+ type
204
+ }: {
205
+ status?: number;
206
+ description?: string;
207
+ type: [ZodDto<TSchema, true, 'input' | 'output'>];
208
+ }): (target: object, propertyKey?: string | symbol, descriptor?: Pick<TypedPropertyDescriptor<(...args: any[]) => Array<output<TSchema>> | Promise<Array<output<TSchema>>>>, 'value'>) => void;
209
+ declare function ZodResponse<TSchema extends UnknownSchema>({
210
+ status,
211
+ description,
212
+ type
213
+ }: {
214
+ status?: number;
215
+ description?: string;
216
+ type: ZodDto<TSchema, false, 'input' | 'output'>;
217
+ }): (target: object, propertyKey?: string | symbol, descriptor?: Pick<TypedPropertyDescriptor<(...args: any[]) => input<TSchema> | Promise<input<TSchema>>>, 'value'>) => void;
218
+ declare function ZodResponse<TSchema extends RequiredBy<UnknownSchema, 'array'>>({
219
+ status,
220
+ description,
221
+ type
222
+ }: {
223
+ status?: number;
224
+ description?: string;
225
+ type: [ZodDto<TSchema, false, 'input' | 'output'>];
226
+ }): (target: object, propertyKey?: string | symbol, descriptor?: Pick<TypedPropertyDescriptor<(...args: any[]) => Array<input<TSchema>> | Promise<Array<input<TSchema>>>>, 'value'>) => void;
227
+ //#endregion
228
+ export { UseZodGuard, type ZodDto, ZodGuard, ZodResponse, ZodSchemaDeclarationException, ZodSerializationException, ZodSerializerDto, ZodSerializerInterceptor, ZodValidationException, ZodValidationPipe, cleanupOpenApiDoc, configureZodDto, createZodDto, createZodGuard, createZodSerializerInterceptor, createZodValidationPipe, validate, zodV3ToOpenAPI };