Skip to content

Commit d0f2f5a

Browse files
authored
fix(DF-889): enhance postcode validation and add error handling in manual address (#329)
1 parent fc472d6 commit d0f2f5a

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/server/plugins/postcode-lookup/models/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,14 @@ export const manualPayloadSchema = Joi.object()
343343
'*': 'Enter town or city'
344344
}),
345345
[countyFieldName]: Joi.string().trim().allow('').required(),
346-
[postcodeFieldName]: Joi.string().trim().required().messages({
347-
'*': 'Enter postcode'
348-
})
346+
[postcodeFieldName]: Joi.string()
347+
.pattern(/^[a-zA-Z]{1,2}\d[a-zA-Z\d]?\s?\d[a-zA-Z]{2}$/)
348+
.trim()
349+
.required()
350+
.messages({
351+
'string.pattern.base': 'Enter a valid postcode',
352+
'*': 'Enter postcode'
353+
})
349354
})
350355
.required()
351356

test/form/postcode-lookup.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,51 @@ describe('Postcode lookup form pages', () => {
556556
expect($heading).toBeInTheDocument()
557557
})
558558

559+
it('should render validation error when invalid postcode is provided on manual page', async () => {
560+
const { csrfToken, headers } = await initialiseJourney(server)
561+
562+
// Dispatch to postcode journey
563+
await server.inject({
564+
url: `${basePath}/address`,
565+
method: 'POST',
566+
headers,
567+
payload: {
568+
action: 'external-ybMHIv',
569+
crumb: csrfToken
570+
}
571+
})
572+
573+
const { response, container } = await renderResponse(server, {
574+
url: '/postcode-lookup?step=manual',
575+
method: 'POST',
576+
headers,
577+
payload: {
578+
step: 'manual',
579+
addressLine1: '1 Street Name',
580+
addressLine2: '',
581+
town: 'Middletown',
582+
county: '',
583+
postcode: 'INVALID123',
584+
crumb: csrfToken
585+
}
586+
})
587+
588+
expect(response.statusCode).toBe(StatusCodes.OK)
589+
const $errorSummary = container.getByRole('alert')
590+
591+
const $heading = within($errorSummary).getByRole('heading', {
592+
name: 'There is a problem',
593+
level: 2
594+
})
595+
expect($heading).toBeInTheDocument()
596+
597+
// Verify the specific postcode error message is shown
598+
const $postcodeError = container.getByRole('link', {
599+
name: 'Enter a valid postcode'
600+
})
601+
expect($postcodeError).toBeInTheDocument()
602+
})
603+
559604
it('should redirect back to the source page after successful POST to manual page', async () => {
560605
const { csrfToken, headers } = await initialiseJourney(server)
561606

0 commit comments

Comments
 (0)