Skip to content

Commit

Permalink
Handle empty objects in EqualKeys
Browse files Browse the repository at this point in the history
Closes #34.
  • Loading branch information
ruimarinho committed Jan 3, 2016
1 parent 15d8e94 commit 4920d52
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
22 changes: 18 additions & 4 deletions src/asserts/equal-keys-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { Violation } from 'validator.js';
import { difference, isPlainObject } from 'lodash';
import { difference, intersection, isArray, isPlainObject } from 'lodash';

/**
* Export `EqualKeysAssert`.
Expand All @@ -17,6 +17,14 @@ export default function equalKeysAssert(keys) {

this.__class__ = 'EqualKeys';

if (!isArray(keys)) {
throw new Error('Keys must be an array');
}

if (keys.length === 0) {
throw new Error('At least one key must be set');
}

/**
* Keys.
*/
Expand All @@ -32,10 +40,16 @@ export default function equalKeysAssert(keys) {
throw new Violation(this, value, { value: 'must_be_a_plain_object' });
}

const diff = difference(Object.keys(value), this.keys);
const keys = Object.keys(value);

if (keys.length === 0) {
throw new Violation(this, value, { difference: difference(this.keys, keys) });
}

const intersects = intersection(this.keys, keys);

if (diff.length > 0) {
throw new Violation(this, value, { difference: diff });
if (intersects.length !== keys.length) {
throw new Violation(this, value, { difference: difference(keys, this.keys) });
}

return true;
Expand Down
36 changes: 33 additions & 3 deletions test/asserts/equal-keys-assert_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,27 @@ describe('EqualKeysAssert', () => {
});
});

it('should throw an error if an object does not have the expected keys', () => {
it('should throw an error if the `keys` to compare to are not set', () => {
try {
new Assert().EqualKeys();

should.fail();
} catch (e) {
e.message.should.equal('Keys must be an array');
}
});

it('should throw an error if the `keys` to compare to are invalid', () => {
try {
new Assert().EqualKeys([]);

should.fail();
} catch (e) {
e.message.should.equal('At least one key must be set');
}
});

it('should throw an error if the object not have the expected keys', () => {
try {
new Assert().EqualKeys(['foo']).validate({ bar: 'biz', foo: 'qux' });

Expand All @@ -45,17 +65,27 @@ describe('EqualKeysAssert', () => {
}
});

it('should throw an error if the object is empty', () => {
try {
new Assert().EqualKeys(['foo']).validate({});

should.fail();
} catch (e) {
e.show().violation.difference.should.eql(['foo']);
}
});

it('should expose `assert` equal to `EqualKeys`', () => {
try {
new Assert().EqualKeys().validate(123);
new Assert().EqualKeys(['foo']).validate(123);

should.fail();
} catch (e) {
e.show().assert.should.equal('EqualKeys');
}
});

it('should expose `difference` on the violation', () => {
it('should expose the keys `difference` on the violation', () => {
try {
new Assert().EqualKeys(['foo']).validate({ bar: 'biz', foo: 'qux' });

Expand Down

0 comments on commit 4920d52

Please sign in to comment.