Skip to content

Commit ebdd77c

Browse files
committed
Fix handleValidation for undefined fieldsets
1 parent 07434e3 commit ebdd77c

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/internals/checkIfConditionMatches.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function checkIfConditionMatchesProperties(node, formValues, formFields,
2020

2121
return Object.keys(node.if.properties ?? {}).every((name) => {
2222
const currentProperty = node.if.properties[name];
23-
const value = formValues[name];
23+
const value = formValues ? formValues[name] : {};
2424
const hasEmptyValue =
2525
typeof value === 'undefined' ||
2626
// NOTE: This is a "Remote API" dependency, as empty fields are sent as "null".

src/tests/conditions.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,74 @@ describe('Conditional with anyOf', () => {
596596
});
597597
});
598598

599+
describe('Conditional with fieldsets', () => {
600+
const schema = {
601+
additionalProperties: false,
602+
type: 'object',
603+
properties: {
604+
field_a: {
605+
type: 'object',
606+
properties: {
607+
min: { type: 'number' },
608+
max: { type: 'number' },
609+
},
610+
},
611+
field_b: { type: 'string' },
612+
},
613+
allOf: [
614+
{
615+
if: {
616+
properties: {
617+
field_a: {
618+
properties: {
619+
min: {
620+
minimum: 10,
621+
},
622+
},
623+
required: ['min'],
624+
},
625+
},
626+
required: ['field_a'],
627+
},
628+
then: {
629+
required: ['field_b'],
630+
},
631+
else: {
632+
properties: {
633+
field_b: false,
634+
},
635+
},
636+
},
637+
],
638+
};
639+
640+
it('handles true case', () => {
641+
const { fields, handleValidation } = createHeadlessForm(schema, { strictInputType: false });
642+
643+
expect(fields[1].isVisible).toBe(false);
644+
expect(handleValidation({ field_a: { min: 100 } }).formErrors).toEqual({
645+
field_b: 'Required field',
646+
});
647+
expect(fields[1].isVisible).toBe(true);
648+
});
649+
650+
it('handles false case', () => {
651+
const { fields, handleValidation } = createHeadlessForm(schema, { strictInputType: false });
652+
653+
expect(fields[1].isVisible).toBe(false);
654+
expect(handleValidation({ field_a: { min: 1 } }).formErrors).toBeUndefined();
655+
expect(fields[1].isVisible).toBe(false);
656+
});
657+
658+
it('handles undefined fieldset case', () => {
659+
const { fields, handleValidation } = createHeadlessForm(schema, { strictInputType: false });
660+
661+
expect(fields[1].isVisible).toBe(false);
662+
expect(handleValidation({}).formErrors).toBeUndefined();
663+
expect(fields[1].isVisible).toBe(false);
664+
});
665+
});
666+
599667
describe('Conditionals - bugs and code-smells', () => {
600668
// Why do we have these bugs?
601669
// To be honest we never realized it much later later.

0 commit comments

Comments
 (0)