Skip to content

Commit

Permalink
Merge pull request #102 from formio/task/fix-validate-required-tests
Browse files Browse the repository at this point in the history
update validate required recursion to not recurse when nested data type
  • Loading branch information
lane-formio committed Jul 1, 2024
1 parent 6b0cb6b commit 3a48ba7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
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

0 comments on commit 3a48ba7

Please sign in to comment.