@voznov/zod-dto-nestjs 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 +176 -0
- package/NOTICE +4 -0
- package/README.md +72 -0
- package/dist/index.cjs +234 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +211 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding any notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/NOTICE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @voznov/zod-dto-nestjs
|
|
2
|
+
|
|
3
|
+
NestJS adapter for `@voznov/zod-dto` — validation pipe + automatic Swagger integration.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @voznov/zod-dto-nestjs @voznov/zod-dto @nestjs/common @nestjs/swagger zod
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
Register the pipe globally, then use DTO classes as controller parameter types:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// main.ts
|
|
17
|
+
import { ZodValidationPipe } from '@voznov/zod-dto-nestjs';
|
|
18
|
+
app.useGlobalPipes(new ZodValidationPipe());
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { Body, Controller, Post } from '@nestjs/common';
|
|
23
|
+
import { ZodDto } from '@voznov/zod-dto';
|
|
24
|
+
import { z } from 'zod';
|
|
25
|
+
|
|
26
|
+
const CreateUserDto = ZodDto(z.object({ name: z.string(), email: z.email() }));
|
|
27
|
+
type CreateUser = z.infer<typeof CreateUserDto>;
|
|
28
|
+
|
|
29
|
+
@Controller('users')
|
|
30
|
+
export class UsersController {
|
|
31
|
+
@Post()
|
|
32
|
+
create(@Body() body: CreateUser) {
|
|
33
|
+
// already validated; `body` is a CreateUserDto instance
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Gradual migration
|
|
39
|
+
|
|
40
|
+
`ZodValidationPipe` only engages when the parameter's metatype is a `ZodDtoClass` — every other type (primitives, plain classes, class-validator DTOs with `ValidationPipe`) passes through untouched. Safe to register globally alongside an existing setup; convert DTOs one at a time.
|
|
41
|
+
|
|
42
|
+
## Swagger integration
|
|
43
|
+
|
|
44
|
+
Importing this package side-effect-registers an `onCreate` hook that decorates every DTO class with `@ApiProperty` metadata based on its Zod schema — no manual decorators required.
|
|
45
|
+
|
|
46
|
+
Supported shapes: scalars, objects (nested), arrays, tuples, records, enums, literals, unions (including discriminated), intersections, optional/nullable/default wrappers, and nested DTO references (via `oneOf` + `ApiExtraModels`).
|
|
47
|
+
|
|
48
|
+
## Custom error response
|
|
49
|
+
|
|
50
|
+
`ZodValidationPipe` accepts `{ createError }`. Default throws `BadRequestException` with the issues list as its response body.
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { HttpException, HttpStatus } from '@nestjs/common';
|
|
54
|
+
|
|
55
|
+
app.useGlobalPipes(
|
|
56
|
+
new ZodValidationPipe({
|
|
57
|
+
createError: (issues) =>
|
|
58
|
+
new HttpException({ statusCode: 400, error: 'Bad Request', errors: issues }, HttpStatus.BAD_REQUEST),
|
|
59
|
+
}),
|
|
60
|
+
);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## API
|
|
64
|
+
|
|
65
|
+
| Export | Description |
|
|
66
|
+
| -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
|
67
|
+
| `ZodValidationPipe` | `PipeTransform` for `@Body()` / `@Param()` / `@Query()`. Accepts `{ createError?: (issues: string[]) => Error }`. |
|
|
68
|
+
| `applySwaggerDecorators(schema)` | Low-level: apply `@ApiProperty` metadata to a schema. Auto-invoked via `registerOnCreate`; export is for manual/edge-case use. |
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
Apache-2.0
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
20
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
21
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
22
|
+
if (decorator = decorators[i])
|
|
23
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
24
|
+
if (kind && result) __defProp(target, key, result);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// src/index.ts
|
|
29
|
+
var index_exports = {};
|
|
30
|
+
__export(index_exports, {
|
|
31
|
+
ZodValidationPipe: () => ZodValidationPipe,
|
|
32
|
+
applySwaggerDecorators: () => applySwaggerDecorators
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(index_exports);
|
|
35
|
+
var import_zod_dto3 = require("@voznov/zod-dto");
|
|
36
|
+
|
|
37
|
+
// src/swagger.ts
|
|
38
|
+
var import_swagger = require("@nestjs/swagger");
|
|
39
|
+
var import_open_api_spec = require("@nestjs/swagger/dist/interfaces/open-api-spec.interface");
|
|
40
|
+
var import_zod_dto = require("@voznov/zod-dto");
|
|
41
|
+
var import_zod = require("zod");
|
|
42
|
+
|
|
43
|
+
// src/utils.ts
|
|
44
|
+
var mapValues = (obj, fn) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, fn(value, key)]));
|
|
45
|
+
|
|
46
|
+
// src/swagger.ts
|
|
47
|
+
var schemaObjectToApiPropertyOptions = (so, selfRequired) => {
|
|
48
|
+
if ("oneOf" in so || "anyOf" in so || "allOf" in so) {
|
|
49
|
+
return { ...so, type: Array, required: selfRequired };
|
|
50
|
+
}
|
|
51
|
+
return { ...so, required: selfRequired };
|
|
52
|
+
};
|
|
53
|
+
var leaf = (so) => ({
|
|
54
|
+
so,
|
|
55
|
+
selfRequired: true,
|
|
56
|
+
innerSchemas: /* @__PURE__ */ new Set()
|
|
57
|
+
});
|
|
58
|
+
var decoratedDtoClasses = /* @__PURE__ */ new Set();
|
|
59
|
+
var applySwaggerDecorators = (schema) => {
|
|
60
|
+
if (schema instanceof import_zod.z.ZodObject) {
|
|
61
|
+
if ((0, import_zod_dto.isZodDtoClass)(schema) && decoratedDtoClasses.has(schema)) {
|
|
62
|
+
return { so: { oneOf: (0, import_swagger.refs)(schema) }, selfRequired: true, innerSchemas: /* @__PURE__ */ new Set([schema]) };
|
|
63
|
+
}
|
|
64
|
+
const properties = {};
|
|
65
|
+
const required = [];
|
|
66
|
+
const innerSchemas = /* @__PURE__ */ new Set();
|
|
67
|
+
Object.entries(schema.shape).forEach(([key, fieldSchema]) => {
|
|
68
|
+
const { so, selfRequired, innerSchemas: innerSchemas_ } = applySwaggerDecorators(fieldSchema);
|
|
69
|
+
properties[key] = so;
|
|
70
|
+
innerSchemas_.forEach((innerSchema) => innerSchemas.add(innerSchema));
|
|
71
|
+
if (selfRequired) {
|
|
72
|
+
required.push(key);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
if ((0, import_zod_dto.isZodDtoClass)(schema)) {
|
|
76
|
+
decoratedDtoClasses.add(schema);
|
|
77
|
+
for (const [key, propertySo] of Object.entries(properties)) {
|
|
78
|
+
(0, import_swagger.ApiProperty)(schemaObjectToApiPropertyOptions(propertySo, required.includes(key)))(schema.prototype, key);
|
|
79
|
+
}
|
|
80
|
+
(0, import_swagger.ApiExtraModels)(...[...innerSchemas.values()].filter((innerSchema) => innerSchema !== schema))(schema);
|
|
81
|
+
return { so: { oneOf: (0, import_swagger.refs)(schema) }, selfRequired: true, innerSchemas: /* @__PURE__ */ new Set([schema]) };
|
|
82
|
+
}
|
|
83
|
+
return { so: { type: "object", properties: mapValues(properties, (so) => so.oneOf?.length === 1 ? so.oneOf[0] : so), required }, selfRequired: true, innerSchemas };
|
|
84
|
+
}
|
|
85
|
+
if (schema instanceof import_zod.z.ZodRecord) {
|
|
86
|
+
const { so } = applySwaggerDecorators(schema._zod.def.valueType);
|
|
87
|
+
return leaf({ type: "object", additionalProperties: so.oneOf?.length === 1 ? so.oneOf[0] : so });
|
|
88
|
+
}
|
|
89
|
+
if (schema instanceof import_zod.z.ZodOptional || schema instanceof import_zod.z.ZodExactOptional) {
|
|
90
|
+
return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: false };
|
|
91
|
+
}
|
|
92
|
+
if (schema instanceof import_zod.z.ZodDefault) {
|
|
93
|
+
return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: false };
|
|
94
|
+
}
|
|
95
|
+
if (schema instanceof import_zod.z.ZodReadonly || schema instanceof import_zod.z.ZodLazy || schema instanceof import_zod.z.ZodCatch) {
|
|
96
|
+
return applySwaggerDecorators(schema.unwrap());
|
|
97
|
+
}
|
|
98
|
+
if (schema instanceof import_zod.z.ZodNonOptional) {
|
|
99
|
+
return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: true };
|
|
100
|
+
}
|
|
101
|
+
if (schema instanceof import_zod.z.ZodNullable) {
|
|
102
|
+
const result = applySwaggerDecorators(schema.unwrap());
|
|
103
|
+
return { ...result, so: { ...result.so, nullable: true } };
|
|
104
|
+
}
|
|
105
|
+
if (schema instanceof import_zod.z.ZodPipe) {
|
|
106
|
+
const inner = schema.in instanceof import_zod.z.ZodTransform ? schema.out : schema.in;
|
|
107
|
+
return applySwaggerDecorators(inner);
|
|
108
|
+
}
|
|
109
|
+
if (schema instanceof import_zod.z.ZodTransform) {
|
|
110
|
+
return leaf({});
|
|
111
|
+
}
|
|
112
|
+
if (schema instanceof import_zod.z.ZodArray) {
|
|
113
|
+
const element = schema.unwrap();
|
|
114
|
+
const { so, selfRequired: selfRequiredElement, innerSchemas } = applySwaggerDecorators(element);
|
|
115
|
+
if (!selfRequiredElement) {
|
|
116
|
+
throw new Error("Not required array item is not supported in Swagger. Use nullable instead.");
|
|
117
|
+
}
|
|
118
|
+
return { so: { type: "array", items: so.oneOf?.length === 1 ? so.oneOf[0] : so }, selfRequired: true, innerSchemas };
|
|
119
|
+
}
|
|
120
|
+
if (schema instanceof import_zod.z.ZodTuple) {
|
|
121
|
+
const itemSchemas = schema._zod.def.items.map((item) => {
|
|
122
|
+
const { so } = applySwaggerDecorators(item);
|
|
123
|
+
return so.oneOf?.length === 1 ? so.oneOf[0] : so;
|
|
124
|
+
});
|
|
125
|
+
const unique = [...new Map(itemSchemas.map((s) => [JSON.stringify(s), s])).values()];
|
|
126
|
+
const items = unique.length === 1 ? unique[0] : { oneOf: unique };
|
|
127
|
+
return leaf({ type: "array", items, minItems: itemSchemas.length, maxItems: itemSchemas.length });
|
|
128
|
+
}
|
|
129
|
+
if (schema instanceof import_zod.z.ZodString || schema instanceof import_zod.z.ZodStringFormat) {
|
|
130
|
+
const so = { type: "string" };
|
|
131
|
+
if (schema.minLength !== null) so.minLength = schema.minLength;
|
|
132
|
+
if (schema.maxLength !== null) so.maxLength = schema.maxLength;
|
|
133
|
+
const fmt = schema.format;
|
|
134
|
+
if (fmt !== null) {
|
|
135
|
+
if (fmt === "regex") {
|
|
136
|
+
const patterns = schema._zod.bag.patterns;
|
|
137
|
+
const first = patterns ? [...patterns][0] : void 0;
|
|
138
|
+
if (first) so.pattern = first.source;
|
|
139
|
+
} else {
|
|
140
|
+
so.format = fmt;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return { so, selfRequired: true, innerSchemas: /* @__PURE__ */ new Set() };
|
|
144
|
+
}
|
|
145
|
+
if (schema instanceof import_zod.z.ZodNumber) {
|
|
146
|
+
const fmt = schema.format;
|
|
147
|
+
const isInt = fmt === "int32" || fmt === "uint32" || fmt === "safeint";
|
|
148
|
+
const so = { type: isInt ? "integer" : "number" };
|
|
149
|
+
if (schema.minValue !== null && schema.minValue !== Number.MIN_SAFE_INTEGER && Number.isFinite(schema.minValue)) so.minimum = schema.minValue;
|
|
150
|
+
if (schema.maxValue !== null && schema.maxValue !== Number.MAX_SAFE_INTEGER && Number.isFinite(schema.maxValue)) so.maximum = schema.maxValue;
|
|
151
|
+
return { so, selfRequired: true, innerSchemas: /* @__PURE__ */ new Set() };
|
|
152
|
+
}
|
|
153
|
+
if (schema instanceof import_zod.z.ZodBigInt) {
|
|
154
|
+
return leaf({ type: "integer", format: "int64" });
|
|
155
|
+
}
|
|
156
|
+
if (schema instanceof import_zod.z.ZodBoolean) {
|
|
157
|
+
return leaf({ type: "boolean" });
|
|
158
|
+
}
|
|
159
|
+
if (schema instanceof import_zod.z.ZodDate) {
|
|
160
|
+
return leaf({ type: "string", format: "date-time" });
|
|
161
|
+
}
|
|
162
|
+
if (schema instanceof import_zod.z.ZodNull) {
|
|
163
|
+
return { so: { nullable: true }, selfRequired: true, innerSchemas: /* @__PURE__ */ new Set() };
|
|
164
|
+
}
|
|
165
|
+
if (schema instanceof import_zod.z.ZodEnum) {
|
|
166
|
+
return leaf({ enum: schema.options });
|
|
167
|
+
}
|
|
168
|
+
if (schema instanceof import_zod.z.ZodLiteral) {
|
|
169
|
+
return leaf({ enum: [...schema.values] });
|
|
170
|
+
}
|
|
171
|
+
if (schema instanceof import_zod.z.ZodUnion || schema instanceof import_zod.z.ZodDiscriminatedUnion) {
|
|
172
|
+
const innerSchemas = /* @__PURE__ */ new Set();
|
|
173
|
+
const oneOf = schema.options.map((option) => {
|
|
174
|
+
const { so, selfRequired, innerSchemas: innerSchemas_ } = applySwaggerDecorators(option);
|
|
175
|
+
if (!selfRequired) {
|
|
176
|
+
throw new Error("Not required option in oneOf is not supported in Swagger. Use nullable instead.");
|
|
177
|
+
}
|
|
178
|
+
innerSchemas_.forEach((innerSchema) => innerSchemas.add(innerSchema));
|
|
179
|
+
return so.oneOf?.length === 1 ? so.oneOf[0] : so;
|
|
180
|
+
});
|
|
181
|
+
return { so: { oneOf }, selfRequired: true, innerSchemas };
|
|
182
|
+
}
|
|
183
|
+
if (schema instanceof import_zod.z.ZodIntersection) {
|
|
184
|
+
const left = applySwaggerDecorators(schema._zod.def.left);
|
|
185
|
+
const right = applySwaggerDecorators(schema._zod.def.right);
|
|
186
|
+
const innerSchemas = /* @__PURE__ */ new Set([...left.innerSchemas, ...right.innerSchemas]);
|
|
187
|
+
const leftSo = left.so.oneOf?.length === 1 ? left.so.oneOf[0] : left.so;
|
|
188
|
+
const rightSo = right.so.oneOf?.length === 1 ? right.so.oneOf[0] : right.so;
|
|
189
|
+
return { so: { allOf: [leftSo, rightSo] }, selfRequired: true, innerSchemas };
|
|
190
|
+
}
|
|
191
|
+
if (schema instanceof import_zod.z.ZodAny || schema instanceof import_zod.z.ZodUnknown) {
|
|
192
|
+
return leaf({});
|
|
193
|
+
}
|
|
194
|
+
if (schema instanceof import_zod.z.ZodUndefined || schema instanceof import_zod.z.ZodVoid || schema instanceof import_zod.z.ZodNever) {
|
|
195
|
+
throw new Error(`applySwaggerDecorators: ${schema.def.type} cannot be represented in JSON`);
|
|
196
|
+
}
|
|
197
|
+
throw new Error(`applySwaggerDecorators: unsupported Zod type "${schema.def.type}"`);
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
// src/pipe.ts
|
|
201
|
+
var import_common = require("@nestjs/common");
|
|
202
|
+
var import_interfaces = require("@nestjs/common/interfaces");
|
|
203
|
+
var import_zod_dto2 = require("@voznov/zod-dto");
|
|
204
|
+
var ZodValidationPipe = class {
|
|
205
|
+
createError;
|
|
206
|
+
constructor(options) {
|
|
207
|
+
this.createError = options?.createError ?? ((issues) => new import_common.BadRequestException(issues));
|
|
208
|
+
}
|
|
209
|
+
transform(value, { metatype }) {
|
|
210
|
+
if (!metatype || !(0, import_zod_dto2.isZodDtoClass)(metatype)) {
|
|
211
|
+
return value;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
return (0, import_zod_dto2.toDto)(metatype, value);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
if (error instanceof import_zod_dto2.ZodDtoValidationError) {
|
|
217
|
+
throw this.createError(error.issues);
|
|
218
|
+
}
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
ZodValidationPipe = __decorateClass([
|
|
224
|
+
(0, import_common.Injectable)()
|
|
225
|
+
], ZodValidationPipe);
|
|
226
|
+
|
|
227
|
+
// src/index.ts
|
|
228
|
+
(0, import_zod_dto3.registerOnCreate)(applySwaggerDecorators);
|
|
229
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
230
|
+
0 && (module.exports = {
|
|
231
|
+
ZodValidationPipe,
|
|
232
|
+
applySwaggerDecorators
|
|
233
|
+
});
|
|
234
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/swagger.ts","../src/utils.ts","../src/pipe.ts"],"sourcesContent":["import { registerOnCreate } from '@voznov/zod-dto';\nimport { applySwaggerDecorators } from './swagger';\n\n// Auto-register swagger decoration on every ZodDto() creation when this package is imported.\nregisterOnCreate(applySwaggerDecorators);\n\nexport { ZodValidationPipe } from './pipe';\nexport type { ZodValidationPipeOptions } from './pipe';\nexport { applySwaggerDecorators } from './swagger';\n","import { ApiExtraModels, ApiProperty, type ApiPropertyOptions, refs } from '@nestjs/swagger';\nimport { type SchemaObject } from '@nestjs/swagger/dist/interfaces/open-api-spec.interface';\nimport { isZodDtoClass, type ZodDtoClass } from '@voznov/zod-dto';\nimport { z } from 'zod';\nimport { mapValues } from './utils';\n\nconst schemaObjectToApiPropertyOptions = (so: SchemaObject, selfRequired: boolean): ApiPropertyOptions => {\n if ('oneOf' in so || 'anyOf' in so || 'allOf' in so) {\n return { ...so, type: Array, required: selfRequired };\n }\n\n return { ...so, required: selfRequired } as ApiPropertyOptions;\n};\n\nconst leaf = (so: SchemaObject): { so: SchemaObject; selfRequired: boolean; innerSchemas: Set<ZodDtoClass> } => ({\n so,\n selfRequired: true,\n innerSchemas: new Set(),\n});\n\nconst decoratedDtoClasses = new Set<ZodDtoClass>();\n\nexport const applySwaggerDecorators = (schema: z.core.$ZodType): { so: SchemaObject; selfRequired: boolean; innerSchemas: Set<ZodDtoClass> } => {\n // --- Objects ---\n\n if (schema instanceof z.ZodObject) {\n if (isZodDtoClass(schema) && decoratedDtoClasses.has(schema)) {\n return { so: { oneOf: refs(schema) }, selfRequired: true, innerSchemas: new Set([schema]) };\n }\n\n const properties: Record<string, SchemaObject> = {};\n const required: string[] = [];\n const innerSchemas = new Set<ZodDtoClass>();\n Object.entries(schema.shape).forEach(([key, fieldSchema]) => {\n const { so, selfRequired, innerSchemas: innerSchemas_ } = applySwaggerDecorators(fieldSchema as z.ZodType);\n properties[key] = so;\n innerSchemas_.forEach((innerSchema) => innerSchemas.add(innerSchema));\n if (selfRequired) {\n required.push(key);\n }\n });\n\n if (isZodDtoClass(schema)) {\n decoratedDtoClasses.add(schema);\n for (const [key, propertySo] of Object.entries(properties)) {\n ApiProperty(schemaObjectToApiPropertyOptions(propertySo, required.includes(key)))(schema.prototype, key);\n }\n ApiExtraModels(...[...innerSchemas.values()].filter((innerSchema) => innerSchema !== schema))(schema);\n\n return { so: { oneOf: refs(schema) }, selfRequired: true, innerSchemas: new Set([schema]) };\n }\n\n return { so: { type: 'object', properties: mapValues(properties, (so) => (so.oneOf?.length === 1 ? so.oneOf[0] : so)), required }, selfRequired: true, innerSchemas };\n }\n\n if (schema instanceof z.ZodRecord) {\n const { so } = applySwaggerDecorators(schema._zod.def.valueType as z.ZodType);\n\n return leaf({ type: 'object', additionalProperties: so.oneOf?.length === 1 ? so.oneOf[0] : so });\n }\n\n // --- Wrappers (unwrap to inner type) ---\n\n if (schema instanceof z.ZodOptional || schema instanceof z.ZodExactOptional) {\n return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: false };\n }\n\n if (schema instanceof z.ZodDefault) {\n return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: false };\n }\n\n if (schema instanceof z.ZodReadonly || schema instanceof z.ZodLazy || schema instanceof z.ZodCatch) {\n return applySwaggerDecorators(schema.unwrap());\n }\n\n if (schema instanceof z.ZodNonOptional) {\n return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: true };\n }\n\n if (schema instanceof z.ZodNullable) {\n const result = applySwaggerDecorators(schema.unwrap());\n\n return { ...result, so: { ...result.so, nullable: true } };\n }\n\n if (schema instanceof z.ZodPipe) {\n // z.preprocess() creates a Pipe(in: ZodTransform, out: schema).\n // For swagger, skip the transform and process the output schema directly.\n const inner = schema.in instanceof z.ZodTransform ? schema.out : schema.in;\n\n return applySwaggerDecorators(inner);\n }\n\n if (schema instanceof z.ZodTransform) {\n return leaf({});\n }\n\n // --- Arrays & tuples ---\n\n if (schema instanceof z.ZodArray) {\n const element = schema.unwrap();\n const { so, selfRequired: selfRequiredElement, innerSchemas } = applySwaggerDecorators(element);\n if (!selfRequiredElement) {\n throw new Error('Not required array item is not supported in Swagger. Use nullable instead.');\n }\n\n return { so: { type: 'array', items: so.oneOf?.length === 1 ? so.oneOf[0] : so }, selfRequired: true, innerSchemas };\n }\n\n if (schema instanceof z.ZodTuple) {\n const itemSchemas = schema._zod.def.items.map((item) => {\n const { so } = applySwaggerDecorators(item);\n\n return so.oneOf?.length === 1 ? so.oneOf[0] : so;\n });\n\n // Deduplicate by JSON representation to collapse identical types\n const unique = [...new Map(itemSchemas.map((s) => [JSON.stringify(s), s])).values()];\n const items = unique.length === 1 ? unique[0] : { oneOf: unique };\n\n return leaf({ type: 'array', items, minItems: itemSchemas.length, maxItems: itemSchemas.length });\n }\n\n // --- Scalars ---\n\n // ZodString: plain z.string(). ZodStringFormat: z.email(), z.uuid(), z.url(), z.ipv4(), etc.\n // Both share the same properties (minLength, maxLength, format).\n if (schema instanceof z.ZodString || schema instanceof z.ZodStringFormat) {\n const so: SchemaObject = { type: 'string' };\n if (schema.minLength !== null) so.minLength = schema.minLength;\n if (schema.maxLength !== null) so.maxLength = schema.maxLength;\n const fmt = schema.format;\n if (fmt !== null) {\n if (fmt === 'regex') {\n // Grab the first regex pattern from the internal bag.\n const patterns = schema._zod.bag.patterns;\n const first = patterns ? [...patterns][0] : undefined;\n if (first) so.pattern = first.source;\n } else {\n so.format = fmt;\n }\n }\n\n return { so, selfRequired: true, innerSchemas: new Set() };\n }\n\n // Number and integer subtypes (ZodInt, ZodNumberFormat, etc.) — all instanceof ZodNumber.\n if (schema instanceof z.ZodNumber) {\n const fmt = schema.format;\n const isInt = fmt === 'int32' || fmt === 'uint32' || fmt === 'safeint';\n const so: SchemaObject = { type: isInt ? 'integer' : 'number' };\n if (schema.minValue !== null && schema.minValue !== Number.MIN_SAFE_INTEGER && Number.isFinite(schema.minValue)) so.minimum = schema.minValue;\n if (schema.maxValue !== null && schema.maxValue !== Number.MAX_SAFE_INTEGER && Number.isFinite(schema.maxValue)) so.maximum = schema.maxValue;\n\n return { so, selfRequired: true, innerSchemas: new Set() };\n }\n\n if (schema instanceof z.ZodBigInt) {\n return leaf({ type: 'integer', format: 'int64' });\n }\n\n if (schema instanceof z.ZodBoolean) {\n return leaf({ type: 'boolean' });\n }\n\n if (schema instanceof z.ZodDate) {\n return leaf({ type: 'string', format: 'date-time' });\n }\n\n if (schema instanceof z.ZodNull) {\n return { so: { nullable: true }, selfRequired: true, innerSchemas: new Set() };\n }\n\n // --- Enums & literals ---\n\n if (schema instanceof z.ZodEnum) {\n return leaf({ enum: schema.options });\n }\n\n if (schema instanceof z.ZodLiteral) {\n return leaf({ enum: [...schema.values] });\n }\n\n // --- Unions & intersections ---\n\n if (schema instanceof z.ZodUnion || schema instanceof z.ZodDiscriminatedUnion) {\n const innerSchemas = new Set<ZodDtoClass>();\n const oneOf = schema.options.map((option) => {\n const { so, selfRequired, innerSchemas: innerSchemas_ } = applySwaggerDecorators(option);\n\n if (!selfRequired) {\n throw new Error('Not required option in oneOf is not supported in Swagger. Use nullable instead.');\n }\n\n innerSchemas_.forEach((innerSchema) => innerSchemas.add(innerSchema));\n\n // Flatten { oneOf: [single_ref] } → just the ref, so union of DTO refs stays clean.\n return so.oneOf?.length === 1 ? so.oneOf[0] : so;\n });\n\n return { so: { oneOf }, selfRequired: true, innerSchemas };\n }\n\n if (schema instanceof z.ZodIntersection) {\n const left = applySwaggerDecorators(schema._zod.def.left as z.ZodType);\n const right = applySwaggerDecorators(schema._zod.def.right as z.ZodType);\n const innerSchemas = new Set([...left.innerSchemas, ...right.innerSchemas]);\n const leftSo = left.so.oneOf?.length === 1 ? left.so.oneOf[0] : left.so;\n const rightSo = right.so.oneOf?.length === 1 ? right.so.oneOf[0] : right.so;\n\n return { so: { allOf: [leftSo, rightSo] }, selfRequired: true, innerSchemas };\n }\n\n // --- Catch-all ---\n\n if (schema instanceof z.ZodAny || schema instanceof z.ZodUnknown) {\n return leaf({});\n }\n\n if (schema instanceof z.ZodUndefined || schema instanceof z.ZodVoid || schema instanceof z.ZodNever) {\n throw new Error(`applySwaggerDecorators: ${(schema as z.ZodType).def.type} cannot be represented in JSON`);\n }\n\n throw new Error(`applySwaggerDecorators: unsupported Zod type \"${(schema as z.ZodType).def.type}\"`);\n};\n","export const mapValues = <K extends string, V, R>(obj: Record<K, V>, fn: (value: V, key: K) => R): Record<K, R> =>\n Object.fromEntries(Object.entries<V>(obj).map(([key, value]) => [key, fn(value, key as K)])) as Record<K, R>;\n","import { BadRequestException, Injectable, type PipeTransform } from '@nestjs/common';\nimport { type ArgumentMetadata } from '@nestjs/common/interfaces';\nimport { isZodDtoClass, toDto, ZodDtoValidationError } from '@voznov/zod-dto';\n\nexport interface ZodValidationPipeOptions {\n createError?: (issues: string[]) => Error;\n}\n\n@Injectable()\nexport class ZodValidationPipe implements PipeTransform {\n private readonly createError: (issues: string[]) => Error;\n\n constructor(options?: ZodValidationPipeOptions) {\n this.createError = options?.createError ?? ((issues) => new BadRequestException(issues));\n }\n\n transform(value: unknown, { metatype }: ArgumentMetadata): unknown {\n if (!metatype || !isZodDtoClass(metatype)) {\n return value;\n }\n\n try {\n return toDto(metatype, value);\n } catch (error) {\n if (error instanceof ZodDtoValidationError) {\n throw this.createError(error.issues);\n }\n throw error;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,kBAAiC;;;ACAjC,qBAA2E;AAC3E,2BAAkC;AAClC,qBAAgD;AAChD,iBAAkB;;;ACHX,IAAM,YAAY,CAAyB,KAAmB,OACnE,OAAO,YAAY,OAAO,QAAW,GAAG,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM,CAAC,KAAK,GAAG,OAAO,GAAQ,CAAC,CAAC,CAAC;;;ADK7F,IAAM,mCAAmC,CAAC,IAAkB,iBAA8C;AACxG,MAAI,WAAW,MAAM,WAAW,MAAM,WAAW,IAAI;AACnD,WAAO,EAAE,GAAG,IAAI,MAAM,OAAO,UAAU,aAAa;AAAA,EACtD;AAEA,SAAO,EAAE,GAAG,IAAI,UAAU,aAAa;AACzC;AAEA,IAAM,OAAO,CAAC,QAAmG;AAAA,EAC/G;AAAA,EACA,cAAc;AAAA,EACd,cAAc,oBAAI,IAAI;AACxB;AAEA,IAAM,sBAAsB,oBAAI,IAAiB;AAE1C,IAAM,yBAAyB,CAAC,WAAyG;AAG9I,MAAI,kBAAkB,aAAE,WAAW;AACjC,YAAI,8BAAc,MAAM,KAAK,oBAAoB,IAAI,MAAM,GAAG;AAC5D,aAAO,EAAE,IAAI,EAAE,WAAO,qBAAK,MAAM,EAAE,GAAG,cAAc,MAAM,cAAc,oBAAI,IAAI,CAAC,MAAM,CAAC,EAAE;AAAA,IAC5F;AAEA,UAAM,aAA2C,CAAC;AAClD,UAAM,WAAqB,CAAC;AAC5B,UAAM,eAAe,oBAAI,IAAiB;AAC1C,WAAO,QAAQ,OAAO,KAAK,EAAE,QAAQ,CAAC,C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AAM;AAAA,MACrC;AACA,YAAM;AAAA,IACR;AAAA,EACF;AACF;AArBa,oBAAN;AAAA,MADN,0BAAW;AAAA,GACC;;;IHLb,kCAAiB,sBAAsB;","names":["import_zod_dto","import_zod_dto"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { PipeTransform } from '@nestjs/common';
|
|
2
|
+
import { ArgumentMetadata } from '@nestjs/common/interfaces';
|
|
3
|
+
import { SchemaObject } from '@nestjs/swagger/dist/interfaces/open-api-spec.interface';
|
|
4
|
+
import { ZodDtoClass } from '@voznov/zod-dto';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
interface ZodValidationPipeOptions {
|
|
8
|
+
createError?: (issues: string[]) => Error;
|
|
9
|
+
}
|
|
10
|
+
declare class ZodValidationPipe implements PipeTransform {
|
|
11
|
+
private readonly createError;
|
|
12
|
+
constructor(options?: ZodValidationPipeOptions);
|
|
13
|
+
transform(value: unknown, { metatype }: ArgumentMetadata): unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare const applySwaggerDecorators: (schema: z.core.$ZodType) => {
|
|
17
|
+
so: SchemaObject;
|
|
18
|
+
selfRequired: boolean;
|
|
19
|
+
innerSchemas: Set<ZodDtoClass>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { ZodValidationPipe, type ZodValidationPipeOptions, applySwaggerDecorators };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { PipeTransform } from '@nestjs/common';
|
|
2
|
+
import { ArgumentMetadata } from '@nestjs/common/interfaces';
|
|
3
|
+
import { SchemaObject } from '@nestjs/swagger/dist/interfaces/open-api-spec.interface';
|
|
4
|
+
import { ZodDtoClass } from '@voznov/zod-dto';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
interface ZodValidationPipeOptions {
|
|
8
|
+
createError?: (issues: string[]) => Error;
|
|
9
|
+
}
|
|
10
|
+
declare class ZodValidationPipe implements PipeTransform {
|
|
11
|
+
private readonly createError;
|
|
12
|
+
constructor(options?: ZodValidationPipeOptions);
|
|
13
|
+
transform(value: unknown, { metatype }: ArgumentMetadata): unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare const applySwaggerDecorators: (schema: z.core.$ZodType) => {
|
|
17
|
+
so: SchemaObject;
|
|
18
|
+
selfRequired: boolean;
|
|
19
|
+
innerSchemas: Set<ZodDtoClass>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { ZodValidationPipe, type ZodValidationPipeOptions, applySwaggerDecorators };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
5
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
6
|
+
if (decorator = decorators[i])
|
|
7
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
8
|
+
if (kind && result) __defProp(target, key, result);
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// src/index.ts
|
|
13
|
+
import { registerOnCreate } from "@voznov/zod-dto";
|
|
14
|
+
|
|
15
|
+
// src/swagger.ts
|
|
16
|
+
import { ApiExtraModels, ApiProperty, refs } from "@nestjs/swagger";
|
|
17
|
+
import "@nestjs/swagger/dist/interfaces/open-api-spec.interface";
|
|
18
|
+
import { isZodDtoClass } from "@voznov/zod-dto";
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
|
|
21
|
+
// src/utils.ts
|
|
22
|
+
var mapValues = (obj, fn) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, fn(value, key)]));
|
|
23
|
+
|
|
24
|
+
// src/swagger.ts
|
|
25
|
+
var schemaObjectToApiPropertyOptions = (so, selfRequired) => {
|
|
26
|
+
if ("oneOf" in so || "anyOf" in so || "allOf" in so) {
|
|
27
|
+
return { ...so, type: Array, required: selfRequired };
|
|
28
|
+
}
|
|
29
|
+
return { ...so, required: selfRequired };
|
|
30
|
+
};
|
|
31
|
+
var leaf = (so) => ({
|
|
32
|
+
so,
|
|
33
|
+
selfRequired: true,
|
|
34
|
+
innerSchemas: /* @__PURE__ */ new Set()
|
|
35
|
+
});
|
|
36
|
+
var decoratedDtoClasses = /* @__PURE__ */ new Set();
|
|
37
|
+
var applySwaggerDecorators = (schema) => {
|
|
38
|
+
if (schema instanceof z.ZodObject) {
|
|
39
|
+
if (isZodDtoClass(schema) && decoratedDtoClasses.has(schema)) {
|
|
40
|
+
return { so: { oneOf: refs(schema) }, selfRequired: true, innerSchemas: /* @__PURE__ */ new Set([schema]) };
|
|
41
|
+
}
|
|
42
|
+
const properties = {};
|
|
43
|
+
const required = [];
|
|
44
|
+
const innerSchemas = /* @__PURE__ */ new Set();
|
|
45
|
+
Object.entries(schema.shape).forEach(([key, fieldSchema]) => {
|
|
46
|
+
const { so, selfRequired, innerSchemas: innerSchemas_ } = applySwaggerDecorators(fieldSchema);
|
|
47
|
+
properties[key] = so;
|
|
48
|
+
innerSchemas_.forEach((innerSchema) => innerSchemas.add(innerSchema));
|
|
49
|
+
if (selfRequired) {
|
|
50
|
+
required.push(key);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
if (isZodDtoClass(schema)) {
|
|
54
|
+
decoratedDtoClasses.add(schema);
|
|
55
|
+
for (const [key, propertySo] of Object.entries(properties)) {
|
|
56
|
+
ApiProperty(schemaObjectToApiPropertyOptions(propertySo, required.includes(key)))(schema.prototype, key);
|
|
57
|
+
}
|
|
58
|
+
ApiExtraModels(...[...innerSchemas.values()].filter((innerSchema) => innerSchema !== schema))(schema);
|
|
59
|
+
return { so: { oneOf: refs(schema) }, selfRequired: true, innerSchemas: /* @__PURE__ */ new Set([schema]) };
|
|
60
|
+
}
|
|
61
|
+
return { so: { type: "object", properties: mapValues(properties, (so) => so.oneOf?.length === 1 ? so.oneOf[0] : so), required }, selfRequired: true, innerSchemas };
|
|
62
|
+
}
|
|
63
|
+
if (schema instanceof z.ZodRecord) {
|
|
64
|
+
const { so } = applySwaggerDecorators(schema._zod.def.valueType);
|
|
65
|
+
return leaf({ type: "object", additionalProperties: so.oneOf?.length === 1 ? so.oneOf[0] : so });
|
|
66
|
+
}
|
|
67
|
+
if (schema instanceof z.ZodOptional || schema instanceof z.ZodExactOptional) {
|
|
68
|
+
return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: false };
|
|
69
|
+
}
|
|
70
|
+
if (schema instanceof z.ZodDefault) {
|
|
71
|
+
return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: false };
|
|
72
|
+
}
|
|
73
|
+
if (schema instanceof z.ZodReadonly || schema instanceof z.ZodLazy || schema instanceof z.ZodCatch) {
|
|
74
|
+
return applySwaggerDecorators(schema.unwrap());
|
|
75
|
+
}
|
|
76
|
+
if (schema instanceof z.ZodNonOptional) {
|
|
77
|
+
return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: true };
|
|
78
|
+
}
|
|
79
|
+
if (schema instanceof z.ZodNullable) {
|
|
80
|
+
const result = applySwaggerDecorators(schema.unwrap());
|
|
81
|
+
return { ...result, so: { ...result.so, nullable: true } };
|
|
82
|
+
}
|
|
83
|
+
if (schema instanceof z.ZodPipe) {
|
|
84
|
+
const inner = schema.in instanceof z.ZodTransform ? schema.out : schema.in;
|
|
85
|
+
return applySwaggerDecorators(inner);
|
|
86
|
+
}
|
|
87
|
+
if (schema instanceof z.ZodTransform) {
|
|
88
|
+
return leaf({});
|
|
89
|
+
}
|
|
90
|
+
if (schema instanceof z.ZodArray) {
|
|
91
|
+
const element = schema.unwrap();
|
|
92
|
+
const { so, selfRequired: selfRequiredElement, innerSchemas } = applySwaggerDecorators(element);
|
|
93
|
+
if (!selfRequiredElement) {
|
|
94
|
+
throw new Error("Not required array item is not supported in Swagger. Use nullable instead.");
|
|
95
|
+
}
|
|
96
|
+
return { so: { type: "array", items: so.oneOf?.length === 1 ? so.oneOf[0] : so }, selfRequired: true, innerSchemas };
|
|
97
|
+
}
|
|
98
|
+
if (schema instanceof z.ZodTuple) {
|
|
99
|
+
const itemSchemas = schema._zod.def.items.map((item) => {
|
|
100
|
+
const { so } = applySwaggerDecorators(item);
|
|
101
|
+
return so.oneOf?.length === 1 ? so.oneOf[0] : so;
|
|
102
|
+
});
|
|
103
|
+
const unique = [...new Map(itemSchemas.map((s) => [JSON.stringify(s), s])).values()];
|
|
104
|
+
const items = unique.length === 1 ? unique[0] : { oneOf: unique };
|
|
105
|
+
return leaf({ type: "array", items, minItems: itemSchemas.length, maxItems: itemSchemas.length });
|
|
106
|
+
}
|
|
107
|
+
if (schema instanceof z.ZodString || schema instanceof z.ZodStringFormat) {
|
|
108
|
+
const so = { type: "string" };
|
|
109
|
+
if (schema.minLength !== null) so.minLength = schema.minLength;
|
|
110
|
+
if (schema.maxLength !== null) so.maxLength = schema.maxLength;
|
|
111
|
+
const fmt = schema.format;
|
|
112
|
+
if (fmt !== null) {
|
|
113
|
+
if (fmt === "regex") {
|
|
114
|
+
const patterns = schema._zod.bag.patterns;
|
|
115
|
+
const first = patterns ? [...patterns][0] : void 0;
|
|
116
|
+
if (first) so.pattern = first.source;
|
|
117
|
+
} else {
|
|
118
|
+
so.format = fmt;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return { so, selfRequired: true, innerSchemas: /* @__PURE__ */ new Set() };
|
|
122
|
+
}
|
|
123
|
+
if (schema instanceof z.ZodNumber) {
|
|
124
|
+
const fmt = schema.format;
|
|
125
|
+
const isInt = fmt === "int32" || fmt === "uint32" || fmt === "safeint";
|
|
126
|
+
const so = { type: isInt ? "integer" : "number" };
|
|
127
|
+
if (schema.minValue !== null && schema.minValue !== Number.MIN_SAFE_INTEGER && Number.isFinite(schema.minValue)) so.minimum = schema.minValue;
|
|
128
|
+
if (schema.maxValue !== null && schema.maxValue !== Number.MAX_SAFE_INTEGER && Number.isFinite(schema.maxValue)) so.maximum = schema.maxValue;
|
|
129
|
+
return { so, selfRequired: true, innerSchemas: /* @__PURE__ */ new Set() };
|
|
130
|
+
}
|
|
131
|
+
if (schema instanceof z.ZodBigInt) {
|
|
132
|
+
return leaf({ type: "integer", format: "int64" });
|
|
133
|
+
}
|
|
134
|
+
if (schema instanceof z.ZodBoolean) {
|
|
135
|
+
return leaf({ type: "boolean" });
|
|
136
|
+
}
|
|
137
|
+
if (schema instanceof z.ZodDate) {
|
|
138
|
+
return leaf({ type: "string", format: "date-time" });
|
|
139
|
+
}
|
|
140
|
+
if (schema instanceof z.ZodNull) {
|
|
141
|
+
return { so: { nullable: true }, selfRequired: true, innerSchemas: /* @__PURE__ */ new Set() };
|
|
142
|
+
}
|
|
143
|
+
if (schema instanceof z.ZodEnum) {
|
|
144
|
+
return leaf({ enum: schema.options });
|
|
145
|
+
}
|
|
146
|
+
if (schema instanceof z.ZodLiteral) {
|
|
147
|
+
return leaf({ enum: [...schema.values] });
|
|
148
|
+
}
|
|
149
|
+
if (schema instanceof z.ZodUnion || schema instanceof z.ZodDiscriminatedUnion) {
|
|
150
|
+
const innerSchemas = /* @__PURE__ */ new Set();
|
|
151
|
+
const oneOf = schema.options.map((option) => {
|
|
152
|
+
const { so, selfRequired, innerSchemas: innerSchemas_ } = applySwaggerDecorators(option);
|
|
153
|
+
if (!selfRequired) {
|
|
154
|
+
throw new Error("Not required option in oneOf is not supported in Swagger. Use nullable instead.");
|
|
155
|
+
}
|
|
156
|
+
innerSchemas_.forEach((innerSchema) => innerSchemas.add(innerSchema));
|
|
157
|
+
return so.oneOf?.length === 1 ? so.oneOf[0] : so;
|
|
158
|
+
});
|
|
159
|
+
return { so: { oneOf }, selfRequired: true, innerSchemas };
|
|
160
|
+
}
|
|
161
|
+
if (schema instanceof z.ZodIntersection) {
|
|
162
|
+
const left = applySwaggerDecorators(schema._zod.def.left);
|
|
163
|
+
const right = applySwaggerDecorators(schema._zod.def.right);
|
|
164
|
+
const innerSchemas = /* @__PURE__ */ new Set([...left.innerSchemas, ...right.innerSchemas]);
|
|
165
|
+
const leftSo = left.so.oneOf?.length === 1 ? left.so.oneOf[0] : left.so;
|
|
166
|
+
const rightSo = right.so.oneOf?.length === 1 ? right.so.oneOf[0] : right.so;
|
|
167
|
+
return { so: { allOf: [leftSo, rightSo] }, selfRequired: true, innerSchemas };
|
|
168
|
+
}
|
|
169
|
+
if (schema instanceof z.ZodAny || schema instanceof z.ZodUnknown) {
|
|
170
|
+
return leaf({});
|
|
171
|
+
}
|
|
172
|
+
if (schema instanceof z.ZodUndefined || schema instanceof z.ZodVoid || schema instanceof z.ZodNever) {
|
|
173
|
+
throw new Error(`applySwaggerDecorators: ${schema.def.type} cannot be represented in JSON`);
|
|
174
|
+
}
|
|
175
|
+
throw new Error(`applySwaggerDecorators: unsupported Zod type "${schema.def.type}"`);
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// src/pipe.ts
|
|
179
|
+
import { BadRequestException, Injectable } from "@nestjs/common";
|
|
180
|
+
import "@nestjs/common/interfaces";
|
|
181
|
+
import { isZodDtoClass as isZodDtoClass2, toDto, ZodDtoValidationError } from "@voznov/zod-dto";
|
|
182
|
+
var ZodValidationPipe = class {
|
|
183
|
+
createError;
|
|
184
|
+
constructor(options) {
|
|
185
|
+
this.createError = options?.createError ?? ((issues) => new BadRequestException(issues));
|
|
186
|
+
}
|
|
187
|
+
transform(value, { metatype }) {
|
|
188
|
+
if (!metatype || !isZodDtoClass2(metatype)) {
|
|
189
|
+
return value;
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
return toDto(metatype, value);
|
|
193
|
+
} catch (error) {
|
|
194
|
+
if (error instanceof ZodDtoValidationError) {
|
|
195
|
+
throw this.createError(error.issues);
|
|
196
|
+
}
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
ZodValidationPipe = __decorateClass([
|
|
202
|
+
Injectable()
|
|
203
|
+
], ZodValidationPipe);
|
|
204
|
+
|
|
205
|
+
// src/index.ts
|
|
206
|
+
registerOnCreate(applySwaggerDecorators);
|
|
207
|
+
export {
|
|
208
|
+
ZodValidationPipe,
|
|
209
|
+
applySwaggerDecorators
|
|
210
|
+
};
|
|
211
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/swagger.ts","../src/utils.ts","../src/pipe.ts"],"sourcesContent":["import { registerOnCreate } from '@voznov/zod-dto';\nimport { applySwaggerDecorators } from './swagger';\n\n// Auto-register swagger decoration on every ZodDto() creation when this package is imported.\nregisterOnCreate(applySwaggerDecorators);\n\nexport { ZodValidationPipe } from './pipe';\nexport type { ZodValidationPipeOptions } from './pipe';\nexport { applySwaggerDecorators } from './swagger';\n","import { ApiExtraModels, ApiProperty, type ApiPropertyOptions, refs } from '@nestjs/swagger';\nimport { type SchemaObject } from '@nestjs/swagger/dist/interfaces/open-api-spec.interface';\nimport { isZodDtoClass, type ZodDtoClass } from '@voznov/zod-dto';\nimport { z } from 'zod';\nimport { mapValues } from './utils';\n\nconst schemaObjectToApiPropertyOptions = (so: SchemaObject, selfRequired: boolean): ApiPropertyOptions => {\n if ('oneOf' in so || 'anyOf' in so || 'allOf' in so) {\n return { ...so, type: Array, required: selfRequired };\n }\n\n return { ...so, required: selfRequired } as ApiPropertyOptions;\n};\n\nconst leaf = (so: SchemaObject): { so: SchemaObject; selfRequired: boolean; innerSchemas: Set<ZodDtoClass> } => ({\n so,\n selfRequired: true,\n innerSchemas: new Set(),\n});\n\nconst decoratedDtoClasses = new Set<ZodDtoClass>();\n\nexport const applySwaggerDecorators = (schema: z.core.$ZodType): { so: SchemaObject; selfRequired: boolean; innerSchemas: Set<ZodDtoClass> } => {\n // --- Objects ---\n\n if (schema instanceof z.ZodObject) {\n if (isZodDtoClass(schema) && decoratedDtoClasses.has(schema)) {\n return { so: { oneOf: refs(schema) }, selfRequired: true, innerSchemas: new Set([schema]) };\n }\n\n const properties: Record<string, SchemaObject> = {};\n const required: string[] = [];\n const innerSchemas = new Set<ZodDtoClass>();\n Object.entries(schema.shape).forEach(([key, fieldSchema]) => {\n const { so, selfRequired, innerSchemas: innerSchemas_ } = applySwaggerDecorators(fieldSchema as z.ZodType);\n properties[key] = so;\n innerSchemas_.forEach((innerSchema) => innerSchemas.add(innerSchema));\n if (selfRequired) {\n required.push(key);\n }\n });\n\n if (isZodDtoClass(schema)) {\n decoratedDtoClasses.add(schema);\n for (const [key, propertySo] of Object.entries(properties)) {\n ApiProperty(schemaObjectToApiPropertyOptions(propertySo, required.includes(key)))(schema.prototype, key);\n }\n ApiExtraModels(...[...innerSchemas.values()].filter((innerSchema) => innerSchema !== schema))(schema);\n\n return { so: { oneOf: refs(schema) }, selfRequired: true, innerSchemas: new Set([schema]) };\n }\n\n return { so: { type: 'object', properties: mapValues(properties, (so) => (so.oneOf?.length === 1 ? so.oneOf[0] : so)), required }, selfRequired: true, innerSchemas };\n }\n\n if (schema instanceof z.ZodRecord) {\n const { so } = applySwaggerDecorators(schema._zod.def.valueType as z.ZodType);\n\n return leaf({ type: 'object', additionalProperties: so.oneOf?.length === 1 ? so.oneOf[0] : so });\n }\n\n // --- Wrappers (unwrap to inner type) ---\n\n if (schema instanceof z.ZodOptional || schema instanceof z.ZodExactOptional) {\n return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: false };\n }\n\n if (schema instanceof z.ZodDefault) {\n return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: false };\n }\n\n if (schema instanceof z.ZodReadonly || schema instanceof z.ZodLazy || schema instanceof z.ZodCatch) {\n return applySwaggerDecorators(schema.unwrap());\n }\n\n if (schema instanceof z.ZodNonOptional) {\n return { ...applySwaggerDecorators(schema.unwrap()), selfRequired: true };\n }\n\n if (schema instanceof z.ZodNullable) {\n const result = applySwaggerDecorators(schema.unwrap());\n\n return { ...result, so: { ...result.so, nullable: true } };\n }\n\n if (schema instanceof z.ZodPipe) {\n // z.preprocess() creates a Pipe(in: ZodTransform, out: schema).\n // For swagger, skip the transform and process the output schema directly.\n const inner = schema.in instanceof z.ZodTransform ? schema.out : schema.in;\n\n return applySwaggerDecorators(inner);\n }\n\n if (schema instanceof z.ZodTransform) {\n return leaf({});\n }\n\n // --- Arrays & tuples ---\n\n if (schema instanceof z.ZodArray) {\n const element = schema.unwrap();\n const { so, selfRequired: selfRequiredElement, innerSchemas } = applySwaggerDecorators(element);\n if (!selfRequiredElement) {\n throw new Error('Not required array item is not supported in Swagger. Use nullable instead.');\n }\n\n return { so: { type: 'array', items: so.oneOf?.length === 1 ? so.oneOf[0] : so }, selfRequired: true, innerSchemas };\n }\n\n if (schema instanceof z.ZodTuple) {\n const itemSchemas = schema._zod.def.items.map((item) => {\n const { so } = applySwaggerDecorators(item);\n\n return so.oneOf?.length === 1 ? so.oneOf[0] : so;\n });\n\n // Deduplicate by JSON representation to collapse identical types\n const unique = [...new Map(itemSchemas.map((s) => [JSON.stringify(s), s])).values()];\n const items = unique.length === 1 ? unique[0] : { oneOf: unique };\n\n return leaf({ type: 'array', items, minItems: itemSchemas.length, maxItems: itemSchemas.length });\n }\n\n // --- Scalars ---\n\n // ZodString: plain z.string(). ZodStringFormat: z.email(), z.uuid(), z.url(), z.ipv4(), etc.\n // Both share the same properties (minLength, maxLength, format).\n if (schema instanceof z.ZodString || schema instanceof z.ZodStringFormat) {\n const so: SchemaObject = { type: 'string' };\n if (schema.minLength !== null) so.minLength = schema.minLength;\n if (schema.maxLength !== null) so.maxLength = schema.maxLength;\n const fmt = schema.format;\n if (fmt !== null) {\n if (fmt === 'regex') {\n // Grab the first regex pattern from the internal bag.\n const patterns = schema._zod.bag.patterns;\n const first = patterns ? [...patterns][0] : undefined;\n if (first) so.pattern = first.source;\n } else {\n so.format = fmt;\n }\n }\n\n return { so, selfRequired: true, innerSchemas: new Set() };\n }\n\n // Number and integer subtypes (ZodInt, ZodNumberFormat, etc.) — all instanceof ZodNumber.\n if (schema instanceof z.ZodNumber) {\n const fmt = schema.format;\n const isInt = fmt === 'int32' || fmt === 'uint32' || fmt === 'safeint';\n const so: SchemaObject = { type: isInt ? 'integer' : 'number' };\n if (schema.minValue !== null && schema.minValue !== Number.MIN_SAFE_INTEGER && Number.isFinite(schema.minValue)) so.minimum = schema.minValue;\n if (schema.maxValue !== null && schema.maxValue !== Number.MAX_SAFE_INTEGER && Number.isFinite(schema.maxValue)) so.maximum = schema.maxValue;\n\n return { so, selfRequired: true, innerSchemas: new Set() };\n }\n\n if (schema instanceof z.ZodBigInt) {\n return leaf({ type: 'integer', format: 'int64' });\n }\n\n if (schema instanceof z.ZodBoolean) {\n return leaf({ type: 'boolean' });\n }\n\n if (schema instanceof z.ZodDate) {\n return leaf({ type: 'string', format: 'date-time' });\n }\n\n if (schema instanceof z.ZodNull) {\n return { so: { nullable: true }, selfRequired: true, innerSchemas: new Set() };\n }\n\n // --- Enums & literals ---\n\n if (schema instanceof z.ZodEnum) {\n return leaf({ enum: schema.options });\n }\n\n if (schema instanceof z.ZodLiteral) {\n return leaf({ enum: [...schema.values] });\n }\n\n // --- Unions & intersections ---\n\n if (schema instanceof z.ZodUnion || schema instanceof z.ZodDiscriminatedUnion) {\n const innerSchemas = new Set<ZodDtoClass>();\n const oneOf = schema.options.map((option) => {\n const { so, selfRequired, innerSchemas: innerSchemas_ } = applySwaggerDecorators(option);\n\n if (!selfRequired) {\n throw new Error('Not required option in oneOf is not supported in Swagger. Use nullable instead.');\n }\n\n innerSchemas_.forEach((innerSchema) => innerSchemas.add(innerSchema));\n\n // Flatten { oneOf: [single_ref] } → just the ref, so union of DTO refs stays clean.\n return so.oneOf?.length === 1 ? so.oneOf[0] : so;\n });\n\n return { so: { oneOf }, selfRequired: true, innerSchemas };\n }\n\n if (schema instanceof z.ZodIntersection) {\n const left = applySwaggerDecorators(schema._zod.def.left as z.ZodType);\n const right = applySwaggerDecorators(schema._zod.def.right as z.ZodType);\n const innerSchemas = new Set([...left.innerSchemas, ...right.innerSchemas]);\n const leftSo = left.so.oneOf?.length === 1 ? left.so.oneOf[0] : left.so;\n const rightSo = right.so.oneOf?.length === 1 ? right.so.oneOf[0] : right.so;\n\n return { so: { allOf: [leftSo, rightSo] }, selfRequired: true, innerSchemas };\n }\n\n // --- Catch-all ---\n\n if (schema instanceof z.ZodAny || schema instanceof z.ZodUnknown) {\n return leaf({});\n }\n\n if (schema instanceof z.ZodUndefined || schema instanceof z.ZodVoid || schema instanceof z.ZodNever) {\n throw new Error(`applySwaggerDecorators: ${(schema as z.ZodType).def.type} cannot be represented in JSON`);\n }\n\n throw new Error(`applySwaggerDecorators: unsupported Zod type \"${(schema as z.ZodType).def.type}\"`);\n};\n","export const mapValues = <K extends string, V, R>(obj: Record<K, V>, fn: (value: V, key: K) => R): Record<K, R> =>\n Object.fromEntries(Object.entries<V>(obj).map(([key, value]) => [key, fn(value, key as K)])) as Record<K, R>;\n","import { BadRequestException, Injectable, type PipeTransform } from '@nestjs/common';\nimport { type ArgumentMetadata } from '@nestjs/common/interfaces';\nimport { isZodDtoClass, toDto, ZodDtoValidationError } from '@voznov/zod-dto';\n\nexport interface ZodValidationPipeOptions {\n createError?: (issues: string[]) => Error;\n}\n\n@Injectable()\nexport class ZodValidationPipe implements PipeTransform {\n private readonly createError: (issues: string[]) => Error;\n\n constructor(options?: ZodValidationPipeOptions) {\n this.createError = options?.createError ?? ((issues) => new BadRequestException(issues));\n }\n\n transform(value: unknown, { metatype }: ArgumentMetadata): unknown {\n if (!metatype || !isZodDtoClass(metatype)) {\n return value;\n }\n\n try {\n return toDto(metatype, value);\n } catch (error) {\n if (error instanceof ZodDtoValidationError) {\n throw this.createError(error.issues);\n }\n throw error;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;AAAA,SAAS,wBAAwB;;;ACAjC,SAAS,gBAAgB,aAAsC,YAAY;AAC3E,OAA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|
|
4
|
+
"description": "NestJS adapter for @voznov/zod-dto — validation pipe, Swagger generation",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"zod",
|
|
7
|
+
"dto",
|
|
8
|
+
"nestjs",
|
|
9
|
+
"validation",
|
|
10
|
+
"openapi",
|
|
11
|
+
"swagger",
|
|
12
|
+
"typescript"
|
|
13
|
+
],
|
|
14
|
+
"author": "Petr Voznov",
|
|
15
|
+
"license": "Apache-2.0",
|
|
16
|
+
"homepage": "https://github.com/Voznov/zod-dto/tree/master/packages/nestjs#readme",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/Voznov/zod-dto.git",
|
|
20
|
+
"directory": "packages/nestjs"
|
|
21
|
+
},
|
|
22
|
+
"bugs": "https://github.com/Voznov/zod-dto/issues",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.cjs",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"default": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"require": {
|
|
34
|
+
"types": "./dist/index.d.cts",
|
|
35
|
+
"default": "./dist/index.cjs"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"LICENSE",
|
|
42
|
+
"NOTICE"
|
|
43
|
+
],
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@nestjs/common": ">=10.0.0",
|
|
49
|
+
"@nestjs/swagger": ">=7.0.0",
|
|
50
|
+
"zod": ">=4.0.0",
|
|
51
|
+
"@voznov/zod-dto": "^0.1.0"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "tsup",
|
|
58
|
+
"typecheck": "tsc --noEmit"
|
|
59
|
+
}
|
|
60
|
+
}
|