Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/server/plugins/engine/components/AutocompleteField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe.each([
list: listString,
examples: listStringExamples,
allow: ['1', '2', '3', '4'],
notInList: '5',
shortDescription: 'My string list'
}
},
Expand All @@ -55,6 +56,7 @@ describe.each([
list: listNumber,
examples: listNumberExamples,
allow: [1, 2, 3, 4],
notInList: 5,
shortDescription: 'My number list'
}
}
Expand Down Expand Up @@ -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: [
{
Expand All @@ -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)
}
)
})
})
})
})
41 changes: 41 additions & 0 deletions src/server/plugins/engine/components/DatePartsField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
91 changes: 91 additions & 0 deletions src/server/plugins/engine/components/EastingNorthingField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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'
})
]
}
}
]
},
Expand Down
80 changes: 80 additions & 0 deletions src/server/plugins/engine/components/LatLongField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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'
})
]
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 5 additions & 10 deletions src/server/plugins/engine/components/LocationFieldBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,7 +33,7 @@ interface LocationFieldOptions {
interface ValidationConfig {
pattern: RegExp
patternErrorMessage: JoiExpression
requiredMessage?: JoiExpression
requiredMessage: JoiExpression
}

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand All @@ -173,7 +168,7 @@ export abstract class LocationFieldBase extends FormComponent {
baseErrors: [
{
type: 'required',
template: config.requiredMessage ?? messageTemplate.required
template: config.requiredMessage
},
...this.getErrorTemplates()
],
Expand Down
Loading
Loading