From 9e63261b350e5b7b61c7ffb17c9e75a6c5a8c711 Mon Sep 17 00:00:00 2001 From: Alex Christoffer Rasmussen Date: Wed, 12 Jun 2024 21:31:08 +0200 Subject: [PATCH] None colour from API (#1063) --- apps/server/src/utils/__tests__/coerceType.test.ts | 7 +++++++ apps/server/src/utils/coerceType.ts | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/apps/server/src/utils/__tests__/coerceType.test.ts b/apps/server/src/utils/__tests__/coerceType.test.ts index 8b37a0d257..b56ddfdad3 100644 --- a/apps/server/src/utils/__tests__/coerceType.test.ts +++ b/apps/server/src/utils/__tests__/coerceType.test.ts @@ -18,6 +18,13 @@ describe('parses a colour string that is', () => { it('not a string', () => { expect(() => coerceColour(5)).toThrowError(Error('Invalid colour value received')); }); + it('undefinde and null are not valid', () => { + expect(() => coerceColour(null)).toThrowError(Error('Invalid colour value received')); + expect(() => coerceColour(undefined)).toThrowError(Error('Invalid colour value received')); + }); + it('empty string is allowed', () => { + expect(coerceColour('')).toBe(''); + }); }); describe('match a string to an enum that is', () => { diff --git a/apps/server/src/utils/coerceType.ts b/apps/server/src/utils/coerceType.ts index 6a57300617..0f64c910fc 100644 --- a/apps/server/src/utils/coerceType.ts +++ b/apps/server/src/utils/coerceType.ts @@ -90,7 +90,12 @@ export function coerceColour(value: unknown): string { if (!isColourHex(lowerCaseValue)) { throw new Error('Invalid hex colour received'); } - } else if (!(lowerCaseValue in cssColours)) { + return lowerCaseValue; + } + if (lowerCaseValue === '') { + return lowerCaseValue; // None colour the same as the UI 'Ø' button + } + if (!(lowerCaseValue in cssColours)) { throw new Error('Invalid colour name received'); } return lowerCaseValue;