diff --git a/src/server/plugins/engine/components/AutocompleteField.test.ts b/src/server/plugins/engine/components/AutocompleteField.test.ts index a1bbe2c60..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' } } @@ -357,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: [ { @@ -375,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 0a26ed1ed..af7e65976 100644 --- a/src/server/plugins/engine/components/DatePartsField.test.ts +++ b/src/server/plugins/engine/components/DatePartsField.test.ts @@ -588,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 2070eae6e..db9c5065e 100644 --- a/src/server/plugins/engine/components/EastingNorthingField.test.ts +++ b/src/server/plugins/engine/components/EastingNorthingField.test.ts @@ -735,6 +735,80 @@ 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: '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: { @@ -758,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 bb4854447..421685652 100644 --- a/src/server/plugins/engine/components/LatLongField.test.ts +++ b/src/server/plugins/engine/components/LatLongField.test.ts @@ -885,6 +885,69 @@ 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: '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: { @@ -908,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' + }) + ] + } } ] } 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() ], diff --git a/src/server/plugins/engine/components/MonthYearField.test.ts b/src/server/plugins/engine/components/MonthYearField.test.ts index 6d625b41c..6ddeab81c 100644 --- a/src/server/plugins/engine/components/MonthYearField.test.ts +++ b/src/server/plugins/engine/components/MonthYearField.test.ts @@ -528,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: {