From efbf04271ddd191cc1f2987ee9520edb8fc7f6aa Mon Sep 17 00:00:00 2001 From: tetuaoro <65575727+tetuaoro@users.noreply.github.com> Date: Wed, 9 Oct 2024 16:07:51 +0200 Subject: [PATCH] improve: with zod validation --- src/server/api/admin/migration.post.ts | 44 ++++++++++++++------------ 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/server/api/admin/migration.post.ts b/src/server/api/admin/migration.post.ts index 5aa49c9e..8e37c460 100644 --- a/src/server/api/admin/migration.post.ts +++ b/src/server/api/admin/migration.post.ts @@ -1,31 +1,33 @@ +import { z } from 'zod'; + // TODO: check what are missing -type Client = { - id: string; - name: string; - address: string; - privateKey: string; - publicKey: string; - preSharedKey: string; - createdAt: string; - updatedAt: string; - enabled: boolean; -}; +const clientSchema = z.object({ + id: z.string(), + name: z.string(), + address: z.string(), + privateKey: z.string(), + publicKey: z.string(), + preSharedKey: z.string(), + createdAt: z.string(), + updatedAt: z.string(), + enabled: z.boolean(), +}); -type OldConfig = { - server: { - privateKey: string; - publicKey: string; - address: string; - }; - clients: Record; -}; +const oldConfigSchema = z.object({ + server: z.object({ + privateKey: z.string(), + publicKey: z.string(), + address: z.string(), + }), + clients: z.record(z.string(), clientSchema), +}); export default defineEventHandler(async (event) => { const { file } = await readValidatedBody(event, validateZod(fileType, event)); - const file_ = JSON.parse(file) as OldConfig; + const file_ = await oldConfigSchema.parseAsync(JSON.parse(file)); // TODO: handle migration - console.log('file_', file_); + console.log('zod file_', file_); return { success: true }; });