Skip to content

Commit

Permalink
Fixing truncate multiple spaces so it does not mutate the data in the…
Browse files Browse the repository at this point in the history
… validation system.
  • Loading branch information
travist committed Apr 17, 2024
1 parent b9ebc0a commit 5033358
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/process/normalize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
7 changes: 4 additions & 3 deletions src/process/validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5033358

Please sign in to comment.