Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
c68686f
Show validation errors in English when validate gets no translator
alexluckett Nov 16, 2026
18b6797
Test Welsh text and errors for file upload, guidance and payment
alexluckett Nov 16, 2026
81028ae
Keep the global messages for an optional autocomplete field
alexluckett Nov 17, 2026
98901e3
Test Welsh validation errors other than required, and the repeater error
alexluckett Nov 17, 2026
1a74ceb
Require the translator in overrideParentTitle
alexluckett Nov 17, 2026
147342a
Fork each translated field schema in the loop that builds it
alexluckett Nov 17, 2026
0b0a170
Update the validate comment, because validate always translates
alexluckett Nov 17, 2026
1e532fa
Move the field schema fork into replaceFieldSchema
alexluckett Nov 17, 2026
773131c
Merge branch 'main' into fix/v5-validation-messages
alexluckett Nov 17, 2026
2079aa2
Test the file upload min, max and length errors in English and Welsh
alexluckett Nov 17, 2026
49edb19
Remove the files and features global messages
alexluckett Nov 17, 2026
d799bd5
Merge branch 'fix/v5-validation-messages' of github.com:DEFRA/forms-e…
alexluckett Nov 17, 2026
94c038c
Move the file upload limits tests into the file upload test file
alexluckett Nov 17, 2026
c5b2cd4
Move the Welsh repeater test into the repeat test file
alexluckett Nov 17, 2026
58533a2
Move the Welsh text minimum and number errors into the Welsh fields test
alexluckett Nov 17, 2026
fa1f478
Remove the unused file minimum from the Welsh components form
alexluckett Nov 17, 2026
108e5a1
Merge remote-tracking branch 'origin/main' into fix/v5-validation-mes…
alexluckett Nov 19, 2026
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
11 changes: 11 additions & 0 deletions src/server/plugins/engine/components/AutocompleteField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ describe.each([
expect(result.errors).toBeUndefined()
})

it('adds the select error for an invalid optional value', () => {
const collectionOptional = new ComponentCollection(
[{ ...def, options: { required: false } }],
{ model }
)

const result = collectionOptional.validate(getFormData('invalid'))

expect(result.errors?.[0].text).toBe(`Select ${lowerFirst(def.title)}`)
})

