Skip to content

Commit

Permalink
HOFF-986: Fix bug where postcode with punctuation was not allowed
Browse files Browse the repository at this point in the history
- amend postcode-lookup component to remove punctuation from postcode during api call to fix 400 error
- amend postcode-lookup component to remove punctuation from postcode in locals
- amend postcode-lookup component to remove punctuation from postcode in prepopulated manual fields
- amend postcode validation to allow all punctuation
- update yarn.lock
- remove unused vscode folder
  • Loading branch information
Rhodine-orleans-lindsay committed Oct 21, 2024
1 parent b7a67fb commit 2440040
Show file tree
Hide file tree
Showing 5 changed files with 984 additions and 1,257 deletions.
2 changes: 0 additions & 2 deletions .vscode/settings.json

This file was deleted.

10 changes: 5 additions & 5 deletions components/postcode-lookup/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable func-names */
/* eslint-disable func-names, max-len */
'use strict';

const path = require('path');
Expand Down Expand Up @@ -222,7 +222,7 @@ module.exports = config => {
locals(req, res, callback) {
const isManualStep = req.query.step === 'manual';
const locals = super.locals(req, res, callback);
const sessionPostcode = req.sessionModel.get(`${addressFieldNamePrefix}-postcode`);
const sessionPostcode = req.sessionModel.get(`${addressFieldNamePrefix}-postcode`)?.replace(/[^\w]+/g, '').replace(/^(.*)(\d)/, '$1 $2');
const section = this.options.route.replace(/^\//, '');
const editLink = conditionalTranslate('pages.address-lookup.edit', req.translate) || defaults.CHANGE;
const searchByPostcodeLink = conditionalTranslate('pages.address.searchByPostcode', req.translate) ||
Expand Down Expand Up @@ -278,8 +278,8 @@ module.exports = config => {
async postcode(req, res, callback) {
// Clear the value stored in the addresses radio button group
req.sessionModel.set(`${addressFieldNamePrefix}-select`, '');
// Call OS Places API to return list of addresses by postcode
const enteredPostcode = req.form.values[`${addressFieldNamePrefix}-postcode`];
// Replace punctuation in postcode and call OS Places API to return list of addresses
const enteredPostcode = req.form.values[`${addressFieldNamePrefix}-postcode`]?.replace(/[^\w]+/g, '').replace(/^(.*)(\d)/, '$1 $2');

await PostcodeLookup(enteredPostcode, apiURL, apiKey)
.then(function (response) {
Expand Down Expand Up @@ -343,7 +343,7 @@ module.exports = config => {
saveValues(req, res, callback) {
const step = req.query.step;
if (step === 'postcode') {
postcode = req.form.values[`${addressFieldNamePrefix}-postcode`];
postcode = req.form.values[`${addressFieldNamePrefix}-postcode`]?.replace(/[^\w]+/g, '').replace(/^(.*)(\d)/, '$1 $2');
this.clearAddressFieldValues(req);
return this.postcode(req, res, err => {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion controller/validation/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ module.exports = Validators = {

postcodeValidation(value) {
// eslint-disable-next-line max-len
return Validators.regex(value, /^(([GIRgir] ?[ ]*0[ ]*[Aa]{2}[ ]*)|((([A-Za-z][ ]*[0-9]{1,2}[ ]*)|(([A-Za-z][ ]*[A-Ha-hJ-Yj-y][ ]*[0-9][ ]*[0-9]?)[ ]*|(([A-Za-z][ ]*[0-9][ ]*[A-Za-z][ ]*)|([A-Za-z][ ]*[A-Ha-hJ-Yj-y][ ]*[0-9]?[ ]*[A-Za-z][ ]*))))[-,. ]?[ ]*[0-9][ ]*[A-Za-z][ ]*[A-Za-z][ ]*))$/);
return Validators.regex(value, /^[^\w]*(([GIRgir] ?[ ]*0[ ]*[Aa]{2}[ ]*)|((([A-Za-z][ ]*[0-9]{1,2}[ ]*)|(([A-Za-z][^\w]*[ ]*[A-Ha-hJ-Yj-y][^\w]*[ ]*[0-9][^\w]*[ ]*[0-9]?)[ ]*|(([A-Za-z][ ]*[0-9][ ]*[A-Za-z][ ]*)|([A-Za-z][ ]*[A-Ha-hJ-Yj-y][ ]*[0-9]?[ ]*[A-Za-z][ ]*))))[^\w]*?[ ]*[0-9][^\w]*[ ]*[A-Za-z][^\w]*[ ]*[A-Za-z][ ]*)[^\w]*)$/);
}

};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hof",
"description": "A bootstrap for HOF projects",
"version": "21.2.0-postcode-lookup-beta",
"version": "21.2.0-postcode-lookup-beta.7",
"license": "MIT",
"main": "index.js",
"author": "HomeOffice",
Expand Down
Loading

0 comments on commit 2440040

Please sign in to comment.