Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fieldset): Fix handleValidation for undefined fieldset #144

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@remoteoss/json-schema-form",
"version": "0.11.11-beta.0",
"version": "0.11.12-dev.20250310202108",
"description": "Headless UI form powered by JSON Schemas",
"author": "Remote.com <engineering@remote.com> (https://remote.com/)",
"license": "MIT",
Expand Down
9 changes: 6 additions & 3 deletions src/internals/checkIfConditionMatches.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { compareFormValueWithSchemaValue, getField, validateFieldSchema } from '../helpers';
import { hasProperty } from '../utils';

import { supportedTypes } from './fields';

/**
* Checks if a "IF" condition matches given the current form state
* @param {Object} node - JSON schema node
Expand All @@ -20,7 +22,10 @@ export function checkIfConditionMatchesProperties(node, formValues, formFields,

return Object.keys(node.if.properties ?? {}).every((name) => {
const currentProperty = node.if.properties[name];
const value = formValues[name];
const field = getField(name, formFields ?? []);
const isFieldsetField = field?.inputType === supportedTypes.FIELDSET;
const value = formValues?.[name] ?? (isFieldsetField ? {} : undefined);

const hasEmptyValue =
typeof value === 'undefined' ||
// NOTE: This is a "Remote API" dependency, as empty fields are sent as "null".
Expand Down Expand Up @@ -65,8 +70,6 @@ export function checkIfConditionMatchesProperties(node, formValues, formFields,
);
}

const field = getField(name, formFields);

return validateFieldSchema(
{
...field,
Expand Down
68 changes: 68 additions & 0 deletions src/tests/conditions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,74 @@ describe('Conditional with anyOf', () => {
});
});

describe('Conditional with fieldset', () => {
const schema = {
additionalProperties: false,
type: 'object',
properties: {
field_a: {
type: 'object',
properties: {
min: { type: 'number' },
max: { type: 'number' },
},
},
field_b: { type: 'string' },
},
allOf: [
{
if: {
properties: {
field_a: {
properties: {
min: {
minimum: 10,
},
},
required: ['min'],
},
},
required: ['field_a'],
},
then: {
required: ['field_b'],
},
else: {
properties: {
field_b: false,
},
},
},
],
};

it('handles true case', () => {
const { fields, handleValidation } = createHeadlessForm(schema, { strictInputType: false });

expect(fields[1].isVisible).toBe(false);
expect(handleValidation({ field_a: { min: 100 } }).formErrors).toEqual({
field_b: 'Required field',
});
expect(fields[1].isVisible).toBe(true);
});

it('handles false case', () => {
const { fields, handleValidation } = createHeadlessForm(schema, { strictInputType: false });

expect(fields[1].isVisible).toBe(false);
expect(handleValidation({ field_a: { min: 1 } }).formErrors).toBeUndefined();
expect(fields[1].isVisible).toBe(false);
});

it('handles undefined fieldset case', () => {
const { fields, handleValidation } = createHeadlessForm(schema, { strictInputType: false });

expect(fields[1].isVisible).toBe(false);
expect(handleValidation({}).formErrors).toBeUndefined();
expect(fields[1].isVisible).toBe(false);
});
});

describe('Conditionals - bugs and code-smells', () => {
// Why do we have these bugs?
// To be honest we never realized it much later later.
Expand Down
Loading