From 8c609ab0e23c58aa80055a233f2d5120c2a69deb Mon Sep 17 00:00:00 2001 From: brendanjbond Date: Wed, 13 Nov 2024 16:29:35 -0600 Subject: [PATCH] fix issue where validateWhenHidden was not validating for intentionally hidden components; add tests --- src/process/validation/index.ts | 22 +++++-------------- .../validation/rules/validateRequired.ts | 10 ++------- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/src/process/validation/index.ts b/src/process/validation/index.ts index 187e502b..98feed93 100644 --- a/src/process/validation/index.ts +++ b/src/process/validation/index.ts @@ -1,5 +1,4 @@ import { - ConditionsScope, ProcessorFn, ProcessorFnSync, ProcessorInfo, @@ -93,7 +92,10 @@ export function isForcedHidden( isConditionallyHidden: ConditionallyHidden, ): boolean { const { component } = context; - if (isConditionallyHidden(context as ConditionsContext)) { + if ( + component.ephemeralState?.conditionallyHidden || + isConditionallyHidden(context as ConditionsContext) + ) { return true; } if (component.hasOwnProperty('hidden')) { @@ -106,20 +108,8 @@ export const _shouldSkipValidation = ( context: ValidationContext, isConditionallyHidden: ConditionallyHidden, ) => { - const { component, scope, path } = context; - const absolutePath = getComponentAbsolutePath(component) || path; + const { component } = context; - if ( - (scope as ConditionsScope)?.conditionals && - (find((scope as ConditionsScope).conditionals, { - path: absolutePath, - conditionallyHidden: true, - }) || - component.ephemeralState?.conditionallyHidden === true) - ) { - return true; - } - const { validateWhenHidden = false } = component || {}; const rules = [ // Skip validation if component is readOnly // () => this.options.readOnly, @@ -128,7 +118,7 @@ export const _shouldSkipValidation = ( // Check to see if we are editing and if so, check component persistence. () => isValueHidden(context), // Force valid if component is hidden. - () => isForcedHidden(context, isConditionallyHidden) && !validateWhenHidden, + () => !component.validateWhenHidden && isForcedHidden(context, isConditionallyHidden), ]; return rules.some((pred) => pred()); diff --git a/src/process/validation/rules/validateRequired.ts b/src/process/validation/rules/validateRequired.ts index 746ab7e2..4c6f94d1 100644 --- a/src/process/validation/rules/validateRequired.ts +++ b/src/process/validation/rules/validateRequired.ts @@ -71,15 +71,9 @@ const valueIsPresent = ( return true; }; -export const shouldValidate = (context: ValidationContext) => { +export const shouldValidate = (context: ValidationContext): boolean => { const { component } = context; - if ( - component.validate?.required && - !(component.hidden || component.ephemeralState?.conditionallyHidden) - ) { - return true; - } - return false; + return !!component.validate?.required; }; export const validateRequired: RuleFn = async (context: ValidationContext) => {