it('is configured with autocomplete suggestions', () => {
const { formSchema } = collection
const { keys } = formSchema.describe()
Expand Down
12 changes: 9 additions & 3 deletions src/server/plugins/engine/components/AutocompleteField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ export class AutocompleteField extends SelectField {
}

getValidationMessagesOverride(translator: Translator) {
const { customValidationMessages } = this.options
const { customValidationMessages, required } = this.options

// An optional field keeps the global messages, as the constructor does
if (required === false) {
return null
}

const { t } = translator
const defaults = {
'any.required': buildValidationMessages(t).objectRequired,
'any.only': buildValidationMessages(t).objectRequired
'any.required': buildValidationMessages(t).required,
'any.only': buildValidationMessages(t).required
}

return {
Expand Down
120 changes: 61 additions & 59 deletions src/server/plugins/engine/components/ComponentCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export class ComponentCollection {

overrideParentTitle(
formSchema: joi.ObjectSchema<FormPayload>,
translator: Translator | undefined
translator: Translator
) {
// Add parent field title to collection field errors
return formSchema.error((errors) => {
Expand All @@ -249,8 +249,14 @@ export class ComponentCollection {
(item) => item.name === key
)

// Update error with child label
if (child && (!error.local.label || error.local.label === 'value')) {
// Update error with child label. A composite field child has a key as
// its title, so that the label shows in the language of the translator
if (child && parent?.collection) {
error.local.label = translator.t(child.title)
} else if (
child &&
(!error.local.label || error.local.label === 'value')
) {
error.local.label = child.title
}

Expand All @@ -261,10 +267,8 @@ export class ComponentCollection {
}

// Update error with parent title
if (translator && parent) {
if (parent) {
error.local.title ??= getTranslatedLabel(parent.def, translator)
} else {
error.local.title ??= parent?.label
}

return error
Expand All @@ -277,73 +281,59 @@ export class ComponentCollection {
*/
validate(
value: FormPayload = {},
translator?: Translator
translator: Translator = this.model.createTranslator()
): FormValidationResult<FormPayload> {
const messages = translator
? buildLanguageMessages(translator.t)
: undefined
const messages = buildLanguageMessages(translator.t)

// When translating, override Joi labels (and custom schema messages where
// needed) so #label/#title in message templates resolve in the correct language.
// Override Joi labels (and custom schema messages where needed) so that
// #label/#title in message templates resolve in the language of the translator
let schema = this.formSchema
// Add parent field title to collection field errors
schema = this.overrideParentTitle(schema, translator)

if (translator) {
const { t } = translator
const labelOverrides: Record<string, joi.Schema> = {}

for (const field of this.fields) {
if (field.collection) {
// Composite field: translate each sub-field's label using the key constant
// (e.g. 'components.addressField.line1' → "Llinell cyfeiriad 1") and apply
// any field-type-specific message overrides (e.g. objectMissing for dates).
const messagesOverride =
field.getValidationMessagesOverride(translator)
for (const subField of field.collection.fields) {
const translatedSubLabel = t(subField.title) || subField.label
let patchedSchema = subField.formSchema.label(translatedSubLabel)
const subFieldMessages = messagesOverride
? messagesOverride[subField.name]
: undefined
if (subFieldMessages) {
patchedSchema = patchedSchema.messages(subFieldMessages)
}
labelOverrides[subField.name] = patchedSchema
}
} else {
const fieldDef = field.def
const translatedLabel = getTranslatedLabel(fieldDef, translator)
const messagesOverride =
field.getValidationMessagesOverride(translator)
let patchedSchema = field.formSchema
if (translatedLabel && translatedLabel !== field.label) {
patchedSchema = patchedSchema.label(translatedLabel)
}
const fieldMessages = messagesOverride
? messagesOverride[field.name]
const { t } = translator

for (const field of this.fields) {
if (field.collection) {
// Composite field: translate each sub-field's label using the key constant
// (e.g. 'components.addressField.line1' → "Llinell cyfeiriad 1") and apply
// any field-type-specific message overrides (e.g. objectMissing for dates).
const messagesOverride = field.getValidationMessagesOverride(translator)
for (const subField of field.collection.fields) {
const translatedSubLabel = t(subField.title) || subField.label
let patchedSchema = subField.formSchema.label(translatedSubLabel)
const subFieldMessages = messagesOverride
? messagesOverride[subField.name]
: undefined
if (fieldMessages) {
patchedSchema = patchedSchema.messages(fieldMessages)
}
if (patchedSchema !== field.formSchema) {
labelOverrides[field.name] = patchedSchema
if (subFieldMessages) {
patchedSchema = patchedSchema.messages(subFieldMessages)
}
schema = this.replaceFieldSchema(schema, subField.name, patchedSchema)
}
} else {
const fieldDef = field.def
const translatedLabel = getTranslatedLabel(fieldDef, translator)
const messagesOverride = field.getValidationMessagesOverride(translator)
let patchedSchema = field.formSchema
if (translatedLabel && translatedLabel !== field.label) {
patchedSchema = patchedSchema.label(translatedLabel)
}
const fieldMessages = messagesOverride
? messagesOverride[field.name]
: undefined
if (fieldMessages) {
patchedSchema = patchedSchema.messages(fieldMessages)
}
if (patchedSchema !== field.formSchema) {
schema = this.replaceFieldSchema(schema, field.name, patchedSchema)
}
}

if (Object.keys(labelOverrides).length) {
schema = schema.keys(labelOverrides)
}
}

const result = schema.validate(value, {
...opts,
...(messages && { messages })
})
const result = schema.validate(value, { ...opts, messages })

const details = result.error?.details
const language = translator?.language
const { language } = translator

return {
value: (result.value ?? {}) as typeof value,
Expand All @@ -352,6 +342,18 @@ export class ComponentCollection {
}
}

/**
* Replace the schema of one field in the form schema. The field keeps its
* position, so that the errors stay in the same order as the page
*/
private replaceFieldSchema(
formSchema: ObjectSchema<FormPayload>,
name: string,
fieldSchema: joi.Schema
) {
return formSchema.fork(name, () => fieldSchema)
}

/**
* Helper to get errors from all fields
*/
Expand Down
11 changes: 4 additions & 7 deletions src/server/plugins/engine/components/DatePartsField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,9 @@ describe('DatePartsField', () => {
)

expect(result1.errors).toBeUndefined()
// Sub-field title is a key constant; error text uses the raw key until
// request-time resolution is wired in (Task 8 / Task 9).
expect(result2.errors).toEqual([
expect.objectContaining({
text: 'Example date parts field must include a components.dateField.month'
text: 'Example date parts field must include a month'
})
])
})
Expand Down Expand Up @@ -244,16 +242,15 @@ describe('DatePartsField', () => {
})
)

// Sub-field titles are key constants; error text uses raw keys until Task 9.
expect(result.errors).toEqual([
expect.objectContaining({
text: 'Example date parts must include a components.dateField.day'
text: 'Example date parts must include a day'
}),
expect.objectContaining({
text: 'Example date parts must include a components.dateField.month'
text: 'Example date parts must include a month'
}),
expect.objectContaining({
text: 'Example date parts must include a components.dateField.year'
text: 'Example date parts must include a year'
})
])
})
Expand Down
19 changes: 6 additions & 13 deletions src/server/plugins/engine/components/EastingNorthingField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,7 @@ describe('EastingNorthingField', () => {
errors: [
expect.objectContaining({
text: expect.stringMatching(
/components\.eastingNorthingField\.easting for .* must be between 1000 and 60000/
// Sub-field title is a key constant; resolved at request time (Task 8/9).
/Easting for .* must be between 1000 and 60000/
)
})
]
Expand All @@ -623,8 +622,7 @@ describe('EastingNorthingField', () => {
errors: [
expect.objectContaining({
text: expect.stringMatching(
/components\.eastingNorthingField\.easting for .* must be between 1000 and 60000/
// Sub-field title is a key constant; resolved at request time (Task 8/9).
/Easting for .* must be between 1000 and 60000/
)
})
]
Expand Down Expand Up @@ -660,8 +658,7 @@ describe('EastingNorthingField', () => {
errors: [
expect.objectContaining({
text: expect.stringMatching(
/components\.eastingNorthingField\.northing for .* must be between 1000 and 1200000/
// Sub-field title is a key constant; resolved at request time (Task 8/9).
/Northing for .* must be between 1000 and 1200000/
)
})
]
Expand All @@ -680,8 +677,7 @@ describe('EastingNorthingField', () => {
errors: [
expect.objectContaining({
text: expect.stringMatching(
/components\.eastingNorthingField\.northing for .* must be between 1000 and 1200000/
// Sub-field title is a key constant; resolved at request time (Task 8/9).
/Northing for .* must be between 1000 and 1200000/
)
})
]
Expand Down Expand Up @@ -712,8 +708,7 @@ describe('EastingNorthingField', () => {
errors: [
expect.objectContaining({
text: expect.stringMatching(
/components\.eastingNorthingField\.easting for .* must be between 1 and 6 digits/
// Sub-field title is a key constant; resolved at request time (Task 8/9).
/Easting for .* must be between 1 and 6 digits/
)
})
]
Expand All @@ -732,8 +727,7 @@ describe('EastingNorthingField', () => {
errors: [
expect.objectContaining({
text: expect.stringMatching(
/components\.eastingNorthingField\.northing for .* must be between 1 and 7 digits/
// Sub-field title is a key constant; resolved at request time (Task 8/9).
/Northing for .* must be between 1 and 7 digits/
)
})
]
Expand Down Expand Up @@ -793,7 +787,6 @@ describe('EastingNorthingField', () => {
expect.objectContaining({
text: expect.stringMatching(
/myComponent failed custom validation because/
// Sub-field title is a key constant; resolved at request time (Task 8/9).
)
})
]
Expand Down
12 changes: 4 additions & 8 deletions src/server/plugins/engine/components/LatLongField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,7 @@ describe('LatLongField', () => {
}),
errors: [
expect.objectContaining({
text: 'components.latLongField.latitude must have no more than 7 decimal places'
// Sub-field title is a key constant; resolved at request time (Task 8/9).
text: 'Latitude must have no more than 7 decimal places'
})
]
}
Expand All @@ -702,8 +701,7 @@ describe('LatLongField', () => {
}),
errors: [
expect.objectContaining({
text: 'components.latLongField.longitude must have no more than 7 decimal places'
// Sub-field title is a key constant; resolved at request time (Task 8/9).
text: 'Longitude must have no more than 7 decimal places'
})
]
}
Expand Down Expand Up @@ -781,8 +779,7 @@ describe('LatLongField', () => {
}),
errors: [
expect.objectContaining({
text: 'components.latLongField.latitude must have no more than 7 decimal places'
// Sub-field title is a key constant; resolved at request time (Task 8/9).
text: 'Latitude must have no more than 7 decimal places'
})
]
}
Expand Down Expand Up @@ -812,8 +809,7 @@ describe('LatLongField', () => {
}),
errors: [
expect.objectContaining({
text: 'components.latLongField.longitude must have no more than 7 decimal places'
// Sub-field title is a key constant; resolved at request time (Task 8/9).
text: 'Longitude must have no more than 7 decimal places'
})
]
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/plugins/engine/components/LocationFieldBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ export abstract class LocationFieldBase extends FormComponent {
const config = this.getValidationConfig(translator)
const defaults = {
'any.required':
config.requiredMessage ?? buildValidationMessages(t).objectRequired,
config.requiredMessage ?? buildValidationMessages(t).required,
'string.empty':
config.requiredMessage ?? buildValidationMessages(t).objectRequired,
config.requiredMessage ?? buildValidationMessages(t).required,
'string.pattern.base': config.patternErrorMessage
}

Expand Down
Loading
Loading