Skip to content

Commit 13618d4

Browse files
Vojtěch Václav Portešvojtechportes
authored andcommitted
fix: Fixed empty rule validation
1 parent 7e47417 commit 13618d4

4 files changed

Lines changed: 66 additions & 1 deletion

File tree

src/builder.test.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,48 @@ describe('#components/Builder', () => {
314314
});
315315
expect(wrapper.text()).toContain('This value is required');
316316
});
317+
318+
it('Does not render field validation errors for placeholder rules without a selected field', async () => {
319+
const wrapper = mount(
320+
<Builder
321+
fields={fields}
322+
data={[
323+
{
324+
type: 'GROUP',
325+
value: 'AND',
326+
isNegated: false,
327+
children: [{ field: '' }],
328+
},
329+
]}
330+
showValidation
331+
/>
332+
);
333+
334+
await Promise.resolve();
335+
wrapper.update();
336+
337+
expect(wrapper.text()).not.toContain('Field "" is not defined');
338+
});
339+
340+
it('Does not render field validation errors for placeholder rules with missing field values', async () => {
341+
const wrapper = mount(
342+
<Builder
343+
fields={fields}
344+
data={[
345+
{
346+
type: 'GROUP',
347+
value: 'AND',
348+
isNegated: false,
349+
children: [{} as never],
350+
},
351+
]}
352+
showValidation
353+
/>
354+
);
355+
356+
await Promise.resolve();
357+
wrapper.update();
358+
359+
expect(wrapper.text()).not.toContain('is not defined');
360+
});
317361
});

src/rule/rule.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const Rule: FC<IRuleProps> = ({
9191
return null;
9292
}
9393

94-
if (fieldRef === '') {
94+
if (typeof fieldRef !== 'string' || fieldRef.trim() === '') {
9595
return (
9696
<RuleContainer
9797
dragHandle={dragHandle}

src/utils/validate-builder-query.util.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ describe('validateBuilderQuery', () => {
9494
expect(result.issues).toHaveLength(0);
9595
});
9696

97+
it('Returns no issue for placeholder rules without a selected field', async () => {
98+
const result = await validateBuilderQuery(
99+
[
100+
{
101+
type: 'GROUP',
102+
value: 'AND',
103+
isNegated: false,
104+
children: [{ id: 'rule-empty', field: '' }],
105+
},
106+
],
107+
context
108+
);
109+
110+
expect(result.isValid).toEqual(true);
111+
expect(result.issuesByRuleId['rule-empty']).toBeUndefined();
112+
});
113+
97114
it('Returns a range issue when numeric range order is invalid', async () => {
98115
const result = await validateBuilderQuery(
99116
[

src/utils/validation/validate-builder-query.util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export const validateBuilderQuery = (
2929
const pendingIssueGroups: Array<Promise<IBuilderValidationIssue[]>> = [];
3030

3131
for (const rule of rules) {
32+
if (typeof rule.field !== 'string' || rule.field.trim() === '') {
33+
continue;
34+
}
35+
3236
const field = context.fields.find((fieldItem) => fieldItem.field === rule.field);
3337

3438
if (!field) {

0 commit comments

Comments
 (0)