Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLorantfy committed Oct 15, 2024
1 parent 2d41ed7 commit b4f5fab
Show file tree
Hide file tree
Showing 9 changed files with 563 additions and 258 deletions.
2 changes: 1 addition & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The extended zod api was moved out of the main package to a separate package. T
```
Additionally, `@nestjs-zod/z` is deprecated and will not be supported soon. This is because the way `@nestjs-zod/z` extends `zod` is brittle and breaks in patch versions of zod. If you still want to use the functionality of `password` and `dateString`, you can implement the same logic using [refine()](https://zod.dev/?id=refine)

> [!IMPORTANT]
> [!CAUTION]
> It is highly recommended to move towards importing `zod` directly, instead of `@nestjs-zod/z`
### `nestjs-zod/frontend` is removed
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,14 @@ In the above example, despite the `userService.findOne` method returns `password

## Extended Zod

> [!IMPORTANT]
> [!CAUTION]
> `@nestjs-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.
`@nestjs-zod/z` provides a special version of Zod. It helps you to validate the user input more accurately by using our custom schemas and methods.

### ZodDateString

> [!IMPORTANT]
> [!CAUTION]
> `@nestjs-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.
In HTTP, we always accept Dates as strings. But default Zod only has validations for full date-time strings. `ZodDateString` was created to address this issue.
Expand Down Expand Up @@ -442,7 +442,7 @@ Errors:

### ZodPassword

> [!IMPORTANT]
> [!CAUTION]
> `@nestjs-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.
`ZodPassword` is a string-like type, just like the `ZodDateString`. As you might have guessed, it's intended to help you with password schemas definition.
Expand Down Expand Up @@ -483,7 +483,7 @@ Errors:

### Json Schema

> [!IMPORTANT]
> [!CAUTION]
> `@nestjs-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.
> Created for `nestjs-zod-prisma`
Expand All @@ -494,7 +494,7 @@ z.json()

### "from" function

> [!IMPORTANT]
> [!CAUTION]
> `@nestjs-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.
> Created for custom schemas in `nestjs-zod-prisma`
Expand All @@ -507,7 +507,7 @@ z.from(MySchema)

### Extended Zod Errors

> [!IMPORTANT]
> [!CAUTION]
> `@nestjs-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.
Currently, we use `custom` error code due to some Zod limitations (`errorMap` priorities)
Expand All @@ -531,7 +531,7 @@ const error = {

### Working with errors on the client side

> [!IMPORTANT]
> [!CAUTION]
> `@nestjs-zod/z/frontend` is deprecated and will not be supported soon. It is recommended to use `zod` directly. See [MIGRATION.md](./MIGRATION.md) for more information.

Expand Down
11 changes: 8 additions & 3 deletions packages/nestjs-zod/src/dto.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { createZodDto } from './dto'
import { z } from '@nestjs-zod/z'
import { z as actualZod } from 'zod'
import { z as nestjsZod } from '@nestjs-zod/z'

describe('createZodDto', () => {
describe.each([
['zod', actualZod],
['@nestjs-zod/z', nestjsZod],
])('createZodDto (using %s)', (description, z) => {
it('should correctly create DTO', () => {
const UserSchema = z.object({
username: z.string(),
Expand All @@ -23,4 +27,5 @@ describe('createZodDto', () => {
password: 'strong',
})
})
})
});

9 changes: 7 additions & 2 deletions packages/nestjs-zod/src/exception.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { BadRequestException, HttpStatus } from '@nestjs/common'
import { ZodValidationException } from './exception'
import { z } from '@nestjs-zod/z'

describe('ZodValidationException', () => {
import { z as actualZod } from 'zod'
import { z as nestjsZod } from '@nestjs-zod/z'

describe.each([
['zod', actualZod],
['@nestjs-zod/z', nestjsZod],
])('ZodValidationException (using %s)', (description, z) => {
it('should correctly create exception', () => {
const UserSchema = z.object({
username: z.string(),
Expand Down
9 changes: 7 additions & 2 deletions packages/nestjs-zod/src/guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { createZodDto } from './dto'
import { ZodValidationException } from './exception'
import { ZodGuard } from './guard'
import { Source } from './shared/types'
import { z } from '@nestjs-zod/z'

describe('ZodGuard', () => {
import { z as actualZod } from 'zod'
import { z as nestjsZod } from '@nestjs-zod/z'

describe.each([
['zod', actualZod],
['@nestjs-zod/z', nestjsZod],
])('ZodGuard (using %s)', (description, z) => {
const UserSchema = z.object({
username: z.string(),
password: z.string(),
Expand Down
Loading

0 comments on commit b4f5fab

Please sign in to comment.