Skip to content

Commit 3a6753a

Browse files
Merge pull request #65 from formio/FIO-8101
FIO-8101: always process json validation even if value is falsy
2 parents 14542d5 + cfc72b4 commit 3a6753a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/process/validation/rules/__tests__/validateJson.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,32 @@ it('A simple component with JSON logic evaluation will return null if the JSON l
7171
const result = await validateJson(context);
7272
expect(result).to.equal(null);
7373
});
74+
75+
it('A simple component with JSON logic evaluation will validate even if the value is falsy', async () => {
76+
const component = {
77+
...simpleTextField,
78+
validate: {
79+
json: {
80+
if: [
81+
{
82+
'===': [
83+
{
84+
var: 'input',
85+
},
86+
'foo',
87+
],
88+
},
89+
true,
90+
"Input must be 'foo'",
91+
],
92+
},
93+
},
94+
};
95+
const data = {
96+
component: '',
97+
};
98+
const context = generateProcessorContext(component, data);
99+
const result = await validateJson(context);
100+
expect(result).to.be.instanceOf(FieldError);
101+
expect(result?.errorKeyOrMessage).to.contain("Input must be 'foo'");
102+
});

src/process/validation/rules/validateJson.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { isObject } from 'lodash';
66

77
export const shouldValidate = (context: ValidationContext) => {
88
const { component, value } = context;
9-
if (!value || !component.validate?.json || !isObject(component.validate.json)) {
9+
if (!component.validate?.json || !isObject(component.validate.json)) {
1010
return false;
1111
}
1212
return true;

0 commit comments

Comments
 (0)