Skip to content

Commit

Permalink
feat: handle zod schema for Body and Query. Need to do ReturnedSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Oct 17, 2024
1 parent f9bb921 commit 4add3ec
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 120 deletions.
5 changes: 4 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"path_to_regexp": "npm:[email protected]",
"@std/path": "jsr:@std/[email protected]",
"@std/assert": "jsr:@std/[email protected]",
"@danet/core": "jsr:@danet/core@2"
"@danet/core": "jsr:@danet/core@2",
"@danet/zod": "../danet-zod/decorators.ts",
"zod": "npm:[email protected]",
"zod-openapi": "npm:@anatine/zod-openapi"
}
}
240 changes: 130 additions & 110 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 47 additions & 7 deletions example/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ import {
} from '@danet/core';
import { Module } from '@danet/core';
import { DanetApplication } from '@danet/core';
import { Query as ZodQuery, Body as ZodBody } from '@danet/zod';
import { z } from 'zod';
import { extendZodWithOpenApi } from 'zod-openapi';

extendZodWithOpenApi(z);

const ZodCat = z.object({
name: z.string(),
breed: z.string(),
dob: z.date(),
isHungry: z.boolean().optional(),
hobbies: z.array(z.any())
}).openapi({
title: 'ZodCat'
})

type ZodCat = z.infer<typeof ZodCat>;

class Cat {
@ApiProperty()
Expand Down Expand Up @@ -65,6 +82,17 @@ class CatSearch {
}
}


const ZodTodo = z.object({
title: z.string(),
description: z.string(),
version: z.number(),
cat: ZodCat,
}).openapi({
title: 'ZodTodo'
})

type ZodTodo = z.infer<typeof ZodTodo>;
class Todo {
@ApiProperty({
description: 'my description'
Expand All @@ -84,20 +112,32 @@ class Todo {
}
}

export class NameSearch {
@ApiProperty()
name!: string;
}
const NameSearch = z.object({
name: z.string(),
}).openapi(
{
title: 'NameSearch'
}
);

type NameSearch = z.infer<typeof NameSearch>;

@Controller('hello')
class HelloController {
@Get()
@QueryType(NameSearch)
hello(@Query() search: NameSearch) {
hello(@ZodQuery(NameSearch) search: NameSearch) {
return `Hello ${search.name}`;
}
}

@Controller('zod')
class ZodController {
@Post()
posZodSomething(@ZodBody(ZodTodo) todo: ZodTodo): number {
return 1;
}
}

@Controller('my-endpoint')
class MyController {

Expand Down Expand Up @@ -149,7 +189,7 @@ class SecondController {
}

@Module({
controllers: [SecondController, HelloController],
controllers: [SecondController, HelloController, ZodController],
})
class SecondModule {
}
Expand Down
Loading

0 comments on commit 4add3ec

Please sign in to comment.