Skip to content

Commit

Permalink
Return false for non-empty objects (#143)
Browse files Browse the repository at this point in the history
* Return false for non-empty objects

* Check that properties are empty.

Rather than the object itself.

* Refactor hasEmptyProperty. Add test

* Fix minor typo
  • Loading branch information
a-morn authored and kalinchernev committed Nov 23, 2018
1 parent e93291f commit 95727cc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
16 changes: 4 additions & 12 deletions lib/helpers/getSpecificationObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@ const specHelper = require('./specification');
const parseApiFile = require('./parseApiFile');
const filterJsDocComments = require('./filterJsDocComments');
const convertGlobPaths = require('./convertGlobPaths');

function isEmptyObject(obj) {
// eslint-disable-next-line
Object.keys(obj).forEach(key => {
if (key in obj) return false;
});

return true;
}
const hasEmptyProperty = require('./hasEmptyProperty');

/**
* OpenAPI specification validator does not accept empty values for a few properties.
Expand All @@ -30,9 +22,9 @@ function cleanUselessProperties(inputSpec) {
'securityDefinitions',
];

toClean.forEach(unncessaryProp => {
if (isEmptyObject(improvedSpec[unncessaryProp])) {
delete improvedSpec[unncessaryProp];
toClean.forEach(unnecessaryProp => {
if (hasEmptyProperty(improvedSpec[unnecessaryProp])) {
delete improvedSpec[unnecessaryProp];
}
});

Expand Down
14 changes: 14 additions & 0 deletions lib/helpers/hasEmptyProperty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Checks if there is any properties of @obj that is a empty object
* @function
* @param {object} obj - the object to check
*/
function hasEmptyProperty(obj) {
return Object.values(obj).every(
keyObject =>
typeof keyObject === 'object' &&
Object.keys(keyObject).every(key => !(key in keyObject))
);
}

module.exports = hasEmptyProperty;
16 changes: 16 additions & 0 deletions test/helpers/specification-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Dependencies.
const chai = require('chai');
const specHelper = require('../../lib/helpers/specification');
const hasEmptyProperty = require('../../lib/helpers/hasEmptyProperty');

const { expect } = chai;
const swaggerObject = require('../fixtures/v2/swaggerObject.json');
Expand Down Expand Up @@ -77,4 +78,19 @@ describe('swagger-helpers submodule', () => {
expect(testObject.responses.api).to.include.keys(['foo', 'bar']);
done();
});

it('hasEmptyProperty() identifies object with an empty object or array as property', done => {
const invalidA = { foo: {} };
const invalidB = { foo: [] };
const validA = { foo: { bar: 'baz' } };
const validB = { foo: ['¯_(ツ)_/¯'] };
const validC = { foo: '¯_(ツ)_/¯' };

expect(hasEmptyProperty(invalidA)).equal(true);
expect(hasEmptyProperty(invalidB)).equal(true);
expect(hasEmptyProperty(validA)).equal(false);
expect(hasEmptyProperty(validB)).equal(false);
expect(hasEmptyProperty(validC)).equal(false);
done();
});
});

0 comments on commit 95727cc

Please sign in to comment.