Skip to content

Commit

Permalink
Handle missing object property in exactShape predicate (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Zherdev authored and sindresorhus committed May 12, 2019
1 parent 702c283 commit 5421063
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions source/utils/match-shape.ts
Expand Up @@ -57,6 +57,10 @@ export function exact(object: {[key: string]: any}, shape: Shape, parent?: strin
if (isPredicate(shape[key])) {
test(object[key], label, shape[key] as BasePredicate);
} else if (is.plainObject(shape[key])) {
if (!Object.prototype.hasOwnProperty.call(object, key)) {
return `Expected \`${label}\` to exist`;
}

const result = exact(object[key], shape[key] as Shape, label);

if (result !== true) {
Expand Down
9 changes: 9 additions & 0 deletions test/object.ts
Expand Up @@ -253,6 +253,15 @@ test('object.exactShape', t => {
}
}));
}, 'Did not expect property `rainbow.value` to exist, got `🌈` in object `foo`');

t.throws(() => {
ow({unicorn: '🦄'}, ow.object.exactShape({
unicorn: ow.string,
rainbow: {
valid: ow.boolean
}
}));
}, 'Expected property `rainbow` to exist in object');
});

test('object.partialShape', t => {
Expand Down

0 comments on commit 5421063

Please sign in to comment.