Skip to content

Commit 966cec5

Browse files
denatyssandrina-p
andauthored
fix(conditionals): Add support to oneOf (#136)
* Add support OR for conditions * Release 0.11.11-dev.20250220164730 * Fix tests * Fix tests * Fix tests * Fix tests * Revert back to 0.11.10-beta.0 --------- Co-authored-by: Sandrina Pereira <[email protected]>
1 parent 96f3ae5 commit 966cec5

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/internals/checkIfConditionMatches.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ export function checkIfConditionMatchesProperties(node, formValues, formFields,
1212
return node.if;
1313
}
1414

15+
if (node.if.anyOf) {
16+
return node.if.anyOf.some((property) =>
17+
checkIfConditionMatchesProperties({ if: property }, formValues, formFields, logic)
18+
);
19+
}
20+
1521
return Object.keys(node.if.properties ?? {}).every((name) => {
1622
const currentProperty = node.if.properties[name];
1723
const value = formValues[name];

src/tests/conditions.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,54 @@ describe('Conditional with literal booleans', () => {
548548
});
549549
});
550550

551+
describe('Conditional with anyOf', () => {
552+
const schema = {
553+
additionalProperties: false,
554+
type: 'object',
555+
properties: {
556+
field_a: { type: 'string' },
557+
field_b: { type: 'string' },
558+
field_c: { type: 'string' },
559+
},
560+
allOf: [
561+
{
562+
if: {
563+
anyOf: [
564+
{ properties: { field_a: { const: '1' } }, required: ['field_a'] },
565+
{ properties: { field_b: { const: '2' } }, required: ['field_b'] },
566+
],
567+
},
568+
then: {
569+
required: ['field_c'],
570+
},
571+
else: {
572+
properties: {
573+
field_c: false,
574+
},
575+
},
576+
},
577+
],
578+
};
579+
580+
it('handles true case', () => {
581+
const { fields, handleValidation } = createHeadlessForm(schema, { strictInputType: false });
582+
583+
expect(fields[2].isVisible).toBe(false);
584+
expect(handleValidation({ field_a: 'x', field_b: '2' }).formErrors).toEqual({
585+
field_c: 'Required field',
586+
});
587+
expect(fields[2].isVisible).toBe(true);
588+
});
589+
590+
it('handles false case', () => {
591+
const { fields, handleValidation } = createHeadlessForm(schema, { strictInputType: false });
592+
593+
expect(fields[2].isVisible).toBe(false);
594+
expect(handleValidation({ field_a: 'x', field_b: 'x' }).formErrors).toBeUndefined();
595+
expect(fields[2].isVisible).toBe(false);
596+
});
597+
});
598+
551599
describe('Conditionals - bugs and code-smells', () => {
552600
// Why do we have these bugs?
553601
// To be honest we never realized it much later later.

0 commit comments

Comments
 (0)