You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: MIGRATION.md
+50Lines changed: 50 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,55 @@
1
1
# Migration
2
2
3
+
## From version 4.x to 5.x
4
+
### `patchNestJsSwagger` has been removed
5
+
There is no need to call `patchNestJsSwagger` anymore. The call to `patchNestJsSwagger` can simply be removed:
6
+
```diff
7
+
- patchNestJsSwagger()
8
+
```
9
+
10
+
### Deprecated `createZodGuard`, `UseZodGuard`, and `ZodGuard`
11
+
12
+
`createZodGuard` and friends have been deprecated. This was a mistake to add to the library, for a few reasons:
13
+
1. It clearly states in the nestjs documentation that guards are only meant for authorization, not for validation:
14
+
15
+
> Guards have a single responsibility. They determine whether a given request will be handled by the route handler or not, depending on certain conditions (like permissions, roles, ACLs, etc.) present at run-time. This is often referred to as authorization
16
+
17
+
2.`createZodGuard` uses `validate` which is also deprecated
18
+
3.`createZodGuard` didn't do anything special. You can create your own guard that does the same thing with very few lines of code. However, it's recommended to use guards for authorization, not exclusively validation. That's not to say you can't use zod inside a guard, but the main purpose of the guard should be authorization.
19
+
20
+
### Deprecated `validate`
21
+
`validate` was a redudant and confusingly named function (it should have been called `parse` if we wanted to keep it). You can simply use the `.parse` function that is attached to the zod schema:
22
+
23
+
```ts
24
+
const MySchema =z.object(...);
25
+
const knownData =MySchema.parse(unknownData);
26
+
```
27
+
28
+
Or if you have a DTO:
29
+
```ts
30
+
classPostDtoextendscreateZodDto(...) {}
31
+
PostDto.schema.parse(...)
32
+
```
33
+
34
+
### Deprecated `zodToOpenAPI`
35
+
[Zod v4](https://v4.zod.dev/v4#json-schema-conversion) introduces a built-in method of converting zod schemas to OpenAPI, so this function is no longer needed.
(it can be useful when you want to do that before other guards)
30
-
-`UseZodGuard` - alias for `@UseGuards(new ZodGuard(source, schema))`
31
28
-`ZodValidationException` - BadRequestException extended with Zod errors
32
29
-`zodToOpenAPI` - create OpenAPI declarations from Zod schemas
33
30
- OpenAPI support
34
31
-`@nestjs/swagger` integration using the patch
35
32
-`zodToOpenAPI` - generate highly accurate Swagger Schema
36
33
- Zod DTOs can be used in any `@nestjs/swagger` decorator
37
-
- Extended Zod schemas for NestJS (`@nest-zod/z`)
38
-
-**Note:**_`@nest-zod/z` is deprecated and will not be supported soon. It is recommended to use `zod` directly. See [MIGRATION.md](./MIGRATION.md) for more information._
39
-
-`dateString` for dates (supports casting to `Date`)
40
-
-`password` for passwords (more complex string rules + OpenAPI conversion)
41
34
- Customization - change exception format easily
42
-
- Useful helpers for client side error handling (`nestjs-zod/frontend`)
43
35
44
36
## Installation
45
37
@@ -52,7 +44,7 @@ Peer dependencies:
52
44
-`zod` - `>= 3.14.3`
53
45
-`@nestjs/common` - `>= 8.0.0` (required on server side)
54
46
-`@nestjs/core` - `>= 8.0.0` (required on server side)
55
-
-`@nestjs/swagger` - `>= 5.0.0` (only when using `patchNestJsSwagger`)
47
+
-`@nestjs/swagger` - `>= 5.0.0`
56
48
57
49
All peer dependencies are marked as optional for better client side usage, but you need to install required ones when using `nestjs-zod` on server side.
> `ZodGuard` is deprecated and will not be supported soon. It is recommended to use guards for authorization, not validation. See [MIGRATION.md](./MIGRATION.md) for more information.
197
+
203
198
Sometimes, we need to validate user input before specific Guards. We can't use Validation Pipe since NestJS Pipes are always executed after Guards.
204
199
205
200
The solution is `ZodGuard`. It works just like `ZodValidationPipe`, except for that is doesn't transform the input.
@@ -234,6 +229,9 @@ class MyController {
234
229
235
230
### Creating custom guard
236
231
232
+
> [!CAUTION]
233
+
> `createZodGuard` is deprecated and will not be supported soon. It is recommended to use guards for authorization, not validation. See [MIGRATION.md](./MIGRATION.md) for more information.
> `validate` is deprecated and will not be supported soon. It is recommended to use `.parse` directly. See [MIGRATION.md](./MIGRATION.md) for more information.
249
+
249
250
If you don't like `ZodGuard` and `ZodValidationPipe`, you can use `validate` function:
250
251
251
252
```ts
@@ -561,19 +562,13 @@ function mapToFormErrors(issues: ZodIssue[]) {
561
562
562
563
## OpenAPI (Swagger) support
563
564
564
-
### Setup
565
-
566
-
Prerequisites:
565
+
> [!Note]
566
+
> There used to be a function called `patchNestJsSwagger`. This function has been removed since it is no longer neccessary to call.
567
567
568
-
-`@nestjs/swagger` with version `^5.0.0` installed
569
-
570
-
Apply the patch `patchNestJsSwagger()` in your `main.ts` file before setting up your swagger module:
571
-
572
-
```ts
573
-
import { patchNestJsSwagger } from'nestjs-zod'
574
-
575
-
patchNestJsSwagger()
576
-
```
568
+
If you have `@nestjs/swagger` setup, documentation will automatically be generated for:
569
+
- Request bodies, if you use `@Body() body: MyDto`
570
+
- Response bodies, if you use `@ApiOkResponse({ type: MyDto })`
571
+
- Query params, if you use `@Query() query: MyQueryParamsDto`
577
572
578
573
For addtional documentation, follow the [Nest.js' Swagger Module Guide](https://docs.nestjs.com/openapi/introduction), or you can see the example application guide [here](/packages/example/) .
> `zodToOpenAPI` is deprecated and will not be supported soon, since zod v4 adds built-in support for generating OpenAPI schemas from zod scehams. See [MIGRATION.md](./MIGRATION.md) for more information.
592
+
595
593
You can convert any Zod schema to an OpenAPI JSON object:
0 commit comments