From 82c4e573793c71e5d8fcc91e50d2037131a765e8 Mon Sep 17 00:00:00 2001 From: JamalAlabdullah Date: Mon, 5 Aug 2024 12:16:25 +0200 Subject: [PATCH 1/2] Added validation for the number of characters in the name of the receipt --- frontend/language/src/nb.json | 1 + frontend/packages/shared/src/utils/layoutSetsUtils.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/frontend/language/src/nb.json b/frontend/language/src/nb.json index 97c61f86448..69a827ed6ac 100644 --- a/frontend/language/src/nb.json +++ b/frontend/language/src/nb.json @@ -613,6 +613,7 @@ "process_editor.configuration_panel_custom_receipt_heading": "Opprett din egen kvittering", "process_editor.configuration_panel_custom_receipt_info": "Hvis du heller vil lage din egen kvittering, kan du opprette den her. Kvitteringen du lager selv vil overstyre standardkvitteringen.", "process_editor.configuration_panel_custom_receipt_layout_set_name": "Navn på sidegruppe: ", + "process_editor.configuration_panel_custom_receipt_layout_set_name_validation": "Navnet må ha minst 2 tegn", "process_editor.configuration_panel_custom_receipt_navigate_to_lage_button": "Gå til Lage", "process_editor.configuration_panel_custom_receipt_navigate_to_lage_title": "Gå til Lage for å utforme kvitteringen din", "process_editor.configuration_panel_custom_receipt_select_data_model_label": "Datamodellknytning", diff --git a/frontend/packages/shared/src/utils/layoutSetsUtils.ts b/frontend/packages/shared/src/utils/layoutSetsUtils.ts index eece4a95710..ee77fcc88fa 100644 --- a/frontend/packages/shared/src/utils/layoutSetsUtils.ts +++ b/frontend/packages/shared/src/utils/layoutSetsUtils.ts @@ -15,6 +15,8 @@ export const getLayoutSetIdValidationErrorKey = ( if (oldLayoutSetId === newLayoutSetId) return null; if (!validateLayoutNameAndLayoutSetName(newLayoutSetId)) return 'ux_editor.pages_error_format'; if (!newLayoutSetId) return 'validation_errors.required'; + if (newLayoutSetId.length === 1) + return 'process_editor.configuration_panel_custom_receipt_layout_set_name_validation'; if (layoutSets.sets.some((set) => set.id === newLayoutSetId)) return 'process_editor.configuration_panel_layout_set_id_not_unique'; return null; From 9f52fcf19af130a55334512adf269d619f78be7a Mon Sep 17 00:00:00 2001 From: JamalAlabdullah Date: Fri, 16 Aug 2024 11:38:05 +0200 Subject: [PATCH 2/2] added validation for whitespace, and added test --- .../shared/src/utils/layoutSetsUtils.test.ts | 19 ++++++++++++++++++- .../shared/src/utils/layoutSetsUtils.ts | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/frontend/packages/shared/src/utils/layoutSetsUtils.test.ts b/frontend/packages/shared/src/utils/layoutSetsUtils.test.ts index 4dd9a2d1bb8..770bc9acf62 100644 --- a/frontend/packages/shared/src/utils/layoutSetsUtils.test.ts +++ b/frontend/packages/shared/src/utils/layoutSetsUtils.test.ts @@ -1,4 +1,7 @@ -import { getLayoutSetNameForCustomReceipt } from 'app-shared/utils/layoutSetsUtils'; +import { + getLayoutSetIdValidationErrorKey, + getLayoutSetNameForCustomReceipt, +} from 'app-shared/utils/layoutSetsUtils'; import type { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; // Test data @@ -44,6 +47,20 @@ describe('getLayoutSetNameForCustomReceipt', () => { expect(getLayoutSetNameForCustomReceipt(layoutSetsWithEmptySets)).toBeUndefined(); }); + it('should return error message when the user types just one character', () => { + const newLayoutSetId = 'a'; + expect(getLayoutSetIdValidationErrorKey({ sets: [] }, layoutSetName, newLayoutSetId)).toBe( + 'process_editor.configuration_panel_custom_receipt_layout_set_name_validation', + ); + }); + + it('should return error message when the user types whitespace', () => { + const newLayoutSetId = ' '; + expect(getLayoutSetIdValidationErrorKey({ sets: [] }, layoutSetName, newLayoutSetId)).toBe( + 'validation_errors.required', + ); + }); + it('should return undefined if layoutSets has a set with no task ids', () => { const layoutSetsWithUndefinedTasks: LayoutSets = { sets: [ diff --git a/frontend/packages/shared/src/utils/layoutSetsUtils.ts b/frontend/packages/shared/src/utils/layoutSetsUtils.ts index ee77fcc88fa..e29d5a28f4f 100644 --- a/frontend/packages/shared/src/utils/layoutSetsUtils.ts +++ b/frontend/packages/shared/src/utils/layoutSetsUtils.ts @@ -13,8 +13,8 @@ export const getLayoutSetIdValidationErrorKey = ( newLayoutSetId: string, ): string => { if (oldLayoutSetId === newLayoutSetId) return null; + if (!newLayoutSetId || newLayoutSetId.trim() === '') return 'validation_errors.required'; if (!validateLayoutNameAndLayoutSetName(newLayoutSetId)) return 'ux_editor.pages_error_format'; - if (!newLayoutSetId) return 'validation_errors.required'; if (newLayoutSetId.length === 1) return 'process_editor.configuration_panel_custom_receipt_layout_set_name_validation'; if (layoutSets.sets.some((set) => set.id === newLayoutSetId))