Skip to content

Commit 8ccc8ef

Browse files
committed
Fix conditional parameter validation
- Determine effective test parameter value considering defaults - Find matching 'when' clause including default_when - Validate only parameters from the matching 'when' branch - Ensure parameters from other branches are not present - Handle missing test parameter values with defaults properly Fixed 4 test cases for gx_conditional_boolean, gx_conditional_boolean_checked, and gx_conditional_select.
1 parent b29bc5b commit 8ccc8ef

File tree

1 file changed

+64
-15
lines changed

1 file changed

+64
-15
lines changed

client/src/components/Tool/structured.ts

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -694,27 +694,76 @@ function validateParameter(inputKey: string, inputValue: any, parameterModel: To
694694
}
695695
} else if (isGxConditional(parameterModel)) {
696696
const testParameter = parameterModel.test_parameter;
697-
let testParameterEffectivelyOptional = testParameter.optional;
698-
if (!testParameterEffectivelyOptional && "value" in testParameter && testParameter.value !== null) {
699-
testParameterEffectivelyOptional = true;
700-
}
701697
const whens = parameterModel.whens;
702698
const testParameterName = testParameter.name;
703-
const testParameterValue = inputValue[testParameterName];
704-
// validateParameter(testParameterName, testParameterValue, testParameter);
705-
let testParameterValueFoundInWhen = false;
699+
let testParameterValue = inputValue[testParameterName];
700+
701+
// Determine effective test parameter value (use default if missing)
702+
if (testParameterValue === undefined || testParameterValue === null) {
703+
// Use default value from test parameter if available
704+
if ("value" in testParameter && testParameter.value !== null && testParameter.value !== undefined) {
705+
testParameterValue = testParameter.value;
706+
}
707+
}
708+
709+
// Find matching "when" clause
710+
let matchingWhen = null;
711+
if (testParameterValue === null || testParameterValue === undefined) {
712+
// Look for default_when
713+
for (const when of whens) {
714+
if (when.is_default_when) {
715+
matchingWhen = when;
716+
break;
717+
}
718+
}
719+
} else {
720+
// Look for matching discriminator
721+
for (const when of whens) {
722+
if (when.discriminator === testParameterValue) {
723+
matchingWhen = when;
724+
break;
725+
}
726+
}
727+
// If no match found and test parameter has default, try default_when
728+
if (!matchingWhen && "value" in testParameter && testParameter.value !== null) {
706729
for (const when of whens) {
707-
const inputKey = when.discriminator;
708-
if (inputKey === testParameterValue) {
709-
testParameterValueFoundInWhen = true;
710-
const whenParameters = when.parameters.concat([testParameter]);
730+
if (when.is_default_when) {
731+
matchingWhen = when;
732+
break;
733+
}
734+
}
735+
}
736+
}
737+
738+
if (matchingWhen) {
739+
// Validate only parameters from the matching "when" branch
740+
const whenParameters = matchingWhen.parameters.concat([testParameter]);
711741
const whenResults = validateParameters(inputValue, whenParameters);
712742
extendValidationResults(results, whenResults);
713-
break;
743+
744+
// Check for parameters from other branches that shouldn't be present
745+
const allowedParameterNames = new Set<string>();
746+
whenParameters.forEach(p => allowedParameterNames.add(p.name));
747+
for (const when of whens) {
748+
if (when !== matchingWhen) {
749+
for (const param of when.parameters) {
750+
if (param.name in inputValue && !allowedParameterNames.has(param.name)) {
751+
results.push(`Parameter ${param.name} is not valid for the selected conditional branch.`);
752+
}
753+
}
754+
}
755+
}
756+
} else {
757+
// No matching when clause found
758+
if (testParameterValue === null || testParameterValue === undefined) {
759+
// Check if test parameter is optional or has default
760+
const hasDefault = "value" in testParameter && testParameter.value !== null && testParameter.value !== undefined;
761+
if (!testParameter.optional && !hasDefault) {
762+
results.push(`Non optional conditional test parameter ${testParameterName} was not found in inputs.`);
763+
}
764+
} else {
765+
results.push(`Invalid conditional test value (${testParameterValue}) for parameter (${testParameterName}).`);
714766
}
715-
}
716-
if (!testParameterValueFoundInWhen && !testParameterEffectivelyOptional) {
717-
results.push(`Non optional conditional test parameter ${testParameterName} was not found in inputs.`);
718767
}
719768
} else if (isGxSection(parameterModel)) {
720769
// Section parameters contain nested child parameters

0 commit comments

Comments
 (0)