From 2babc57982dd1b48604e8866b3f1b59679203929 Mon Sep 17 00:00:00 2001 From: rajun Date: Tue, 27 Oct 2026 16:28:49 +0530 Subject: [PATCH] feat: add asset form schema --- app/schemas/assets/asset-form.ts | 59 ++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 app/schemas/assets/asset-form.ts diff --git a/app/schemas/assets/asset-form.ts b/app/schemas/assets/asset-form.ts new file mode 100644 index 0000000..3af255b --- /dev/null +++ b/app/schemas/assets/asset-form.ts @@ -0,0 +1,59 @@ +import { z } from 'zod'; +import { MODEL_SOURCE_MODES } from '~/data/assets/constants'; +import { RECORD_ID_PATTERN } from '~/lib/assets/qr'; +import type { ArAsset } from '~/types/asset'; + +const isAbsoluteUrl = (value: string) => URL.canParse(value); + +const modelSourceSchema = z.object({ + mode: z.enum(MODEL_SOURCE_MODES), + url: z.string().trim(), + file: z.instanceof(File).nullable(), + existing: z.string(), +}); + +export const assetFormSchema = z + .object({ + id: z + .string() + .trim() + .refine((v) => v === '' || RECORD_ID_PATTERN.test(v), 'Use exactly 15 lowercase letters or digits'), + name: z.string().trim().min(1, 'Enter a name'), + project: z.string(), + brandedQr: z.boolean(), + glb: modelSourceSchema, + usdz: modelSourceSchema, + }) + .superRefine(({ glb, usdz }, ctx) => { + if (glb.mode === 'url' && !isAbsoluteUrl(glb.url)) { + ctx.addIssue({ code: 'custom', message: 'Enter a valid model URL', path: ['glb', 'url'] }); + } + if (glb.mode === 'file' && !glb.file && !glb.existing) { + ctx.addIssue({ code: 'custom', message: 'Choose a GLB file', path: ['glb', 'file'] }); + } + if (usdz.mode === 'url' && usdz.url && !isAbsoluteUrl(usdz.url)) { + ctx.addIssue({ code: 'custom', message: 'Enter a valid model URL', path: ['usdz', 'url'] }); + } + }); + +export type AssetFormValues = z.infer; + +function toModelSource(file: string, url: string): AssetFormValues['glb'] { + return { mode: url && !file ? 'url' : 'file', url: file ? '' : url, file: null, existing: file }; +} + +interface NewAssetDefaults { + projectId?: string; + brandedQr: boolean; +} + +export function toAssetFormValues(asset: ArAsset | null, defaults: NewAssetDefaults): AssetFormValues { + return { + id: asset?.id ?? '', + name: asset?.name ?? '', + project: asset ? asset.project : (defaults.projectId ?? ''), + brandedQr: asset ? asset.branded_qr : defaults.brandedQr, + glb: toModelSource(asset?.glb_file ?? '', asset?.glb_url ?? ''), + usdz: toModelSource(asset?.usdz_file ?? '', asset?.usdz_url ?? ''), + }; +}