1
0
Fork 0

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.
This commit is contained in:
Jason Fraley 2026-12-01 09:29:14 -05:00
parent 749bef0258
commit ca9961bf19

View file

@ -1,5 +1,8 @@
export type RibbonValidationResult = { ok: true } | { ok: false; error: string }; 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 { export function validateRibbonDesign(design: unknown): RibbonValidationResult {
if (!design || typeof design !== "object") { if (!design || typeof design !== "object") {
return { ok: false, error: "Invalid 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<string, unknown>;
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 // Check devices
const devices = designObj.devices; const devices = designObj.devices;
if (!Array.isArray(devices)) { if (!Array.isArray(devices)) {