From 0dc75b156270111fccf23bc8b2dcc5eb46863a25 Mon Sep 17 00:00:00 2001 From: Bram Suurd <78373894+BramSuurdje@users.noreply.github.com> Date: Wed, 13 Nov 2024 23:21:27 +0100 Subject: [PATCH] Refactor JSON schema handling: move schema definitions to separate file --- .../src/app/json-editor/_schemas/schemas.ts | 39 +++++++++++++++++++ frontend/src/app/json-editor/page.tsx | 39 +------------------ 2 files changed, 40 insertions(+), 38 deletions(-) create mode 100644 frontend/src/app/json-editor/_schemas/schemas.ts diff --git a/frontend/src/app/json-editor/_schemas/schemas.ts b/frontend/src/app/json-editor/_schemas/schemas.ts new file mode 100644 index 00000000..9118eab5 --- /dev/null +++ b/frontend/src/app/json-editor/_schemas/schemas.ts @@ -0,0 +1,39 @@ +import { z } from "zod"; + +const InstallMethodSchema = z.object({ + type: z.enum(["default", "alpine"]), + script: z.string().min(1), + resources: z.object({ + cpu: z.number().nullable(), + ram: z.number().nullable(), + hdd: z.number().nullable(), + os: z.string().nullable(), + version: z.number().nullable(), + }), +}); + +const NoteSchema = z.object({ + text: z.string().min(1), + type: z.string().min(1), +}); + +export const ScriptSchema = z.object({ + name: z.string().min(1), + slug: z.string().min(1), + categories: z.array(z.number()), + date_created: z.string().regex(/^\d{4}-\d{2}-\d{2}$/), + type: z.enum(["vm", "ct", "misc"]), + updateable: z.boolean(), + privileged: z.boolean(), + interface_port: z.number().nullable(), + documentation: z.string().nullable(), + website: z.string().url().nullable(), + logo: z.string().url().nullable(), + description: z.string().min(1), + install_methods: z.array(InstallMethodSchema), + default_credentials: z.object({ + username: z.string().nullable(), + password: z.string().nullable(), + }), + notes: z.array(NoteSchema), +}); diff --git a/frontend/src/app/json-editor/page.tsx b/frontend/src/app/json-editor/page.tsx index 75c370bc..c40c3e07 100644 --- a/frontend/src/app/json-editor/page.tsx +++ b/frontend/src/app/json-editor/page.tsx @@ -24,44 +24,7 @@ import { z } from "zod"; import { format } from "date-fns"; import { Label } from "@/components/ui/label"; import { AlertColors } from "@/config/siteConfig"; - -const InstallMethodSchema = z.object({ - type: z.enum(["default", "alpine"]), - script: z.string().min(1), - resources: z.object({ - cpu: z.number().nullable(), - ram: z.number().nullable(), - hdd: z.number().nullable(), - os: z.string().nullable(), - version: z.number().nullable(), - }), -}); - -const NoteSchema = z.object({ - text: z.string().min(1), - type: z.string().min(1), -}); - -const ScriptSchema = z.object({ - name: z.string().min(1), - slug: z.string().min(1), - categories: z.array(z.number()), - date_created: z.string().regex(/^\d{4}-\d{2}-\d{2}$/), - type: z.enum(["vm", "ct", "misc"]), - updateable: z.boolean(), - privileged: z.boolean(), - interface_port: z.number().nullable(), - documentation: z.string().nullable(), - website: z.string().url().nullable(), - logo: z.string().url().nullable(), - description: z.string().min(1), - install_methods: z.array(InstallMethodSchema), - default_credentials: z.object({ - username: z.string().nullable(), - password: z.string().nullable(), - }), - notes: z.array(NoteSchema), -}); +import { ScriptSchema } from "./_schemas/schemas"; type Script = z.infer;