Slide 37
Slide 37 text
37
ようするに
1. Zodのバリデーションコードを書く
3. 下処理したFastifyに食わせる
2. JSON SchemaとTypeScript向けの型を作る
import { z } from "zod";
const Blog = z.object({
blog_id: z.number(),
title: z.string().max(50),
description: z.string().max(200),
body: z.string().describe("ブログの中身(Markdown)"),
thumnail_url: z.string().url().optional().describe("サムネイル(optional)"),
author: z.string().describe("投稿者"),
created_at: z.date().describe("作成日"),
});
const getBlogsOutput = z.array(Blog)
// TypeScriptの型
export type GetBlogsOutput = z.infer;
// JSON Schema
export const GetBlogsOutput = zodToJsonSchema(getBlogsOutput, {
name: ”getBlogOutput",
}).definitions.getBlogOutput;
server.get("/blogs", {
schema: {
response: {
200: {
...GetBlogsOutput,
description: "取得成功”
},
},
tags: ["blog"],
},
handler: getBlogsHandler,
});