Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing truncate multiple spaces so it does not mutate the data in the… #82

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
50 changes: 0 additions & 50 deletions src/process/validation/rules/__tests__/validateRequired.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' };
Expand Down
2 changes: 1 addition & 1 deletion src/process/validation/rules/validateRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading