From 8a7804b4aec1ebb4f93b1ce3c4dea722319d5566 Mon Sep 17 00:00:00 2001 From: Travis Tidwell Date: Tue, 23 Apr 2024 11:37:15 -0500 Subject: [PATCH] Fixing the 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 {