Skip to content

Commit

Permalink
fix issue where validateWhenHidden was not validating for intentional…
Browse files Browse the repository at this point in the history
…ly hidden components; add tests (#191)
  • Loading branch information
brendanbond authored Nov 18, 2024
1 parent 555beb3 commit 770d1df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
22 changes: 6 additions & 16 deletions src/process/validation/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
ConditionsScope,
ProcessorFn,
ProcessorFnSync,
ProcessorInfo,
Expand Down Expand Up @@ -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')) {
Expand All @@ -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,
Expand All @@ -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());
Expand Down
10 changes: 2 additions & 8 deletions src/process/validation/rules/validateRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 770d1df

Please sign in to comment.