From 50333583f47d51b64103b4c2625dd2f4a0f485a8 Mon Sep 17 00:00:00 2001 From: Travis Tidwell Date: Wed, 17 Apr 2024 12:19:01 -0500 Subject: [PATCH 1/2] Fixing truncate multiple spaces so it does not mutate the data in the validation system. --- src/process/normalize/index.ts | 4 ++++ src/process/validation/index.ts | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/process/normalize/index.ts b/src/process/normalize/index.ts index 10abc50b..8cc6127e 100644 --- a/src/process/normalize/index.ts +++ b/src/process/normalize/index.ts @@ -252,6 +252,10 @@ const normalizeTextFieldComponentValue = ( value: any, path: string ) => { + // If the component has truncate multiple spaces enabled, then normalize the value to remove extra spaces. + if (component.truncateMultipleSpaces && typeof value === 'string') { + value = value.trim().replace(/\s{2,}/g, ' '); + } if (component.allowMultipleMasks && component.inputMasks && component.inputMasks.length > 0) { if (Array.isArray(value)) { return value.map((val) => normalizeMaskValue(component, defaultValues, val, path)); diff --git a/src/process/validation/index.ts b/src/process/validation/index.ts index 2d8387cc..6d6c7e13 100644 --- a/src/process/validation/index.ts +++ b/src/process/validation/index.ts @@ -165,7 +165,8 @@ function handleError(error: FieldError | null, context: ValidationContext) { } export const validateProcess: ValidationProcessorFn = async (context) => { - const { component, data, row, path, instance, scope, rules, skipValidation, value } = context; + const { component, data, row, path, instance, scope, rules, skipValidation } = context; + let { value } = context; if (!scope.validated) scope.validated = []; if (!scope.errors) scope.errors = []; if (!rules || !rules.length) { @@ -216,7 +217,7 @@ export const validateProcess: ValidationProcessorFn = async (context) => { return; } if (component.truncateMultipleSpaces && value && typeof value === 'string') { - set(data, path, value.trim().replace(/\s{2,}/g, ' ')); + value = value.trim().replace(/\s{2,}/g, ' '); } for (const rule of rulesToExecute) { try { @@ -279,7 +280,7 @@ export const validateProcessSync: ValidationProcessorFnSync = (context) => { return; } if (component.truncateMultipleSpaces && value && typeof value === 'string') { - set(data, path, value.trim().replace(/\s{2,}/g, ' ')); + value = value.trim().replace(/\s{2,}/g, ' '); } for (const rule of rulesToExecute) { try { From bc09287e606c3f55fb5c757d4acd257723f6604e Mon Sep 17 00:00:00 2001 From: Travis Tidwell Date: Thu, 18 Apr 2024 09:28:15 -0500 Subject: [PATCH 2/2] Revert "Fixed required validation considering false value falsy" This reverts commit a100e980ce37fadbe4515f95eb424dcaa618708f. --- .../rules/__tests__/validateRequired.test.ts | 50 ------------------- .../validation/rules/validateRequired.ts | 2 +- 2 files changed, 1 insertion(+), 51 deletions(-) diff --git a/src/process/validation/rules/__tests__/validateRequired.test.ts b/src/process/validation/rules/__tests__/validateRequired.test.ts index 13827a1a..830a129c 100644 --- a/src/process/validation/rules/__tests__/validateRequired.test.ts +++ b/src/process/validation/rules/__tests__/validateRequired.test.ts @@ -26,56 +26,6 @@ it('Validating a simple component that is required and present in the data will expect(result).to.equal(null); }); - -it('Validating a simple radio component that is required and present in the data with value set to false will return null', async () => { - const component = { ...simpleRadioField, validate: { required: true }, values: [ - { - label: 'Yes', - value: 'true', - }, - { - label: 'No', - value: 'false', - }] }; - const data = { component: false }; - const context = generateProcessorContext(component, data); - const result = await validateRequired(context); - expect(result).to.equal(null); -}); - - -it('Validating a simple selectbox that is required and present in the data with value set to zero will return null', async () => { - const component = { ...simpleSelectBoxes, validate: { required: true }, values: [ - { - label: 'true', - value: 'true', - }, - { - label: 'Null', - value: '0', - }] }; - const data = { component: 0 }; - const context = generateProcessorContext(component, data); - const result = await validateRequired(context); - expect(result).to.equal(null); -}); - -it('Validating a simple selectbox that is required and present in the data with value set to false will return null', async () => { - const component = { ...simpleSelectBoxes, validate: { required: true }, values: [ - { - label: 'true', - value: 'true', - }, - { - label: 'false', - value: 'false', - }] }; - const data = { component: false }; - const context = generateProcessorContext(component, data); - const result = await validateRequired(context); - expect(result).to.equal(null); -}); - it('Validating a simple component that is not required and present in the data will return null', async () => { const component = simpleTextField; const data = { component: 'a simple value' }; diff --git a/src/process/validation/rules/validateRequired.ts b/src/process/validation/rules/validateRequired.ts index bc80fad9..565d2219 100644 --- a/src/process/validation/rules/validateRequired.ts +++ b/src/process/validation/rules/validateRequired.ts @@ -26,7 +26,7 @@ export const validateRequiredSync: RuleFnSync = (context: ValidationContext) => return null; } if ( - (value === null || value === undefined || isEmptyObject(value) || (!!value === false && value !== 0 && value !== false )) && + (value === null || value === undefined || isEmptyObject(value) || (!!value === false && value !== 0)) && !component.hidden ) { return error;