Repository: UK-Export-Finance/dtfs2
Pull request #4283: fix(DTFS2-7521): correct validation error
Author: login: abhi-markan
Labels: name: Type: Fix
description: Fix
Created: 2025-03-27T17:10:37Z | Merged: 2025-03-28T13:46:40Z

## Introduction :pencil2:
Ensure correct validation error is thrown when the number input is supplied to portal amendment facility value field.

## Resolution :heavy_check_mark:
* Amended the error message to `Enter the new facility value in number format`.
* Updated unit and E2E tests.


Diff:
diff --git a/e2e-tests/ukef/cypress/e2e/journeys/portal/amendments/pages/facility-value.spec.js b/e2e-tests/ukef/cypress/e2e/journeys/portal/amendments/pages/facility-value.spec.js
index 6b07130ec70..7cd7259218a 100644
--- a/e2e-tests/ukef/cypress/e2e/journeys/portal/amendments/pages/facility-value.spec.js
+++ b/e2e-tests/ukef/cypress/e2e/journeys/portal/amendments/pages/facility-value.spec.js
@@ -7,6 +7,7 @@ import whatDoYouNeedToChange from '../../../../../../../gef/cypress/e2e/pages/am
 import facilityValue from '../../../../../../../gef/cypress/e2e/pages/amendments/facility-value';
 
 const { BANK1_MAKER1 } = MOCK_USERS;
