fix: evaluate value-dependent items conditionals per array row - #271
Open
vermaxik wants to merge 1 commit into
Open
fix: evaluate value-dependent items conditionals per array row#271vermaxik wants to merge 1 commit into
vermaxik wants to merge 1 commit into
Conversation
calculateFinalSchema pre-applies conditional rules by merging the matching
branch into the schema and deleting it. For array items it evaluated every
rule against an empty object, so the {}-matching branch was permanently
baked into the one shared items schema: rules with an else branch (or a
negated if) were wrong for every row, in both validation and field state.
Only constant conditionals (if: true / if: false) are pre-applied now --
their branch is row-independent, which is what the workaround originally
protected (schema-driven visibility inside items). Value-dependent rules
keep their then/else so validateSchema evaluates them per row against the
row's actual value.
Per-row FIELD mutations (visibility / required flags) remain unsupported
for group-array items: all rows share a single fields array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Conditionals (
if/then/elseinallOf) inside an array'sitemsschema areevaluated against an empty object instead of each row's value. Whichever branch
matches
{}gets permanently merged into the shareditemsschema and deleted,so rules with an
elsebranch (or a negatedif) validate wrongly for everyrow. This PR pre-applies only constant conditionals inside
itemsand leavesvalue-dependent ones intact, so
validateSchemaevaluates them per row.Json Schema
{ "title": "Per-row conditionals in nested arrays", "type": "object", "properties": { "discount_rules": { "type": "array", "title": "Discount rules", "x-jsf-presentation": { "inputType": "group-array", "addFieldText": "Add rule" }, "items": { "type": "object", "title": "Rule", "x-jsf-order": ["name", "mode", "percent", "tiering"], "required": ["name", "mode"], "x-jsf-presentation": { "inputType": "fieldset" }, "properties": { "name": { "type": "string", "title": "Name", "x-jsf-presentation": { "inputType": "text" } }, "mode": { "type": "string", "title": "Mode", "default": "flat", "oneOf": [ { "const": "flat", "title": "Flat" }, { "const": "tiered", "title": "Tiered" } ], "x-jsf-presentation": { "inputType": "radio" } }, "percent": { "type": ["string", "null"], "title": "Percent", "x-jsf-presentation": { "inputType": "text" } }, "tiering": { "type": ["object", "null"], "title": "Tiering", "x-jsf-order": ["period", "tiers"], "x-jsf-presentation": { "inputType": "fieldset" }, "properties": { "period": { "type": "string", "title": "Period", "oneOf": [ { "const": "monthly", "title": "Monthly" }, { "const": "yearly", "title": "Yearly" } ], "x-jsf-presentation": { "inputType": "select" } }, "tiers": { "type": "array", "title": "Tiers", "x-jsf-presentation": { "inputType": "group-array", "addFieldText": "Add tier" }, "items": { "type": "object", "title": "Tier", "x-jsf-order": ["up_to", "percent"], "required": ["percent"], "x-jsf-presentation": { "inputType": "fieldset" }, "properties": { "up_to": { "type": "integer", "title": "Up to", "x-jsf-presentation": { "inputType": "number" } }, "percent": { "type": "string", "title": "Percent", "x-jsf-presentation": { "inputType": "text" } } } } } } } }, "allOf": [ { "if": { "properties": { "mode": { "const": "tiered" } }, "required": ["mode"] }, "then": { "required": ["tiering"], "properties": { "tiering": { "type": "object", "required": ["period", "tiers"], "properties": { "tiers": { "minItems": 1, "x-jsf-errorMessage": { "minItems": "Add at least one tier, or switch the mode to flat." } } } }, "percent": { "maxLength": 0, "x-jsf-errorMessage": { "maxLength": "Not used for tiered rules. Clear it or switch the mode to flat." } } } }, "else": { "required": ["percent"], "properties": { "percent": { "type": "string" }, "tiering": { "properties": { "tiers": { "maxItems": 0, "x-jsf-errorMessage": { "maxItems": "Tiers only apply to tiered rules. Remove them or switch the mode." } } } } } } } ] } } } }Changes Made
calculateFinalSchemapre-applies conditional rules by merging the matchingbranch into the schema and deleting the branch. That is correct at the root,
where
valuesis the real form value, but foritemsthe existing workaroundin
applySchemaRulespassed a hardcoded{}:elsebranch had theelsebaked in for all rows (anifthat requires a field never matches
{}), producing wrong validation evenfor rows where the condition is true;
ifalways matches{}, baking thethenin for all rows;evaluation in
validateCondition(which is correct) never saw it.The change threads a
constantIfsOnlyflag throughapplySchemaRules. Insideitems, only conditionals whoseifis a boolean (if: true/if: false)are pre-applied — their branch is row-independent, which is what the original
workaround supported (schema-driven visibility inside items, covered by the
existing "with constant logic" tests). Value-dependent rules keep their
then/elseand are evaluated per item with the row's actual value.Known limitation, unchanged by this PR: per-row FIELD state (visibility,
required flags, titles) is still not representable, since all rows of a
group-array share a single fields array. This PR fixes validation only; the
describe.skip('with logic based on answers')for group-array field visibilitystays skipped.