Skip to content

Commit

Permalink
fix: do not remove errors with no children (atlassian#26)
Browse files Browse the repository at this point in the history
* fix: do not remove errors with no children

* fix: added snapshot test
  • Loading branch information
acheronfail authored and torifat committed Aug 28, 2018
1 parent 34fb6bc commit cbacb75
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/__tests__/__snapshots__/helpers.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
8 changes: 6 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down

0 comments on commit cbacb75

Please sign in to comment.