CAKE20

AI Web PaaS

입력 검증

API config에 입력 조건과 에디터 테스트 데이터를 함께 선언합니다.

config 내보내기

  • config는 export const로 내보냅니다.
  • config.filter에는 각 입력값을 검사할 Zod shape를 작성합니다.
  • config.sample은 filter를 통과하는 값으로 작성하며 에디터의 테스트 실행에만 사용합니다.
  • GET, DELETE, HEAD는 query string을 검사하므로 숫자는 z.coerce.number()로 변환합니다.
  • POST, PUT, PATCH는 JSON body를 검사하고 변환된 값을 handler에 전달합니다.
  • [id], [...path]와 같은 동적 경로값은 filter가 아닌 context.params로 받습니다.
export const config = {
  filter: {
    username: z.string().min(3),
    age: z.number().positive(),
    email: z.string().email()
  },
  sample: {
    username: "candy",
    age: 17,
    email: "[email protected]"
  }
};

export default async (data: Input<typeof config.filter>) => ({
  ...data,
  count: await db.user.count()
});

config.filter가 있으면 유효한 config.sample이 있어야 에디터 테스트를 실행할 수 있습니다.