From 5140fd7108ee4f53c6915bd2f083ed06e83fca17 Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Fri, 28 Jul 2023 18:11:31 +0500 Subject: [PATCH] theme: fix theme validation --- packages/theme/src/theme-engine/index.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/theme/src/theme-engine/index.ts b/packages/theme/src/theme-engine/index.ts index a4a4a0bf4..e9cecd8ff 100644 --- a/packages/theme/src/theme-engine/index.ts +++ b/packages/theme/src/theme-engine/index.ts @@ -280,7 +280,6 @@ const HEX_COLOR_REGEX_ALPHA = export function validateTheme(json: ThemeDefinition) { const flattenedTheme = flatten(json); - const missingKeys = []; for (const key of RequiredKeys) { if (!Object.keys(flattenedTheme).includes(key)) { @@ -294,31 +293,27 @@ export function validateTheme(json: ThemeDefinition) { )} are missing from the theme.` }; } - const invalidColors = []; - for (const key in flattenedTheme) { if (!key.startsWith("scopes")) continue; + const keyPart = key.split(".").pop() as string; const value = flattenedTheme[key]; + const isHexAlphaColor = /hover|shade|backdrop|textSelection/g.test(keyPart); + HEX_COLOR_REGEX.lastIndex = 0; + HEX_COLOR_REGEX_ALPHA.lastIndex = 0; if ( - !/hover|shade|backdrop|textSelection/g.test(key) && - !HEX_COLOR_REGEX.test(value) + (!isHexAlphaColor && !HEX_COLOR_REGEX.test(value)) || + (isHexAlphaColor && !HEX_COLOR_REGEX_ALPHA.test(value)) ) { - invalidColors.push(key); - } else if ( - /hover|shade|backdrop|textSelection/g.test(key) && - !HEX_COLOR_REGEX_ALPHA.test(value) - ) { - invalidColors.push(key); + if (!HEX_COLOR_REGEX.test(value)) invalidColors.push(key); } } - if (invalidColors.length > 0) { return { - error: `Failed to apply theme, ${missingKeys.join( + error: `Failed to apply theme, ${invalidColors.join( "," - )} are missing from the theme.` + )} have invalid values.` }; }