From 3582570596d31a94787fa58ff775927d5672f030 Mon Sep 17 00:00:00 2001 From: Alex Luckett Date: Thu, 19 Nov 2026 16:13:43 +0000 Subject: [PATCH 1/4] Require a required message in the location field config Both location fields set their own required message, so the fallback to the generic required message never ran outside the test subclass. The config now requires the message, and the fallback is removed. --- .../engine/components/LocationFieldBase.test.ts | 1 + .../engine/components/LocationFieldBase.ts | 15 +++++---------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/server/plugins/engine/components/LocationFieldBase.test.ts b/src/server/plugins/engine/components/LocationFieldBase.test.ts index f5893b76e..555a98715 100644 --- a/src/server/plugins/engine/components/LocationFieldBase.test.ts +++ b/src/server/plugins/engine/components/LocationFieldBase.test.ts @@ -14,6 +14,7 @@ class TestLocationField extends LocationFieldBase { return { pattern: /^TEST\d{4}$/i, patternErrorMessage: 'Enter a valid test code like TEST1234', + requiredMessage: 'Enter a test code', additionalMessages: { 'string.custom': 'This is a custom error from additional messages' } as LanguageMessages, diff --git a/src/server/plugins/engine/components/LocationFieldBase.ts b/src/server/plugins/engine/components/LocationFieldBase.ts index 838b85e32..0199da65d 100644 --- a/src/server/plugins/engine/components/LocationFieldBase.ts +++ b/src/server/plugins/engine/components/LocationFieldBase.ts @@ -14,8 +14,6 @@ import { withCustomValidationOverrides } from '~/src/server/plugins/engine/components/helpers/index.js' import { type RenderContext } from '~/src/server/plugins/engine/components/types.js' -import { buildValidationMessages } from '~/src/server/plugins/engine/i18n/buildValidationMessages.js' -import { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js' import { type Translator } from '~/src/server/plugins/engine/types/index.js' import { type ErrorMessageTemplateList, @@ -35,7 +33,7 @@ interface LocationFieldOptions { interface ValidationConfig { pattern: RegExp patternErrorMessage: JoiExpression - requiredMessage?: JoiExpression + requiredMessage: JoiExpression } /** @@ -68,7 +66,7 @@ export abstract class LocationFieldBase extends FormComponent { addClassOptionIfNone(locationOptions, 'govuk-input--width-10') const config = this.getValidationConfig(undefined) - const requiredMessage = config.requiredMessage ?? messageTemplate.required + const { requiredMessage } = config const messages = { 'any.required': requiredMessage, @@ -147,13 +145,10 @@ export abstract class LocationFieldBase extends FormComponent { getValidationMessagesOverride(translator: Translator) { const { customValidationMessage, customValidationMessages } = this.options - const { t } = translator const config = this.getValidationConfig(translator) const defaults = { - 'any.required': - config.requiredMessage ?? buildValidationMessages(t).required, - 'string.empty': - config.requiredMessage ?? buildValidationMessages(t).required, + 'any.required': config.requiredMessage, + 'string.empty': config.requiredMessage, 'string.pattern.base': config.patternErrorMessage } @@ -173,7 +168,7 @@ export abstract class LocationFieldBase extends FormComponent { baseErrors: [ { type: 'required', - template: config.requiredMessage ?? messageTemplate.required + template: config.requiredMessage }, ...this.getErrorTemplates() ], From 02875314ed182ed08fbdaa28411be7381b18a642 Mon Sep 17 00:00:00 2001 From: Alex Luckett Date: Thu, 19 Nov 2026 16:13:47 +0000 Subject: [PATCH 2/4] Assert the error text for the translated validation messages validate() now always applies the translated message overrides. Some of those messages had no test that checked their text, because the tests for invalid values only checked that an error was present. These tests check the text for: - a value that is not in the autocomplete list - a date day or month that is not a number, or is too large to be one - an easting that is not a number, or is too large to be one - a latitude that is too large to be a number --- .../components/AutocompleteField.test.ts | 10 ++++++++++ .../engine/components/DatePartsField.test.ts | 19 +++++++++++++++++++ .../components/EastingNorthingField.test.ts | 19 +++++++++++++++++++ .../engine/components/LatLongField.test.ts | 15 +++++++++++++++ .../engine/components/MonthYearField.test.ts | 17 +++++++++++++++++ 5 files changed, 80 insertions(+) diff --git a/src/server/plugins/engine/components/AutocompleteField.test.ts b/src/server/plugins/engine/components/AutocompleteField.test.ts index a1bbe2c60..0f8d341d9 100644 --- a/src/server/plugins/engine/components/AutocompleteField.test.ts +++ b/src/server/plugins/engine/components/AutocompleteField.test.ts @@ -215,6 +215,16 @@ describe.each([ expect(result1.errors).toBeTruthy() expect(result2.errors).toBeTruthy() }) + + it('adds the required error for a value that is not in the list', () => { + const result = collection.validate(getFormData('5')) + + expect(result.errors).toEqual([ + expect.objectContaining({ + text: `Enter ${lowerFirst(def.title)}` + }) + ]) + }) }) describe('State', () => { diff --git a/src/server/plugins/engine/components/DatePartsField.test.ts b/src/server/plugins/engine/components/DatePartsField.test.ts index 0a26ed1ed..b27560a8b 100644 --- a/src/server/plugins/engine/components/DatePartsField.test.ts +++ b/src/server/plugins/engine/components/DatePartsField.test.ts @@ -295,6 +295,25 @@ describe('DatePartsField', () => { expect(result4).toBeNull() expect(result5).toBe('2000-02-01') }) + + it.each([ + { + scenario: 'a day that is not a number', + day: 'abc', + text: 'Example date parts must include a day' + }, + { + scenario: 'a day that is too large to be a number', + day: '99999999999999999999', + text: 'Example date parts must be a real date' + } + ])('adds an error for $scenario', ({ day, text }) => { + const result = collection.validate( + getFormData({ day, month: '1', year: '2000' }) + ) + + expect(result.errors).toEqual([expect.objectContaining({ text })]) + }) }) describe('State', () => { diff --git a/src/server/plugins/engine/components/EastingNorthingField.test.ts b/src/server/plugins/engine/components/EastingNorthingField.test.ts index 2070eae6e..bc9ec71ae 100644 --- a/src/server/plugins/engine/components/EastingNorthingField.test.ts +++ b/src/server/plugins/engine/components/EastingNorthingField.test.ts @@ -221,6 +221,25 @@ describe('EastingNorthingField', () => { expect(result1.errors).toBeTruthy() expect(result2.errors).toBeTruthy() }) + + it.each([ + { + scenario: 'an easting that is not a number', + easting: 'abc', + text: 'Enter easting' + }, + { + scenario: 'an easting that is too large to be a number', + easting: '99999999999999999999', + text: 'Easting for example location must be between 1 and 6 digits' + } + ])('adds an error for $scenario', ({ easting, text }) => { + const result = collection.validate( + getFormData({ easting, northing: '1234567' }) + ) + + expect(result.errors).toEqual([expect.objectContaining({ text })]) + }) }) describe('State', () => { diff --git a/src/server/plugins/engine/components/LatLongField.test.ts b/src/server/plugins/engine/components/LatLongField.test.ts index bb4854447..8ff445ed5 100644 --- a/src/server/plugins/engine/components/LatLongField.test.ts +++ b/src/server/plugins/engine/components/LatLongField.test.ts @@ -209,6 +209,21 @@ describe('LatLongField', () => { expect(result1.errors).toBeTruthy() }) + + it('adds an error for a latitude that is too large to be a number', () => { + const result = collection.validate( + getFormData({ + latitude: '99999999999999999999', + longitude: '-0.127758' + }) + ) + + expect(result.errors).toEqual([ + expect.objectContaining({ + text: 'Latitude must be a valid number' + }) + ]) + }) }) describe('State', () => { diff --git a/src/server/plugins/engine/components/MonthYearField.test.ts b/src/server/plugins/engine/components/MonthYearField.test.ts index 6d625b41c..87d632e42 100644 --- a/src/server/plugins/engine/components/MonthYearField.test.ts +++ b/src/server/plugins/engine/components/MonthYearField.test.ts @@ -281,6 +281,23 @@ describe('MonthYearField', () => { expect(result4).toBeNull() expect(result5).toBe('2000-05') }) + + it.each([ + { + scenario: 'a month that is not a number', + month: 'abc', + text: 'Example month/year must include a month' + }, + { + scenario: 'a month that is too large to be a number', + month: '99999999999999999999', + text: 'Example month/year must be a real date' + } + ])('adds an error for $scenario', ({ month, text }) => { + const result = collection.validate(getFormData({ month, year: '2000' })) + + expect(result.errors).toEqual([expect.objectContaining({ text })]) + }) }) describe('State', () => { From f54002dcbabd0d414df015a402ab6010a5cc2d47 Mon Sep 17 00:00:00 2001 From: Alex Luckett Date: Thu, 19 Nov 2026 20:57:55 +0000 Subject: [PATCH 3/4] Move the error text tests into the Validation tables Each component test file has a Validation table of inputs and the exact output of validate(). The error text tests now sit in those tables, so that each file has one place for its validation errors. The Autocomplete Validation table had no test function, so its case did not run. It now runs for both lists, with the component of each list. --- .../components/AutocompleteField.test.ts | 46 +++++++++----- .../engine/components/DatePartsField.test.ts | 60 +++++++++++++------ .../components/EastingNorthingField.test.ts | 60 +++++++++++++------ .../engine/components/LatLongField.test.ts | 45 +++++++++----- .../engine/components/MonthYearField.test.ts | 50 ++++++++++------ 5 files changed, 175 insertions(+), 86 deletions(-) diff --git a/src/server/plugins/engine/components/AutocompleteField.test.ts b/src/server/plugins/engine/components/AutocompleteField.test.ts index 0f8d341d9..278fda10a 100644 --- a/src/server/plugins/engine/components/AutocompleteField.test.ts +++ b/src/server/plugins/engine/components/AutocompleteField.test.ts @@ -39,6 +39,7 @@ describe.each([ list: listString, examples: listStringExamples, allow: ['1', '2', '3', '4'], + notInList: '5', shortDescription: 'My string list' } }, @@ -55,6 +56,7 @@ describe.each([ list: listNumber, examples: listNumberExamples, allow: [1, 2, 3, 4], + notInList: 5, shortDescription: 'My number list' } } @@ -215,16 +217,6 @@ describe.each([ expect(result1.errors).toBeTruthy() expect(result2.errors).toBeTruthy() }) - - it('adds the required error for a value that is not in the list', () => { - const result = collection.validate(getFormData('5')) - - expect(result.errors).toEqual([ - expect.objectContaining({ - text: `Enter ${lowerFirst(def.title)}` - }) - ]) - }) }) describe('State', () => { @@ -367,12 +359,9 @@ describe.each([ { description: 'Use short description if it exists', component: { + ...def, title: 'What is your example text?', - shortDescription: 'Your example text', - name: 'myComponent', - type: ComponentType.AutocompleteField, - list: 'ABCE', - options: {} + shortDescription: 'Your example text' } satisfies AutocompleteFieldComponent, assertions: [ { @@ -385,10 +374,35 @@ describe.each([ }) ] } + }, + { + input: getFormData('5'), + output: { + value: getFormData(options.notInList), + errors: [ + expect.objectContaining({ + text: 'Enter your example text' + }) + ] + } } ] } - ]) + ])('$description', ({ component, assertions }) => { + let collection: ComponentCollection + + beforeEach(() => { + collection = new ComponentCollection([component], { model }) + }) + + it.each([...assertions])( + 'validates custom example', + ({ input, output }) => { + const result = collection.validate(input) + expect(result).toEqual(output) + } + ) + }) }) }) }) diff --git a/src/server/plugins/engine/components/DatePartsField.test.ts b/src/server/plugins/engine/components/DatePartsField.test.ts index b27560a8b..af7e65976 100644 --- a/src/server/plugins/engine/components/DatePartsField.test.ts +++ b/src/server/plugins/engine/components/DatePartsField.test.ts @@ -295,25 +295,6 @@ describe('DatePartsField', () => { expect(result4).toBeNull() expect(result5).toBe('2000-02-01') }) - - it.each([ - { - scenario: 'a day that is not a number', - day: 'abc', - text: 'Example date parts must include a day' - }, - { - scenario: 'a day that is too large to be a number', - day: '99999999999999999999', - text: 'Example date parts must be a real date' - } - ])('adds an error for $scenario', ({ day, text }) => { - const result = collection.validate( - getFormData({ day, month: '1', year: '2000' }) - ) - - expect(result.errors).toEqual([expect.objectContaining({ text })]) - }) }) describe('State', () => { @@ -607,6 +588,47 @@ describe('DatePartsField', () => { } ] }, + { + description: 'Values that are not numbers', + component: { + title: 'Example date parts field', + name: 'myComponent', + type: ComponentType.DatePartsField, + options: {} + } satisfies DatePartsFieldComponent, + assertions: [ + { + input: getFormData({ day: 'abc', month: '1', year: '2000' }), + output: { + value: getFormData({ day: 'abc', month: 1, year: 2000 }), + errors: [ + expect.objectContaining({ + text: 'Example date parts field must include a day' + }) + ] + } + }, + { + input: getFormData({ + day: '99999999999999999999', + month: '1', + year: '2000' + }), + output: { + value: getFormData({ + day: 100000000000000000000, + month: 1, + year: 2000 + }), + errors: [ + expect.objectContaining({ + text: 'Example date parts field must be a real date' + }) + ] + } + } + ] + }, { description: 'Leap years', component: { diff --git a/src/server/plugins/engine/components/EastingNorthingField.test.ts b/src/server/plugins/engine/components/EastingNorthingField.test.ts index bc9ec71ae..05bb64070 100644 --- a/src/server/plugins/engine/components/EastingNorthingField.test.ts +++ b/src/server/plugins/engine/components/EastingNorthingField.test.ts @@ -221,25 +221,6 @@ describe('EastingNorthingField', () => { expect(result1.errors).toBeTruthy() expect(result2.errors).toBeTruthy() }) - - it.each([ - { - scenario: 'an easting that is not a number', - easting: 'abc', - text: 'Enter easting' - }, - { - scenario: 'an easting that is too large to be a number', - easting: '99999999999999999999', - text: 'Easting for example location must be between 1 and 6 digits' - } - ])('adds an error for $scenario', ({ easting, text }) => { - const result = collection.validate( - getFormData({ easting, northing: '1234567' }) - ) - - expect(result.errors).toEqual([expect.objectContaining({ text })]) - }) }) describe('State', () => { @@ -754,6 +735,47 @@ describe('EastingNorthingField', () => { } ] }, + { + description: 'Values that are not numbers', + component: { + title: 'Example easting northing', + shortDescription: 'Example location', + name: 'myComponent', + type: ComponentType.EastingNorthingField, + options: {}, + schema: {} + } satisfies EastingNorthingFieldComponent, + assertions: [ + { + input: getFormData({ easting: 'abc', northing: '1234567' }), + output: { + value: getFormData({ easting: 'abc', northing: 1234567 }), + errors: [ + expect.objectContaining({ + text: 'Enter easting' + }) + ] + } + }, + { + input: getFormData({ + easting: '99999999999999999999', + northing: '1234567' + }), + output: { + value: getFormData({ + easting: 100000000000000000000, + northing: 1234567 + }), + errors: [ + expect.objectContaining({ + text: 'Easting for example location must be between 1 and 6 digits' + }) + ] + } + } + ] + }, { description: 'Optional field', component: { diff --git a/src/server/plugins/engine/components/LatLongField.test.ts b/src/server/plugins/engine/components/LatLongField.test.ts index 8ff445ed5..46359cff1 100644 --- a/src/server/plugins/engine/components/LatLongField.test.ts +++ b/src/server/plugins/engine/components/LatLongField.test.ts @@ -209,21 +209,6 @@ describe('LatLongField', () => { expect(result1.errors).toBeTruthy() }) - - it('adds an error for a latitude that is too large to be a number', () => { - const result = collection.validate( - getFormData({ - latitude: '99999999999999999999', - longitude: '-0.127758' - }) - ) - - expect(result.errors).toEqual([ - expect.objectContaining({ - text: 'Latitude must be a valid number' - }) - ]) - }) }) describe('State', () => { @@ -900,6 +885,36 @@ describe('LatLongField', () => { } ] }, + { + description: 'Values that are not numbers', + component: { + title: 'Example lat long', + shortDescription: 'Example location', + name: 'myComponent', + type: ComponentType.LatLongField, + options: {}, + schema: {} + } satisfies LatLongFieldComponent, + assertions: [ + { + input: getFormData({ + latitude: '99999999999999999999', + longitude: '-0.127758' + }), + output: { + value: getFormData({ + latitude: 100000000000000000000, + longitude: -0.127758 + }), + errors: [ + expect.objectContaining({ + text: 'Latitude must be a valid number' + }) + ] + } + } + ] + }, { description: 'Optional field', component: { diff --git a/src/server/plugins/engine/components/MonthYearField.test.ts b/src/server/plugins/engine/components/MonthYearField.test.ts index 87d632e42..6ddeab81c 100644 --- a/src/server/plugins/engine/components/MonthYearField.test.ts +++ b/src/server/plugins/engine/components/MonthYearField.test.ts @@ -281,23 +281,6 @@ describe('MonthYearField', () => { expect(result4).toBeNull() expect(result5).toBe('2000-05') }) - - it.each([ - { - scenario: 'a month that is not a number', - month: 'abc', - text: 'Example month/year must include a month' - }, - { - scenario: 'a month that is too large to be a number', - month: '99999999999999999999', - text: 'Example month/year must be a real date' - } - ])('adds an error for $scenario', ({ month, text }) => { - const result = collection.validate(getFormData({ month, year: '2000' })) - - expect(result.errors).toEqual([expect.objectContaining({ text })]) - }) }) describe('State', () => { @@ -545,6 +528,39 @@ describe('MonthYearField', () => { } ] }, + { + description: 'Values that are not numbers', + component: { + title: 'Example month/year field', + name: 'myComponent', + type: ComponentType.MonthYearField, + options: {} + } satisfies MonthYearFieldComponent, + assertions: [ + { + input: getFormData({ month: 'abc', year: '2000' }), + output: { + value: getFormData({ month: 'abc', year: 2000 }), + errors: [ + expect.objectContaining({ + text: 'Example month/year field must include a month' + }) + ] + } + }, + { + input: getFormData({ month: '99999999999999999999', year: '2000' }), + output: { + value: getFormData({ month: 100000000000000000000, year: 2000 }), + errors: [ + expect.objectContaining({ + text: 'Example month/year field must be a real date' + }) + ] + } + } + ] + }, { description: 'Out of range values', component: { From c19cd798a4c130385b90d1ef25239394dba82439 Mon Sep 17 00:00:00 2001 From: Alex Luckett Date: Thu, 19 Nov 2026 21:49:50 +0000 Subject: [PATCH 4/4] Test the errors for a partly filled lat long or easting northing A user who fills in only one part of the field gets an error for the missing part. No test checked the text of that error, for a required or an optional field. --- .../components/EastingNorthingField.test.ts | 50 +++++++++++++++++++ .../engine/components/LatLongField.test.ts | 50 +++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/src/server/plugins/engine/components/EastingNorthingField.test.ts b/src/server/plugins/engine/components/EastingNorthingField.test.ts index 05bb64070..db9c5065e 100644 --- a/src/server/plugins/engine/components/EastingNorthingField.test.ts +++ b/src/server/plugins/engine/components/EastingNorthingField.test.ts @@ -776,6 +776,39 @@ describe('EastingNorthingField', () => { } ] }, + { + description: 'Partly filled', + component: { + title: 'Example easting northing', + shortDescription: 'Example location', + name: 'myComponent', + type: ComponentType.EastingNorthingField, + options: {}, + schema: {} + } satisfies EastingNorthingFieldComponent, + assertions: [ + { + input: getFormData({ + easting: '12345', + northing: '' + }), + output: { + value: getFormData({ + easting: 12345, + northing: '' + }), + errors: [ + expect.objectContaining({ + text: 'Enter northing' + }), + expect.objectContaining({ + text: 'Example location must include a northing' + }) + ] + } + } + ] + }, { description: 'Optional field', component: { @@ -799,6 +832,23 @@ describe('EastingNorthingField', () => { northing: '' }) } + }, + { + input: getFormData({ + easting: '12345', + northing: '' + }), + output: { + value: getFormData({ + easting: 12345, + northing: '' + }), + errors: [ + expect.objectContaining({ + text: 'Example easting northing must include a northing' + }) + ] + } } ] }, diff --git a/src/server/plugins/engine/components/LatLongField.test.ts b/src/server/plugins/engine/components/LatLongField.test.ts index 46359cff1..421685652 100644 --- a/src/server/plugins/engine/components/LatLongField.test.ts +++ b/src/server/plugins/engine/components/LatLongField.test.ts @@ -915,6 +915,39 @@ describe('LatLongField', () => { } ] }, + { + description: 'Partly filled', + component: { + title: 'Example lat long', + shortDescription: 'Example location', + name: 'myComponent', + type: ComponentType.LatLongField, + options: {}, + schema: {} + } satisfies LatLongFieldComponent, + assertions: [ + { + input: getFormData({ + latitude: '51.5', + longitude: '' + }), + output: { + value: getFormData({ + latitude: 51.5, + longitude: '' + }), + errors: [ + expect.objectContaining({ + text: 'Enter longitude' + }), + expect.objectContaining({ + text: 'Example location must include a longitude' + }) + ] + } + } + ] + }, { description: 'Optional field', component: { @@ -938,6 +971,23 @@ describe('LatLongField', () => { longitude: '' }) } + }, + { + input: getFormData({ + latitude: '51.5', + longitude: '' + }), + output: { + value: getFormData({ + latitude: 51.5, + longitude: '' + }), + errors: [ + expect.objectContaining({ + text: 'Example lat long must include a longitude' + }) + ] + } } ] }