Skip to content

Commit e012445

Browse files
committed
chore: fix broken tests
1 parent bb83706 commit e012445

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/jsonLogic.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function createValidationChecker(schema) {
2222
const scopes = new Map();
2323

2424
function createScopes(jsonSchema, key = 'root') {
25+
const sampleEmptyObject = buildSampleEmptyObject(schema);
2526
scopes.set(key, createValidationsScope(jsonSchema));
2627
Object.entries(jsonSchema?.properties ?? {})
2728
.filter(([, property]) => property.type === 'object' || property.type === 'array')
@@ -31,6 +32,8 @@ export function createValidationChecker(schema) {
3132
}
3233
createScopes(property, key);
3334
});
35+
36+
validateInlineRules(jsonSchema, sampleEmptyObject);
3437
}
3538

3639
createScopes(schema);
@@ -297,15 +300,39 @@ function buildSampleEmptyObject(schema = {}) {
297300
return sample;
298301
}
299302

300-
function checkRuleIntegrity(rule, id, data) {
303+
function validateInlineRules(jsonSchema, sampleEmptyObject) {
304+
const properties = (jsonSchema?.properties || jsonSchema?.items?.properties) ?? {};
305+
Object.entries(properties)
306+
.filter(([, property]) => property['x-jsf-computedAttributes'] !== undefined)
307+
.forEach(([fieldName, property]) => {
308+
Object.entries(property['x-jsf-computedAttributes'])
309+
.filter(([, value]) => typeof value === 'object')
310+
.forEach(([key, { rule }]) => {
311+
checkRuleIntegrity(
312+
rule,
313+
fieldName,
314+
sampleEmptyObject,
315+
(item) =>
316+
`"${item.var}" in inline rule in property "${fieldName}.x-jsf-computedAttributes.${key}" does not exist as a JSON schema property.`
317+
);
318+
});
319+
});
320+
}
321+
322+
function checkRuleIntegrity(
323+
rule,
324+
id,
325+
data,
326+
errorMessage = (item) => `"${item.var}" in rule "${id}" does not exist as a JSON schema property.`
327+
) {
301328
Object.values(rule ?? {}).map((subRule) => {
302329
if (!Array.isArray(subRule) && subRule !== null && subRule !== undefined) return;
303330
subRule.map((item) => {
304331
const isVar = item !== null && typeof item === 'object' && Object.hasOwn(item, 'var');
305332
if (isVar) {
306333
const exists = jsonLogic.apply({ var: removeIndicesFromPath(item.var) }, data);
307334
if (exists === null) {
308-
throw Error(`"${item.var}" in rule "${id}" does not exist as a JSON schema property.`);
335+
throw Error(errorMessage(item));
309336
}
310337
} else {
311338
checkRuleIntegrity(item, id, data);

src/tests/jsonLogic.test.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
schemaWithGreaterThanChecksForThreeFields,
2323
schemaWithIfStatementWithComputedValuesAndValidationChecks,
2424
schemaWithInlineRuleForComputedAttributeWithCopy,
25+
schemaWithInlinedRuleOnComputedAttributeThatReferencesUnknownVar,
2526
schemaWithMissingRule,
2627
schemaWithMultipleComputedValueChecks,
2728
schemaWithNativeAndJSONLogicChecks,
@@ -178,9 +179,17 @@ describe('cross-value validations', () => {
178179
);
179180
});
180181

181-
it.todo(
182-
'On an inline rule for a computedAttribute, error if theres a value referenced that does not exist'
183-
);
182+
it('On an inline rule for a computedAttribute, error if theres a value referenced that does not exist', () => {
183+
createHeadlessForm(schemaWithInlinedRuleOnComputedAttributeThatReferencesUnknownVar, {
184+
strictInputType: false,
185+
});
186+
expect(console.error).toHaveBeenCalledWith(
187+
'JSON Schema invalid!',
188+
Error(
189+
'"IdontExist" in inline rule in property "field_a.x-jsf-computedAttributes.title" does not exist as a JSON schema property.'
190+
)
191+
);
192+
});
184193
});
185194

186195
describe('Relative: <, >, =', () => {
@@ -515,7 +524,7 @@ describe('cross-value validations', () => {
515524
}
516525
);
517526
const [, fieldB] = fields;
518-
expect(handleValidation({ field_a: 0 }).formErrors).toEqual(undefined);
527+
expect(handleValidation({ field_a: 0, field_b: null }).formErrors).toEqual(undefined);
519528
expect(fieldB.label).toEqual('I need this to work using the 10.');
520529
expect(handleValidation({ field_a: 10 }).formErrors).toEqual(undefined);
521530
expect(fieldB.label).toEqual('I need this to work using the 20.');

0 commit comments

Comments
 (0)