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

update validate required recursion to not recurse when nested data type #102

Merged
merged 3 commits into from
Jun 4, 2024
Merged
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 @@ -254,6 +254,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
9 changes: 5 additions & 4 deletions src/process/validation/rules/validateRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DayComponent
} from 'types';
import { isEmptyObject } from '../util';
import { isComponentNestedDataType } from 'utils/formUtil';
import { ProcessorInfo } from 'types/process/ProcessorInfo';

const isAddressComponent = (component: any): component is AddressComponent => {
Expand All @@ -28,7 +29,7 @@ const isComponentThatCannotHaveFalseValue = (component: any): boolean => {
return component.type === 'checkbox' || component.type === 'selectboxes'
}

const valueIsPresent = (value: any, considerFalseTruthy: boolean): boolean => {
const valueIsPresent = (value: any, considerFalseTruthy: boolean, isNestedDatatype?: boolean): boolean => {
// Evaluate for 3 out of 6 falsy values ("", null, undefined), don't check for 0
// and only check for false under certain conditions
if (value === null || value === undefined || value === "" || (!considerFalseTruthy && value === false)) {
Expand All @@ -43,7 +44,7 @@ const valueIsPresent = (value: any, considerFalseTruthy: boolean): boolean => {
return false;
}
// Recursively evaluate
else if (typeof value === 'object') {
else if (typeof value === 'object' && !isNestedDatatype) {
return Object.values(value).some((val) => valueIsPresent(val, considerFalseTruthy));
}
return true;
Expand Down Expand Up @@ -74,9 +75,9 @@ export const validateRequiredSync: RuleFnSync = (context: ValidationContext) =>
return error;
}
else if (isComponentThatCannotHaveFalseValue(component)) {
return !valueIsPresent(value, false) ? error : null;
return !valueIsPresent(value, false, isComponentNestedDataType(component)) ? error : null;
}
return !valueIsPresent(value, true) ? error : null;
return !valueIsPresent(value, true, isComponentNestedDataType(component)) ? error : null;
};

export const validateRequiredInfo: ProcessorInfo<ValidationContext, FieldError | null> = {
Expand Down
Loading