35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { z } from 'zod';
|
|
import { AR_PAGE_DEFAULTS, BUTTON_CUSTOM_LIMITS } from '~/data/projects/constants';
|
|
import { hexColor } from '~/schemas/color';
|
|
import type { ArPageSettings, ButtonCustomSize } from '~/types/project';
|
|
|
|
function px(key: keyof typeof BUTTON_CUSTOM_LIMITS) {
|
|
const { label, min, max } = BUTTON_CUSTOM_LIMITS[key];
|
|
const message = `${label} must be ${min} to ${max} px`;
|
|
return z.number({ error: message }).int(message).min(min, message).max(max, message);
|
|
}
|
|
|
|
export const buttonCustomSchema = z.object({
|
|
height: px('height'),
|
|
width: px('width').nullable(),
|
|
paddingX: px('paddingX'),
|
|
radius: px('radius'),
|
|
borderWidth: px('borderWidth'),
|
|
borderColor: hexColor,
|
|
}) satisfies z.ZodType<ButtonCustomSize>;
|
|
|
|
export const arPageSchema = z.object({
|
|
bars: z.boolean(),
|
|
modelName: z.boolean(),
|
|
logoBacking: z.boolean(),
|
|
thobLogo: z.boolean(),
|
|
buttonSize: z.enum(['sm', 'md', 'lg', 'full', 'custom']),
|
|
buttonCustom: buttonCustomSchema,
|
|
}) satisfies z.ZodType<ArPageSettings>;
|
|
|
|
// Saved options over the defaults; anything missing or invalid keeps its default
|
|
export function readArPage(value: unknown): ArPageSettings {
|
|
const result = arPageSchema.partial().safeParse(value);
|
|
return { ...AR_PAGE_DEFAULTS, ...(result.success ? result.data : {}) };
|
|
}
|