+const invalidNumberError = 'Enter the new facility value in number format';
 
 context('Amendments - Facility value - page tests', () => {
   /**
@@ -74,37 +75,52 @@ context('Amendments - Facility value - page tests', () => {
     cy.clickContinueButton();
 
     facilityValue.errorSummary().should('be.visible');
-    facilityValue.errorSummary().contains('Enter the new facility value in number format');
+    facilityValue.errorSummary().contains(invalidNumberError);
 
     facilityValue.facilityValueInlineError().should('be.visible');
-    facilityValue.facilityValueInlineError().contains('Enter the new facility value in number format');
+    facilityValue.facilityValueInlineError().contains(invalidNumberError);
   });
 
   const errorTestCases = [
     {
       description: 'the value contains non-numeric characters',
       value: '1000x',
-      expectedErrorMessage: 'Enter a valid facility value',
+      expectedErrorMessage: '',
+    },
+    {
+      description: 'the value contains string characters',
+      value: 'abc',
+      expectedErrorMessage: invalidNumberError,
+    },
+    {
+      description: 'the value contains special characters',
+      value: '!@£',
+      expectedErrorMessage: invalidNumberError,
+    },
+    {
+      description: 'the value contains number and non-numeric characters',
+      value: '1ooo',
+      expectedErrorMessage: invalidNumberError,
     },
     {
       description: 'the value contains too many decimal places',
       value: '1000.000',
-      expectedErrorMessage: 'Enter a valid facility value',
+      expectedErrorMessage: invalidNumberError,
     },
     {
       description: 'the value contains too many decimal points',
       value: '1000.00.0',
-      expectedErrorMessage: 'Enter a valid facility value',
+      expectedErrorMessage: invalidNumberError,
     },
     {
       description: 'the value contains has no leading digit',
       value: '.99',
-      expectedErrorMessage: 'Enter a valid facility value',
+      expectedErrorMessage: invalidNumberError,
     },
     {
       description: 'the value ends with a decimal place',
       value: '123.',
-      expectedErrorMessage: 'Enter a valid facility value',
+      expectedErrorMessage: invalidNumberError,
     },
     {
       description: 'the value is too small',
diff --git a/gef-ui/api-tests/amendments/facility-value.post.api-test.ts b/gef-ui/api-tests/amendments/facility-value.post.api-test.ts
index 7db7426936c..8a160ff32de 100644
--- a/gef-ui/api-tests/amendments/facility-value.post.api-test.ts
+++ b/gef-ui/api-tests/amendments/facility-value.post.api-test.ts
@@ -142,9 +142,22 @@ describe(`POST ${url}`, () => {
       expect(response.headers.location).toEqual('/not-found');
     });
 
+    it('should render facility value page with errors if facility value is not a number', async () => {
+      // Arrange
+      const facilityValue = '1000x';
+
+      // Act
+      const response = await postWithSessionCookie({ facilityValue }, sessionCookie);
+
+      // Assert
+      expect(response.status).toEqual(HttpStatusCode.Ok);
+      expect(response.text).toContain('New facility value');
+      expect(response.text).toContain('Enter the new facility value in number format');
+    });
+
     it('should render facility value page with errors if facility value is invalid', async () => {
       // Arrange
-      const facilityValue = 'not a number';
+      const facilityValue = '0.99';
 
       // Act
       const response = await postWithSessionCookie({ facilityValue }, sessionCookie);
@@ -155,6 +168,19 @@ describe(`POST ${url}`, () => {
       expect(response.text).toContain('Enter a valid facility value');
     });
 
+    it('should render facility value page with errors if facility value is too high', async () => {
+      // Arrange
+      const facilityValue = '1000000000000.01';
+
+      // Act
+      const response = await postWithSessionCookie({ facilityValue }, sessionCookie);
+
+      // Assert
+      expect(response.status).toEqual(HttpStatusCode.Ok);
+      expect(response.text).toContain('New facility value');
+      expect(response.text).toContain('The new facility value is too high');
+    });
+
     it('should redirect to the next page if the facility value is valid', async () => {
       // Arrange
       const facilityValue = '10000';
diff --git a/gef-ui/server/controllers/amendments/facility-value/validation.test.ts b/gef-ui/server/controllers/amendments/facility-value/validation.test.ts
index c5d3650a35a..712e7662f57 100644
--- a/gef-ui/server/controllers/amendments/facility-value/validation.test.ts
+++ b/gef-ui/server/controllers/amendments/facility-value/validation.test.ts
@@ -1,6 +1,7 @@
 import { validateFacilityValue } from './validation';
 
 const errRef = 'facilityValue';
+const invalidNumberError = 'Enter the new facility value in number format';
 
 describe('validateFacilityValue', () => {
   const errorTestCases = [
@@ -9,7 +10,7 @@ describe('validateFacilityValue', () => {
       value: '',
       expectedError: {
         errRef,
-        errMsg: 'Enter the new facility value in number format',
+        errMsg: invalidNumberError,
       },
     },
     {
@@ -17,7 +18,31 @@ describe('validateFacilityValue', () => {
       value: '1000x',
       expectedError: {
         errRef,
-        errMsg: 'Enter a valid facility value',
+        errMsg: invalidNumberError,
+      },
+    },
+    {
+      description: 'the value contains too many decimal places',
+      value: 'abc',
+      expectedError: {
+        errRef,
+        errMsg: invalidNumberError,
+      },
+    },
+    {
+      description: 'the value contains too many decimal places',
+      value: '!@£',
+      expectedError: {
+        errRef,
+        errMsg: invalidNumberError,
+      },
+    },
+    {
+      description: 'the value contains too many decimal places',
+      value: '1ooo',
+      expectedError: {
+        errRef,
+        errMsg: invalidNumberError,
       },
     },
     {
@@ -25,7 +50,7 @@ describe('validateFacilityValue', () => {
       value: '1000.000',
       expectedError: {
         errRef,
-        errMsg: 'Enter a valid facility value',
+        errMsg: invalidNumberError,
       },
     },
     {
@@ -33,7 +58,7 @@ describe('validateFacilityValue', () => {
       value: '1000.00.0',
       expectedError: {
         errRef,
-        errMsg: 'Enter a valid facility value',
+        errMsg: invalidNumberError,
       },
     },
     {
@@ -41,7 +66,7 @@ describe('validateFacilityValue', () => {
       value: '.99',
       expectedError: {
         errRef,
-        errMsg: 'Enter a valid facility value',
+        errMsg: invalidNumberError,
       },
     },
     {
@@ -49,7 +74,7 @@ describe('validateFacilityValue', () => {
       value: '123.',
       expectedError: {
         errRef,
-        errMsg: 'Enter a valid facility value',
+        errMsg: invalidNumberError,
       },
     },
     {
diff --git a/gef-ui/server/controllers/amendments/facility-value/validation.ts b/gef-ui/server/controllers/amendments/facility-value/validation.ts
index 3d8cdd3d3ea..1eae89af488 100644
--- a/gef-ui/server/controllers/amendments/facility-value/validation.ts
+++ b/gef-ui/server/controllers/amendments/facility-value/validation.ts
@@ -12,20 +12,9 @@ const errRef = 'facilityValue';
  * @returns the value or errors depending on the validation result
  */
 export const validateFacilityValue = (value: string): ErrorsOrValue<number> => {
-  if (!value) {
+  if (!value || !CURRENCY_NUMBER_REGEX.test(value)) {
     return {
-      errors: [
-        {
-          errRef,
-          errMsg: 'Enter the new facility value in number format',
-        },
-      ],
-    };
-  }
-
-  if (!CURRENCY_NUMBER_REGEX.test(value)) {
-    return {
-      errors: [{ errRef, errMsg: 'Enter a valid facility value' }],
+      errors: [{ errRef, errMsg: 'Enter the new facility value in number format' }],
     };
   }
 


Reviews:
user: login: rkontogianni
state: DISMISSED
author_association: CONTRIBUTOR
submitted_at: 2025-03-27T17:53:51Z
user: login: zainUKEF
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-28T10:02:04Z
user: login: zainUKEF
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-28T10:02:22Z
user: login: abhi-markan
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-28T10:53:49Z
user: login: zainUKEF
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-03-28T10:55:22Z
user: login: rkontogianni
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-03-28T13:35:01Z

Review comments:
diff_hunk: @@ -17,39 +17,63 @@ describe('validateFacilityValue', () => {
       value: '1000x',
       expectedError: {
         errRef,
-        errMsg: 'Enter a valid facility value',
+        errMsg: 'Enter the new facility value in number format',
+      },
+    },
+    {
+      description: 'the value contains too many decimal places',
+      value: 'abc',
+      expectedError: {
+        errRef,
+        errMsg: 'Enter the new facility value in number format',
+      },
+    },
+    {
+      description: 'the value contains too many decimal places',
+      value: '!@£',
+      expectedError: {
+        errRef,
+        errMsg: 'Enter the new facility value in number format',
+      },
+    },
+    {
+      description: 'the value contains too many decimal places',
+      value: '1ooo',
+      expectedError: {
+        errRef,
+        errMsg: 'Enter the new facility value in number format',
       },
     },
     {
       description: 'the value contains too many decimal places',
       value: '1000.000',
       expectedError: {
         errRef,
-        errMsg: 'Enter a valid facility value',
+        errMsg: 'Enter the new facility value in number format',
       },
     },
     {
       description: 'the value contains too many decimal points',
       value: '1000.00.0',
       expectedError: {
         errRef,
-        errMsg: 'Enter a valid facility value',
+        errMsg: 'Enter the new facility value in number format',
path: gef-ui/server/controllers/amendments/facility-value/validation.test.ts
user: login: zainUKEF
body: these are being repeated, could they be stored as consts at the top of the file and then errMsg: valueFormatError?
created_at: 2025-03-28T10:02:03Z
author_association: CONTRIBUTOR
diff_hunk: @@ -84,27 +84,42 @@ context('Amendments - Facility value - page tests', () => {
     {
       description: 'the value contains non-numeric characters',
       value: '1000x',
-      expectedErrorMessage: 'Enter a valid facility value',
+      expectedErrorMessage: 'Enter the new facility value in number format',
+    },
+    {
+      description: 'the value contains string characters',
+      value: 'abc',
+      expectedErrorMessage: 'Enter the new facility value in number format',
+    },
+    {
+      description: 'the value contains special characters',
+      value: '!@£',
+      expectedErrorMessage: 'Enter the new facility value in number format',
+    },
+    {
+      description: 'the value contains number and non-numeric characters',
+      value: '1ooo',
+      expectedErrorMessage: 'Enter the new facility value in number format',
     },
     {
       description: 'the value contains too many decimal places',
       value: '1000.000',
-      expectedErrorMessage: 'Enter a valid facility value',
+      expectedErrorMessage: 'Enter the new facility value in number format',
path: e2e-tests/ukef/cypress/e2e/journeys/portal/amendments/pages/facility-value.spec.js
user: login: zainUKEF
body: these are being repeated, could they be stored as consts at the top of the file and then errMsg: valueFormatError?
created_at: 2025-03-28T10:02:22Z
author_association: CONTRIBUTOR
diff_hunk: @@ -84,27 +84,42 @@ context('Amendments - Facility value - page tests', () => {
     {
       description: 'the value contains non-numeric characters',
       value: '1000x',
-      expectedErrorMessage: 'Enter a valid facility value',
+      expectedErrorMessage: 'Enter the new facility value in number format',
+    },
+    {
+      description: 'the value contains string characters',
+      value: 'abc',
+      expectedErrorMessage: 'Enter the new facility value in number format',
+    },
+    {
+      description: 'the value contains special characters',
+      value: '!@£',
+      expectedErrorMessage: 'Enter the new facility value in number format',
+    },
+    {
+      description: 'the value contains number and non-numeric characters',
+      value: '1ooo',
+      expectedErrorMessage: 'Enter the new facility value in number format',
     },
     {
       description: 'the value contains too many decimal places',
       value: '1000.000',
-      expectedErrorMessage: 'Enter a valid facility value',
+      expectedErrorMessage: 'Enter the new facility value in number format',
path: e2e-tests/ukef/cypress/e2e/journeys/portal/amendments/pages/facility-value.spec.js
user: login: abhi-markan
body: Good shout, have refactored.
created_at: 2025-03-28T10:53:37Z
author_association: CONTRIBUTOR
diff_hunk: @@ -17,39 +17,63 @@ describe('validateFacilityValue', () => {
       value: '1000x',
       expectedError: {
         errRef,
-        errMsg: 'Enter a valid facility value',
+        errMsg: 'Enter the new facility value in number format',
+      },
+    },
+    {
+      description: 'the value contains too many decimal places',
+      value: 'abc',
+      expectedError: {
+        errRef,
+        errMsg: 'Enter the new facility value in number format',
+      },
+    },
+    {
+      description: 'the value contains too many decimal places',
+      value: '!@£',
+      expectedError: {
+        errRef,
+        errMsg: 'Enter the new facility value in number format',
+      },
+    },
+    {
+      description: 'the value contains too many decimal places',
+      value: '1ooo',
+      expectedError: {
+        errRef,
+        errMsg: 'Enter the new facility value in number format',
       },
     },
     {
       description: 'the value contains too many decimal places',
       value: '1000.000',
       expectedError: {
         errRef,
-        errMsg: 'Enter a valid facility value',
+        errMsg: 'Enter the new facility value in number format',
       },
     },
     {
       description: 'the value contains too many decimal points',
       value: '1000.00.0',
       expectedError: {
         errRef,
-        errMsg: 'Enter a valid facility value',
+        errMsg: 'Enter the new facility value in number format',
path: gef-ui/server/controllers/amendments/facility-value/validation.test.ts
user: login: abhi-markan
body: Good shout, have refactored.
created_at: 2025-03-28T10:53:42Z
author_association: CONTRIBUTOR

PR comments:
user: login: sonarqubecloud[bot]
created_at: 2025-03-28T10:56:27Z
body: ## [![Quality Gate Failed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-failed-20px.png 'Quality Gate Failed')](https://sonarcloud.io/dashboard?id=UK-Export-Finance_dtfs2&pullRequest=4283) **Quality Gate failed**  
Failed conditions  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/failed-16px.png '') [91.6% Duplication on New Code](https://sonarcloud.io/component_measures?id=UK-Export-Finance_dtfs2&pullRequest=4283&metric=new_duplicated_lines_density&view=list) (required ≤ 40%)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=UK-Export-Finance_dtfs2&pullRequest=4283)


author_association: NONE