@origins-digital/zod-openapi-utils 0.0.5 → 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 +98 -1
- package/package.json +19 -1
package/README.md
CHANGED
|
@@ -1 +1,98 @@
|
|
|
1
|
-
#
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@origins-digital/zod-openapi-utils",
|
|
3
|
-
"version": "0.0.
|
|
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
|
}
|