diff --git a/src/server/plugins/engine/components/ComponentBase.ts b/src/server/plugins/engine/components/ComponentBase.ts index bfeffac0a..901963df6 100644 --- a/src/server/plugins/engine/components/ComponentBase.ts +++ b/src/server/plugins/engine/components/ComponentBase.ts @@ -28,6 +28,7 @@ export class ComponentBase { isFormComponent = false model: FormModel + def: ComponentDef /** joi schemas based on a component defined in the form JSON. This validates a user's answer and is generated from {@link ComponentDef} */ formSchema: ComponentSchema = joi.string() @@ -45,6 +46,7 @@ export class ComponentBase { this.type = def.type this.name = def.name this.title = def.title + this.def = def if ('schema' in def) { this.schema = def.schema diff --git a/src/server/plugins/engine/components/ComponentCollection.ts b/src/server/plugins/engine/components/ComponentCollection.ts index cc4286b1f..f0ae4cfc9 100644 --- a/src/server/plugins/engine/components/ComponentCollection.ts +++ b/src/server/plugins/engine/components/ComponentCollection.ts @@ -308,12 +308,11 @@ export class ComponentCollection { labelOverrides[subField.name] = patchedSchema } } else { + const fieldDef = field.def const translatedLabel = - translator.tComponent( - field as unknown as ComponentDef, - 'shortDescription' - ) || - translator.tComponent(field as unknown as ComponentDef, 'title') + translator.tComponent(fieldDef, 'errorDescription') || + translator.tComponent(fieldDef, 'shortDescription') || + translator.tComponent(fieldDef, 'title') const messagesOverride = field.getValidationMessagesOverride(translator) let patchedSchema = field.formSchema diff --git a/src/server/plugins/engine/components/DeclarationField.ts b/src/server/plugins/engine/components/DeclarationField.ts index 2836e1371..f413b756c 100644 --- a/src/server/plugins/engine/components/DeclarationField.ts +++ b/src/server/plugins/engine/components/DeclarationField.ts @@ -2,7 +2,6 @@ import { ComponentType, hasFormComponents, isFormType, - type ComponentDef, type DeclarationFieldComponent, type Item } from '@defra/forms-model' @@ -148,7 +147,7 @@ export class DeclarationField extends FormComponent { 'components.declarationField.defaultLabel' ) } = this - const content = tComponent(this as unknown as ComponentDef, 'content') + const content = tComponent(this.def, 'content') const viewModel = super.getViewModel(context) let { fieldset, label } = viewModel diff --git a/src/server/plugins/engine/components/FormComponent.ts b/src/server/plugins/engine/components/FormComponent.ts index 8806fcf32..528e2977b 100644 --- a/src/server/plugins/engine/components/FormComponent.ts +++ b/src/server/plugins/engine/components/FormComponent.ts @@ -1,5 +1,4 @@ import { - type ComponentDef, type FormComponentsDef, type FormMetadata, type Item @@ -178,15 +177,14 @@ export class FormComponent extends ComponentBase { const isRequired = !('required' in options) || options.required !== false const hideOptional = 'optionalText' in options && options.optionalText - const resolvedTitle = - tComponent(this as unknown as ComponentDef, 'title') || title + const resolvedTitle = tComponent(this.def, 'title') || title const optionalTag = !isRequired && !hideOptional ? ` ${t('common.optional')}` : '' const label = `${resolvedTitle}${optionalTag}` if (hint) { viewModel.hint = { - text: tComponent(this as unknown as ComponentDef, 'hint') || hint + text: tComponent(this.def, 'hint') || hint } } diff --git a/src/server/plugins/engine/components/Markdown.ts b/src/server/plugins/engine/components/Markdown.ts index 7d7abe3be..a8aaa2bf1 100644 --- a/src/server/plugins/engine/components/Markdown.ts +++ b/src/server/plugins/engine/components/Markdown.ts @@ -1,4 +1,4 @@ -import { type ComponentDef, type MarkdownComponent } from '@defra/forms-model' +import { type MarkdownComponent } from '@defra/forms-model' import { ComponentBase } from '~/src/server/plugins/engine/components/ComponentBase.js' import { type RenderContext } from '~/src/server/plugins/engine/components/types.js' @@ -28,8 +28,7 @@ export class Markdown extends ComponentBase { return { ...viewModel, - content: - tComponent(this as unknown as ComponentDef, 'content') || content, + content: tComponent(this.def, 'content') || content, headerStartLevel: this.headerStartLevel } } diff --git a/src/server/plugins/engine/components/TextField.test.ts b/src/server/plugins/engine/components/TextField.test.ts index 1223fdbbe..c6eae1ba8 100644 --- a/src/server/plugins/engine/components/TextField.test.ts +++ b/src/server/plugins/engine/components/TextField.test.ts @@ -272,7 +272,7 @@ describe('TextField', () => { language: 'en-GB' } }) - expect(tComponent).toHaveBeenCalledWith(field, 'title') + expect(tComponent).toHaveBeenCalledWith(field.def, 'title') expect(viewModel.label.text).toBe('Translated title') }) @@ -295,7 +295,7 @@ describe('TextField', () => { language: 'en-GB' } }) - expect(tComponent).toHaveBeenCalledWith(hintField, 'hint') + expect(tComponent).toHaveBeenCalledWith(hintField.def, 'hint') expect(viewModel.hint?.text).toBe('Translated hint') }) diff --git a/src/server/plugins/engine/i18n/types.ts b/src/server/plugins/engine/i18n/types.ts index a10db632f..f9aaf3cfb 100644 --- a/src/server/plugins/engine/i18n/types.ts +++ b/src/server/plugins/engine/i18n/types.ts @@ -22,6 +22,7 @@ export type FormDefinitionTranslations = Record< hint: string content: string shortDescription: string + errorDescription: string paymentDescription: string }> >