From ca9961bf1963a7f76357dbcce6697f38944923d6 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Tue, 1 Dec 2026 09:29:14 -0500 Subject: [PATCH] feat(ribbon): validate stripe direction and overlay line bounds Optional keys only: legacy designs stay valid untouched. Orientation must be vertical|horizontal; at most five lines with hex colors, positions in 0..100, and positive thickness. Covers the Studio submit action and the admin collection path through the shared validator. --- src/lib/awards/ribbonValidation.ts | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/lib/awards/ribbonValidation.ts b/src/lib/awards/ribbonValidation.ts index 1279fde5..8c3f1e09 100644 --- a/src/lib/awards/ribbonValidation.ts +++ b/src/lib/awards/ribbonValidation.ts @@ -1,5 +1,8 @@ export type RibbonValidationResult = { ok: true } | { ok: false; error: string }; +/** Overlay accent lines a design may carry. Mirrors the editors' add-button cap. */ +const MAX_LINES = 5; + export function validateRibbonDesign(design: unknown): RibbonValidationResult { if (!design || typeof design !== "object") { return { ok: false, error: "Invalid design object" }; @@ -33,6 +36,51 @@ export function validateRibbonDesign(design: unknown): RibbonValidationResult { } } + // Stripe orientation is optional; absent means the legacy vertical layout. + if ( + designObj.stripeOrientation !== undefined && + designObj.stripeOrientation !== "vertical" && + designObj.stripeOrientation !== "horizontal" + ) { + return { ok: false, error: "Stripe orientation must be 'vertical' or 'horizontal'" }; + } + + // Accent lines are optional; legacy designs without the key stay valid. + const lines = designObj.lines; + if (lines !== undefined) { + if (!Array.isArray(lines)) { + return { ok: false, error: "Lines must be an array" }; + } + + if (lines.length > MAX_LINES) { + return { ok: false, error: `Must have at most ${MAX_LINES} lines` }; + } + + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + if (!line || typeof line !== "object") { + return { ok: false, error: `Line ${i + 1} is invalid` }; + } + + const lineObj = line as Record; + if (typeof lineObj.id !== "string") { + return { ok: false, error: `Line ${i + 1} must have a string id` }; + } + + if (typeof lineObj.color !== "string" || !/^#[0-9a-fA-F]{6}$/.test(lineObj.color)) { + return { ok: false, error: `Line ${i + 1} color must be a valid hex color (#RRGGBB)` }; + } + + if (typeof lineObj.positionY !== "number" || !Number.isFinite(lineObj.positionY) || lineObj.positionY < 0 || lineObj.positionY > 100) { + return { ok: false, error: `Line ${i + 1} position must be a finite number between 0 and 100` }; + } + + if (typeof lineObj.thickness !== "number" || !Number.isFinite(lineObj.thickness) || lineObj.thickness <= 0 || lineObj.thickness > 100) { + return { ok: false, error: `Line ${i + 1} thickness must be a finite number between 0 and 100` }; + } + } + } + // Check devices const devices = designObj.devices; if (!Array.isArray(devices)) {