-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split extended zod into separate package
@nest-zod/z
(#105)
- Loading branch information
1 parent
4e3564b
commit 4f65783
Showing
57 changed files
with
1,086 additions
and
383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,24 +19,35 @@ jobs: | |
node-version: 20 | ||
cache: 'pnpm' | ||
- run: pnpm install | ||
- run: pnpm test | ||
- run: pnpm build | ||
- name: Build z | ||
run: cd packages/z && pnpm build | ||
- name: Test z | ||
run: cd packages/z && pnpm test | ||
- name: Test nestjs-zod | ||
run: cd packages/nestjs-zod && pnpm test | ||
- name: Build nestjs-zod | ||
run: cd packages/nestjs-zod && pnpm build | ||
- name: Build example app | ||
run: cd packages/example && pnpm run build | ||
- name: Test example app | ||
run: cd packages/example && pnpm run test:e2e | ||
|
||
- name: Extract version | ||
id: version | ||
uses: olegtarasov/[email protected] | ||
uses: olegtarasov/[email protected].3 | ||
with: | ||
tagRegex: 'v(.*)' | ||
|
||
- name: Set version from release | ||
uses: reedyuk/npm-version@1.0.1 | ||
uses: reedyuk/npm-version@1.1.1 | ||
with: | ||
version: ${{ steps.version.outputs.tag }} | ||
package: 'packages/nestjs-zod' | ||
|
||
- name: Create NPM config | ||
run: npm config set //registry.npmjs.org/:_authToken $NPM_TOKEN | ||
run: cd packages/nestjs-zod && npm config set //registry.npmjs.org/:_authToken $NPM_TOKEN | ||
env: | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
|
||
- name: Publish to NPM | ||
run: npm publish | ||
run: cd packages/nestjs-zod && npm publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Migration | ||
|
||
## From version 3.x to 4.x | ||
|
||
### `nestjs-zod/z` is now `@nest-zod/z` | ||
The extended zod api was moved out of the main package to a separate package. This requires a slight change to the import path: | ||
```diff | ||
- import { z } from 'nestjs-zod/z' | ||
+ import { z } from '@nest-zod/z' | ||
``` | ||
Additionally, `@nest-zod/z` is deprecated and will not be supported soon. This is because the way `@nest-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) | ||
|
||
> [!CAUTION] | ||
> It is highly recommended to move towards importing `zod` directly, instead of `@nest-zod/z` | ||
### `nestjs-zod/frontend` is removed | ||
The same exports are now available in `@nest-zod/z/frontend` (see details about `@nest-zod/z` above). This requires a slight change to the import path: | ||
```diff | ||
- import { isNestJsZodIssue } from 'nestjs-zod/frontend' | ||
+ import { isNestJsZodIssue } from '@nest-zod/z/frontend' | ||
``` | ||
`@nest-zod/z/frontend` is also deprecated and will not be supported soon, as explained above. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{ | ||
"name": "nestjs-zod-example", | ||
"version": "0.0.1", | ||
"description": "", | ||
"author": "", | ||
"description": "Example app showing how to use nestjs-zod", | ||
"author": "Ben Lorantfy <[email protected]>", | ||
"private": true, | ||
"license": "UNLICENSED", | ||
"scripts": { | ||
|
@@ -24,7 +24,8 @@ | |
"@nestjs/core": "^10.0.0", | ||
"@nestjs/platform-express": "^10.0.0", | ||
"@nestjs/swagger": "^7.4.2", | ||
"nestjs-zod": "0.0.0-set-by-ci", | ||
"@nest-zod/z": "workspace:*", | ||
"nestjs-zod": "workspace:*", | ||
"reflect-metadata": "^0.2.0", | ||
"rxjs": "^7.8.1", | ||
"zod": "^3.23.8" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,34 @@ | ||
import { Body, Controller, Post } from '@nestjs/common'; | ||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'; | ||
import { ApiOkResponse } from '@nestjs/swagger'; | ||
import { createZodDto } from 'nestjs-zod' | ||
import { z } from 'nestjs-zod/z' | ||
import { z } from 'zod' | ||
|
||
class PostDto extends createZodDto(z.object({ | ||
title: z.string().describe('The title of the post'), | ||
content: z.string().describe('The content of the post'), | ||
authorId: z.number().describe('The ID of the author of the post'), | ||
})) {} | ||
|
||
@Controller('posts') | ||
export class PostsController { | ||
@Post() | ||
createPost(@Body() body: PostDto) { | ||
console.log(body); | ||
return body; | ||
} | ||
|
||
@Get() | ||
@ApiOkResponse({ type: [PostDto], description: 'Get all posts' }) | ||
getAll() { | ||
return []; | ||
} | ||
|
||
@Get(':id') | ||
@ApiOkResponse({ type: PostDto, description: 'Get a post by ID' }) | ||
getById(@Param('id') id: string) { | ||
return { | ||
title: 'Hello', | ||
content: 'World', | ||
authorId: 1, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import * as request from 'supertest'; | ||
import { AppModule } from './../src/app.module'; | ||
|
||
describe('PostsController (e2e)', () => { | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
app = await createApp(); | ||
}); | ||
|
||
test('POST /posts - should validate input using Zod', async () => { | ||
const validPost = { | ||
title: 'Test Post', | ||
content: 'This is a test post content.', | ||
authorId: 1 | ||
}; | ||
|
||
const invalidPost = { | ||
title: 'Test Post', | ||
content: 'This is a test post content.', | ||
authorId: 'not a number' // Should be a number | ||
}; | ||
|
||
// Test with valid data | ||
await request(app.getHttpServer()) | ||
.post('/posts') | ||
.send(validPost) | ||
.expect(201) // Assuming 201 is returned on successful creation | ||
.expect((res) => { | ||
expect(res.body).toEqual({ | ||
title: validPost.title, | ||
content: validPost.content, | ||
authorId: validPost.authorId | ||
}) | ||
}); | ||
|
||
// Test with invalid data | ||
await request(app.getHttpServer()) | ||
.post('/posts') | ||
.send(invalidPost) | ||
.expect(400) // Bad request due to validation failure | ||
.expect((res) => { | ||
expect(res.body).toEqual({ | ||
statusCode: 400, | ||
message: 'Validation failed', | ||
errors: [ | ||
{ | ||
code: 'invalid_type', | ||
expected: 'number', | ||
received: 'string', | ||
path: ['authorId'], | ||
message: 'Expected number, received string' | ||
} | ||
] | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
async function createApp() { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
|
||
const app = moduleFixture.createNestApplication(); | ||
await app.init(); | ||
return app; | ||
} |
Oops, something went wrong.