diff --git a/src/__tests__/__snapshots__/helpers.js.snap b/src/__tests__/__snapshots__/helpers.js.snap index 26169f2f..a8f2fcbc 100644 --- a/src/__tests__/__snapshots__/helpers.js.snap +++ b/src/__tests__/__snapshots__/helpers.js.snap @@ -56,6 +56,32 @@ Object { } `; +exports[`filterRedundantErrors should not remove anyOf errors if there are no children 1`] = ` +Object { + "children": Object { + "/object": Object { + "children": Object { + "/type": Object { + "children": Object {}, + "errors": Array [ + Object { + "keyword": "type", + }, + Object { + "keyword": "type", + }, + Object { + "keyword": "anyOf", + }, + ], + }, + }, + "errors": Array [], + }, + }, +} +`; + exports[`filterRedundantErrors should prioritize required 1`] = ` Object { "children": Object { diff --git a/src/__tests__/helpers.js b/src/__tests__/helpers.js index ba9ddda7..ed3d5709 100644 --- a/src/__tests__/helpers.js +++ b/src/__tests__/helpers.js @@ -150,4 +150,33 @@ describe('filterRedundantErrors', () => { filterRedundantErrors(tree); expect(tree).toMatchSnapshot(); }); + + it('should not remove anyOf errors if there are no children', async () => { + const tree = { + children: { + '/object': { + children: { + '/type': { + children: {}, + errors: [ + { + keyword: 'type', + }, + { + keyword: 'type', + }, + { + keyword: 'anyOf', + }, + ], + }, + }, + errors: [], + }, + }, + }; + + filterRedundantErrors(tree); + expect(tree).toMatchSnapshot(); + }); }); diff --git a/src/helpers.js b/src/helpers.js index cfe6ba2c..0be47169 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -52,10 +52,14 @@ export function filterRedundantErrors(root, parent, key) { /** * If there is an `anyOf` error that means we have more meaningful errors * inside children. So we will just remove all errors from this level. + * + * If there are no children, then we don't delete the errors since we should + * have at least one error to report. */ - // TODO: Need to check children too. There might be no children :( if (getErrors(root).some(isAnyOfError)) { - delete root.errors; + if (Object.keys(root.children).length > 0) { + delete root.errors; + } } /**