Skip to content

Commit 69a888e

Browse files
committed
Update default schema validator
- accepts sequence - conditional actions
1 parent 063ef6b commit 69a888e

File tree

3 files changed

+347
-3
lines changed

3 files changed

+347
-3
lines changed

packages/react-form-renderer/src/files/default-schema-validator.js

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,23 @@ const checkFieldsArray = (obj, objectKey) => {
1616
}
1717
};
1818

19-
const checkCondition = (condition, fieldName) => {
19+
const checkConditionalAction = (type, action, fieldName) => {
20+
if (action.hasOwnProperty('visible') && typeof action.visible !== 'boolean') {
21+
throw new DefaultSchemaError(`
22+
Error occured in field definition with "name" property: "${fieldName}".
23+
'visible' property in action "${type}" has to be a boolean value! Received: ${typeof action.visible}.
24+
`);
25+
}
26+
27+
if (action.hasOwnProperty('set') && (typeof action.set !== 'object' || Array.isArray(action.set))) {
28+
throw new DefaultSchemaError(`
29+
Error occured in field definition with "name" property: "${fieldName}".
30+
'set' property in action "${type}" has to be a object! Received: ${typeof action.visible}, isArray: ${Array.isArray(action.set)}.
31+
`);
32+
}
33+
};
34+
35+
const checkCondition = (condition, fieldName, isRoot) => {
2036
/**
2137
* validate array condition
2238
*/
@@ -38,14 +54,48 @@ const checkCondition = (condition, fieldName) => {
3854
`);
3955
}
4056

57+
if (condition.hasOwnProperty('sequence') && !Array.isArray(condition.sequence)) {
58+
throw new DefaultSchemaError(`
59+
Error occured in field definition with "name" property: "${fieldName}".
60+
'sequence' property in a field condition must be an array! Received: ${typeof condition.sequence}.
61+
`);
62+
}
63+
64+
if (condition.hasOwnProperty('sequence') && !isRoot) {
65+
throw new DefaultSchemaError(`
66+
Error occured in field definition with "name" property: "${fieldName}".
67+
'sequence' condition has to be the root condition: " condition: { sequence: [ ... ]} "
68+
`);
69+
}
70+
71+
if ((condition.hasOwnProperty('then') || condition.hasOwnProperty('else')) && !isRoot) {
72+
throw new DefaultSchemaError(`
73+
Error occured in field definition with "name" property: "${fieldName}".
74+
'then', 'else' condition keys can be included only in root conditions or in a 'sequence'.
75+
`);
76+
}
77+
78+
if (condition.hasOwnProperty('then')) {
79+
checkConditionalAction('then', condition.then, fieldName);
80+
}
81+
82+
if (condition.hasOwnProperty('else')) {
83+
checkConditionalAction('else', condition.else, fieldName);
84+
}
85+
4186
if (typeof condition !== 'object') {
4287
throw new DefaultSchemaError(`
4388
Error occured in field definition with name: "${fieldName}".
4489
Field condition must be an object, received ${Array.isArray(condition) ? 'array' : typeof condition}!
4590
`);
4691
}
4792

48-
if (!condition.hasOwnProperty('and') && !condition.hasOwnProperty('or') && !condition.hasOwnProperty('not')) {
93+
if (
94+
!condition.hasOwnProperty('and') &&
95+
!condition.hasOwnProperty('or') &&
96+
!condition.hasOwnProperty('not') &&
97+
!condition.hasOwnProperty('sequence')
98+
) {
4999
if (!condition.hasOwnProperty('when')) {
50100
throw new DefaultSchemaError(`
51101
Error occured in field definition with "name" property: "${fieldName}".
@@ -85,6 +135,16 @@ const checkCondition = (condition, fieldName) => {
85135
Field condition must have "pattern" of instance "RegExp" or "string"! Instance received: [${condition.pattern.constructor.name}].
86136
`);
87137
}
138+
} else {
139+
['and', 'or', 'not'].forEach((key) => {
140+
if (condition.hasOwnProperty(key)) {
141+
checkCondition(condition[key], fieldName);
142+
}
143+
});
144+
145+
if (condition.hasOwnProperty('sequence')) {
146+
condition.sequence.forEach((item) => checkCondition(item, fieldName, 'root'));
147+
}
88148
}
89149
};
90150

@@ -211,7 +271,7 @@ const iterateOverFields = (fields, componentMapper, validatorTypes, actionTypes,
211271
}
212272

213273
if (field.hasOwnProperty('condition')) {
214-
checkCondition(field.condition, field.name);
274+
checkCondition(field.condition, field.name, 'root');
215275
}
216276

217277
if (field.hasOwnProperty('validate')) {

packages/react-form-renderer/src/tests/parsers/__snapshots__/default-schema-validator.test.js.snap

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,48 @@ exports[`Default schema validator should fail if input object does fields names
117117

118118
exports[`Default schema validator should fail if input object does not have fields names 1`] = `"Component of type schema must contain \\"fields\\" property of type array, received undefined!"`;
119119

120+
exports[`Default schema validator should fail validation when sequence is not array 1`] = `
121+
"
122+
Error occured in field definition with \\"name\\" property: \\"foo\\".
123+
'sequence' property in a field condition must be an array! Received: object.
124+
"
125+
`;
126+
127+
exports[`Default schema validator should fail validation when sequence is not root condition 1`] = `
128+
"
129+
Error occured in field definition with \\"name\\" property: \\"foo\\".
130+
'sequence' condition has to be the root condition: \\" condition: { sequence: [ ... ]} \\"
131+
"
132+
`;
133+
134+
exports[`Default schema validator should fail validation when set is not object 1`] = `
135+
"
136+
Error occured in field definition with \\"name\\" property: \\"foo\\".
137+
'set' property in action \\"then\\" has to be a object! Received: undefined, isArray: false.
138+
"
139+
`;
140+
141+
exports[`Default schema validator should fail validation when set is not object 2`] = `
142+
"
143+
Error occured in field definition with \\"name\\" property: \\"foo\\".
144+
'set' property in action \\"then\\" has to be a object! Received: undefined, isArray: true.
145+
"
146+
`;
147+
148+
exports[`Default schema validator should fail validation when then/else is not in root condition 1`] = `
149+
"
150+
Error occured in field definition with \\"name\\" property: \\"foo\\".
151+
'then', 'else' condition keys can be included only in root conditions or in a 'sequence'.
152+
"
153+
`;
154+
155+
exports[`Default schema validator should fail validation when then/else is not in root condition 2`] = `
156+
"
157+
Error occured in field definition with \\"name\\" property: \\"foo\\".
158+
'then', 'else' condition keys can be included only in root conditions or in a 'sequence'.
159+
"
160+
`;
161+
120162
exports[`Default schema validator should fail validation when using "and" and "or" conditions 1`] = `
121163
"
122164
Error occured in field definition with \\"name\\" property: \\"foo\\".
@@ -151,3 +193,17 @@ exports[`Default schema validator should fail validation when using wrong data t
151193
Unknow dataType. Data type must be string
152194
"
153195
`;
196+
197+
exports[`Default schema validator should fail validation when visible is not boolean 1`] = `
198+
"
199+
Error occured in field definition with \\"name\\" property: \\"foo\\".
200+
'visible' property in action \\"then\\" has to be a boolean value! Received: string.
201+
"
202+
`;
203+
204+
exports[`Default schema validator should fail validation when visible is not boolean 2`] = `
205+
"
206+
Error occured in field definition with \\"name\\" property: \\"foo\\".
207+
'visible' property in action \\"else\\" has to be a boolean value! Received: string.
208+
"
209+
`;

0 commit comments

Comments
 (0)