Skip to content

Commit

Permalink
fix(lib/traverseschema.js): filter out skippable keywords based on pa…
Browse files Browse the repository at this point in the history
…rent's type

'properties', 'required' and 'examples' should be skipped only when they are part of the 'object' or
'array' schema type. These should not be ommited when then are part of 'properties' object, for
instance.

BREAKING CHANGE: If schema contained 'properties', 'required' or 'examples' as a part of JSON schema
(i.e. it was described as 'properties' within an object of 'object' type), these will be included
into output.

fix #282
  • Loading branch information
puradawid committed Apr 4, 2021
1 parent 13886ec commit 0f75837
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/traverseSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const { parent, pointer } = require('./symbols');
function isSkippableKeyword(schema) {
const skippableKeywords = ['examples', 'required', 'properties'];
const containsCurrentSchema = contains((keyword) => eq(keyword, schema[pointer].split('/').pop()));
const schemaMember = schema[parent] && (schema[parent].type === 'object' || schema[parent].type === 'array');

return containsCurrentSchema(skippableKeywords);
return schemaMember && containsCurrentSchema(skippableKeywords);
}

function reducer({ seen, ids }, schema) {
Expand Down
20 changes: 19 additions & 1 deletion test/traverseSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ const example = {
{ $ref: '#/properties/bar' },
],
},
properties: {
type: 'object',
description: 'properties witin properties',
},
required: {
type: 'array',
examples: [
[
{
properies: [],
},
],
],
},
examples: {
type: 'number',
examples: [1, 2, 3],
},
},
};

Expand All @@ -70,7 +88,7 @@ describe('Testing Schema Traversal', () => {
const proxied = loader()(example, 'example.schema.json');
const schemas = traverse([proxied]);

assert.equal(schemas.length, 8);
assert.equal(schemas.length, 11);
assert.equal(schemas[7][filename], 'example.schema.json');
});

Expand Down

0 comments on commit 0f75837

Please sign in to comment.