Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return false for non-empty objects #143

Merged
merged 4 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.values does not exist in Node v6
This should result in a major/braking change version update so as to not break those still using Node v6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could change it too:

Suggested change
return Object.values(obj).every(
return Object.keys(obj).map(key => obj[key]).every(

and consider it a bug fix. This shouldn't warrant a major version bump.

Or transpile the entire lib.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No transpilations please. I had to think about that, but didn't. Most certainly it's not a major version bump.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
});
});