Skip to content

Commit 4add3ec

Browse files
committed
feat: handle zod schema for Body and Query. Need to do ReturnedSchema
1 parent f9bb921 commit 4add3ec

File tree

5 files changed

+286
-120
lines changed

5 files changed

+286
-120
lines changed

deno.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
"path_to_regexp": "npm:[email protected]",
4747
"@std/path": "jsr:@std/[email protected]",
4848
"@std/assert": "jsr:@std/[email protected]",
49-
"@danet/core": "jsr:@danet/core@2"
49+
"@danet/core": "jsr:@danet/core@2",
50+
"@danet/zod": "../danet-zod/decorators.ts",
51+
"zod": "npm:[email protected]",
52+
"zod-openapi": "npm:@anatine/zod-openapi"
5053
}
5154
}

deno.lock

Lines changed: 130 additions & 110 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/app.ts

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ import {
2121
} from '@danet/core';
2222
import { Module } from '@danet/core';
2323
import { DanetApplication } from '@danet/core';
24+
import { Query as ZodQuery, Body as ZodBody } from '@danet/zod';
25+
import { z } from 'zod';
26+
import { extendZodWithOpenApi } from 'zod-openapi';
27+
28+
extendZodWithOpenApi(z);
29+
30+
const ZodCat = z.object({
31+
name: z.string(),
32+
breed: z.string(),
33+
dob: z.date(),
34+
isHungry: z.boolean().optional(),
35+
hobbies: z.array(z.any())
36+
}).openapi({
37+
title: 'ZodCat'
38+
})
39+
40+
type ZodCat = z.infer<typeof ZodCat>;
2441

2542
class Cat {
2643
@ApiProperty()
@@ -65,6 +82,17 @@ class CatSearch {
6582
}
6683
}
6784

85+
86+
const ZodTodo = z.object({
87+
title: z.string(),
88+
description: z.string(),
89+
version: z.number(),
90+
cat: ZodCat,
91+
}).openapi({
92+
title: 'ZodTodo'
93+
})
94+
95+
type ZodTodo = z.infer<typeof ZodTodo>;
6896
class Todo {
6997
@ApiProperty({
7098
description: 'my description'
@@ -84,20 +112,32 @@ class Todo {
84112
}
85113
}
86114

87-
export class NameSearch {
88-
@ApiProperty()
89-
name!: string;
90-
}
115+
const NameSearch = z.object({
116+
name: z.string(),
117+
}).openapi(
118+
{
119+
title: 'NameSearch'
120+
}
121+
);
122+
123+
type NameSearch = z.infer<typeof NameSearch>;
91124

92125
@Controller('hello')
93126
class HelloController {
94127
@Get()
95-
@QueryType(NameSearch)
96-
hello(@Query() search: NameSearch) {
128+
hello(@ZodQuery(NameSearch) search: NameSearch) {
97129
return `Hello ${search.name}`;
98130
}
99131
}
100132

133+
@Controller('zod')
134+
class ZodController {
135+
@Post()
136+
posZodSomething(@ZodBody(ZodTodo) todo: ZodTodo): number {
137+
return 1;
138+
}
139+
}
140+
101141
@Controller('my-endpoint')
102142
class MyController {
103143

@@ -149,7 +189,7 @@ class SecondController {
149189
}
150190

151191
@Module({
152-
controllers: [SecondController, HelloController],
192+
controllers: [SecondController, HelloController, ZodController],
153193
})
154194
class SecondModule {
155195
}

0 commit comments

Comments
 (0)