From 766ff021679cef8fb80c774b3fbc655566ecf0ee Mon Sep 17 00:00:00 2001 From: Viren070 Date: Fri, 22 Aug 2025 17:31:37 +0100 Subject: [PATCH] fix: handle forced/default values for preset options in frontend and config validation --- packages/core/src/utils/config.ts | 16 +++++++++---- .../frontend/src/components/menu/addons.tsx | 2 +- .../src/components/shared/template-option.tsx | 24 ++++++++++++------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/packages/core/src/utils/config.ts b/packages/core/src/utils/config.ts index f6023294..2b5d2f7b 100644 --- a/packages/core/src/utils/config.ts +++ b/packages/core/src/utils/config.ts @@ -648,6 +648,13 @@ function validateOption( value: any, decryptValues: boolean = false ): any { + const forcedValue = + option.forced !== undefined && option.forced !== null + ? option.forced + : undefined; + if (forcedValue !== undefined) { + value = forcedValue; + } if (value === undefined) { if (option.required) { throw new Error(`Option ${option.id} is required, got ${value}`); @@ -710,7 +717,7 @@ function validateOption( if (option.type === 'string' || option.type === 'password') { if (typeof value !== 'string') { throw new Error( - `Option ${option.id} must be a string, got ${typeof value}` + `Option ${option.id} must be a string, got ${typeof value}: ${value}` ); } if (option.constraints?.min && value.length < option.constraints.min) { @@ -726,10 +733,6 @@ function validateOption( } if (option.type === 'password') { - if (option.forced) { - // option.forced is already encrypted - value = option.forced; - } if (isEncrypted(value) && decryptValues) { const { success, data, error } = decryptString(value); if (!success) { @@ -742,6 +745,9 @@ function validateOption( } if (option.type === 'url') { + if (forcedValue !== undefined) { + value = forcedValue; + } if (typeof value !== 'string') { throw new Error( `Option ${option.id} must be a string, got ${typeof value}` diff --git a/packages/frontend/src/components/menu/addons.tsx b/packages/frontend/src/components/menu/addons.tsx index 41d43f86..c96c1076 100644 --- a/packages/frontend/src/components/menu/addons.tsx +++ b/packages/frontend/src/components/menu/addons.tsx @@ -160,7 +160,7 @@ function Content() { options: Object.fromEntries( (preset.OPTIONS || []).map((opt: any) => [ opt.id, - opt.default ?? undefined, + opt.forced ?? opt.default ?? undefined, ]) ), }); diff --git a/packages/frontend/src/components/shared/template-option.tsx b/packages/frontend/src/components/shared/template-option.tsx index 7283109f..43bd30bb 100644 --- a/packages/frontend/src/components/shared/template-option.tsx +++ b/packages/frontend/src/components/shared/template-option.tsx @@ -50,7 +50,9 @@ const TemplateOption: React.FC = ({ emptyIsUndefined = false, } = option; - const isDisabled = disabled || !!forced; + const isDisabled = disabled || !(forced === undefined || forced === null); + const forcedValue = + forced !== undefined && forced !== null ? forced : undefined; switch (type) { case 'socials': @@ -74,7 +76,7 @@ const TemplateOption: React.FC = ({
onChange(emptyIsUndefined ? value || undefined : value) } @@ -99,7 +101,7 @@ const TemplateOption: React.FC = ({
onChange(emptyIsUndefined ? value || undefined : value) } @@ -123,7 +125,7 @@ const TemplateOption: React.FC = ({ return (
onChange(value) @@ -156,7 +158,11 @@ const TemplateOption: React.FC = ({
{name} - +
{description && (
@@ -170,7 +176,7 @@ const TemplateOption: React.FC = ({