From 5d00f3cc6a75a22732ec7ffa63d9335d9d75853c Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 8 Aug 2023 20:41:18 +0200 Subject: [PATCH] Fix: fix #1532 --- src/Models/ThemeConfig/TagRenderingConfig.ts | 82 ++++++++++++-------- 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/src/Models/ThemeConfig/TagRenderingConfig.ts b/src/Models/ThemeConfig/TagRenderingConfig.ts index dbc0ba2f3f..6cd5dc4dc8 100644 --- a/src/Models/ThemeConfig/TagRenderingConfig.ts +++ b/src/Models/ThemeConfig/TagRenderingConfig.ts @@ -58,7 +58,7 @@ export default class TagRenderingConfig { public readonly freeform?: { readonly key: string - readonly type: string + readonly type: ValidatorType readonly placeholder: Translation readonly addExtraTags: UploadableTag[] readonly inline: boolean @@ -133,7 +133,17 @@ export default class TagRenderingConfig { ) { throw `Freeform.addExtraTags should be a list of strings - not a single string (at ${context})` } - const type = json.freeform.type ?? "string" + if ( + json.freeform.type && + Validators.availableTypes.indexOf(json.freeform.type) < 0 + ) { + throw `At ${context}: invalid type, perhaps you meant ${Utils.sortedByLevenshteinDistance( + json.freeform.key, + Validators.availableTypes, + (s) => s + )}` + } + const type: ValidatorType = json.freeform.type ?? "string" let placeholder: Translation = Translations.T(json.freeform.placeholder) if (placeholder === undefined) { @@ -622,7 +632,7 @@ export default class TagRenderingConfig { * * @param singleSelectedMapping (Only used if multiAnswer == false): the single mapping to apply. Use (mappings.length) for the freeform * @param multiSelectedMapping (Only used if multiAnswer == true): all the mappings that must be applied. Set multiSelectedMapping[mappings.length] to use the freeform as well - * @param currentProperties: The current properties of the object for which the question should be answered + * @param currentProperties The current properties of the object for which the question should be answered */ public constructChangeSpecification( freeformValue: string | undefined, @@ -685,38 +695,42 @@ export default class TagRenderingConfig { return undefined } return and - } else { - // Is at least one mapping shown in the answer? - const someMappingIsShown = this.mappings.some((m) => { - if (typeof m.hideInAnswer === "boolean") { - return !m.hideInAnswer - } - const isHidden = m.hideInAnswer.matchesProperties(currentProperties) - return !isHidden - }) - // If all mappings are hidden for the current tags, we can safely assume that we should use the freeform key - const useFreeform = - freeformValue !== undefined && - (singleSelectedMapping === this.mappings.length || !someMappingIsShown) - if (useFreeform) { - return new And([ - new Tag(this.freeform.key, freeformValue), - ...(this.freeform.addExtraTags ?? []), - ]) - } else if (singleSelectedMapping !== undefined) { - return new And([ - this.mappings[singleSelectedMapping].if, - ...(this.mappings[singleSelectedMapping].addExtraTags ?? []), - ]) - } else { - console.warn("TagRenderingConfig.ConstructSpecification has a weird fallback for", { - freeformValue, - singleSelectedMapping, - multiSelectedMapping, - currentProperties, - }) - return undefined + } + + // Is at least one mapping shown in the answer? + const someMappingIsShown = this.mappings.some((m) => { + if (typeof m.hideInAnswer === "boolean") { + return !m.hideInAnswer } + const isHidden = m.hideInAnswer.matchesProperties(currentProperties) + return !isHidden + }) + // If all mappings are hidden for the current tags, we can safely assume that we should use the freeform key + const useFreeform = + freeformValue !== undefined && + (singleSelectedMapping === this.mappings.length || + !someMappingIsShown || + singleSelectedMapping === undefined) + if (useFreeform) { + return new And([ + new Tag(this.freeform.key, freeformValue), + ...(this.freeform.addExtraTags ?? []), + ]) + } else if (singleSelectedMapping !== undefined) { + return new And([ + this.mappings[singleSelectedMapping].if, + ...(this.mappings[singleSelectedMapping].addExtraTags ?? []), + ]) + } else { + console.error("TagRenderingConfig.ConstructSpecification has a weird fallback for", { + freeformValue, + singleSelectedMapping, + multiSelectedMapping, + currentProperties, + useFreeform, + }) + + return undefined } }