@origins-digital/zod-openapi-utils 0.0.4 → 0.0.6

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/README.md CHANGED
@@ -1 +1,98 @@
1
- # Origins Digital Zod API Utils
1
+ # @origins-digital/zod-openapi-utils
2
+
3
+ A utility package that bridges Zod schemas with OpenAPI/Swagger documentation in NestJS applications.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @origins-digital/zod-openapi-utils
9
+ ```
10
+
11
+ ## Features
12
+
13
+ - Automatic conversion of Zod schemas to OpenAPI/Swagger schemas
14
+ - Support for all Zod types and validations
15
+ - Type-safe API documentation
16
+ - Seamless integration with NestJS Swagger
17
+ - Custom format support (email, URL, UUID, CUID, etc.)
18
+
19
+ ## Usage
20
+
21
+ ### Basic Setup
22
+
23
+ ```typescript
24
+ import { Module } from '@nestjs/common';
25
+ import { makeOpenAPIComponents } from '@origins-digital/zod-openapi-utils';
26
+ import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
27
+
28
+ @Module({})
29
+ export class AppModule {
30
+ constructor() {
31
+ const config = new DocumentBuilder()
32
+ .setTitle('Your API')
33
+ .setDescription('API description')
34
+ .setVersion('1.0')
35
+ .build();
36
+
37
+ const document = SwaggerModule.createDocument(app, config, {
38
+ extraModels: makeOpenAPIComponents([
39
+ // Your Zod schemas here
40
+ ]),
41
+ });
42
+
43
+ SwaggerModule.setup('api', app, document);
44
+ }
45
+ }
46
+ ```
47
+
48
+ ### Using Zod Schemas with OpenAPI
49
+
50
+ ```typescript
51
+ import { z } from 'zod';
52
+ import { createZodDto } from '@origins-digital/zod-openapi-utils';
53
+
54
+ // Define your Zod schema
55
+ const UserSchema = z
56
+ .object({
57
+ id: z.string().uuid(),
58
+ email: z.string().email(),
59
+ name: z.string().min(2).max(100),
60
+ age: z.number().int().min(0).max(120),
61
+ createdAt: z.date(),
62
+ password: z.string().min(8).password(),
63
+ })
64
+ .dto('UserSchema');
65
+
66
+ // Create DTO from Zod schema
67
+ export class CreateUserDto extends createZodDto(UserSchema) {}
68
+
69
+ // Use it in your controller
70
+ @Controller('users')
71
+ export class UsersController {
72
+ @Post()
73
+ create(@Body() createUserDto: CreateUserDto) {
74
+ // The DTO will be validated against the Zod schema
75
+ return this.usersService.create(createUserDto);
76
+ }
77
+ }
78
+ ```
79
+
80
+ ### Supported Zod Types and Validations
81
+
82
+ - **Strings**: email, URL, UUID, CUID, regex patterns, min/max length
83
+ - **Numbers**: integers, min/max values, multipleOf
84
+ - **Dates**: date-time format
85
+ - **Booleans**
86
+ - **Objects**: nested schemas
87
+ - **Arrays**: min/max length
88
+ - **Unions and Enums**
89
+ - **Custom Formats**: password, time, etc.
90
+ - **Nested types**: This package is also able to detect nested types when using the .dto method, and create the reference in the openapi spec.
91
+
92
+ ## Error Handling
93
+
94
+ The package includes a custom exception handler for Zod validation errors that integrates with NestJS's exception handling system.
95
+
96
+ ## Contributing
97
+
98
+ Contributions are welcome! Please feel free to submit a Pull Request.
package/dist/index.d.ts CHANGED
@@ -2,4 +2,5 @@ export * from './openapi/zod.extension';
2
2
  export * from './openapi/zod-to-openapi';
3
3
  export * from './openapi/make-open-api';
4
4
  export * from './openapi/dto';
5
+ export * from './openapi/zod.exception';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,eAAe,CAAC;AAC9B,cAAc,yBAAyB,CAAC"}
package/dist/index.js CHANGED
@@ -18,4 +18,5 @@ __exportStar(require("./openapi/zod.extension"), exports);
18
18
  __exportStar(require("./openapi/zod-to-openapi"), exports);
19
19
  __exportStar(require("./openapi/make-open-api"), exports);
20
20
  __exportStar(require("./openapi/dto"), exports);
21
+ __exportStar(require("./openapi/zod.exception"), exports);
21
22
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0DAAwC;AACxC,2DAAyC;AACzC,0DAAwC;AACxC,gDAA8B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0DAAwC;AACxC,2DAAyC;AACzC,0DAAwC;AACxC,gDAA8B;AAC9B,0DAAwC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@origins-digital/zod-openapi-utils",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Origins Zod OpenAPI Utils",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.d.ts",
@@ -97,6 +97,24 @@
97
97
  "path": [
98
98
  "dependencies.@origins-digital/zod-openapi-utils"
99
99
  ]
100
+ },
101
+ {
102
+ "file": "../query-filter/core/package.json",
103
+ "path": [
104
+ "dependencies.@origins-digital/zod-openapi-utils"
105
+ ]
106
+ },
107
+ {
108
+ "file": "../backoffice/package.json",
109
+ "path": [
110
+ "dependencies.@origins-digital/zod-openapi-utils"
111
+ ]
112
+ },
113
+ {
114
+ "file": "../nestjs-commons/package.json",
115
+ "path": [
116
+ "dependencies.@origins-digital/zod-openapi-utils"
117
+ ]
100
118
  }
101
119
  ]
102
120
  }