From b8317f429a61406437d210a630745980d54a142c Mon Sep 17 00:00:00 2001 From: Ali Nehzat Date: Fri, 8 Apr 2016 11:37:51 +1000 Subject: [PATCH] fix arrays passing as objects --- lib/matchers/object.js | 2 +- test/matchers/object.spec.js | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/matchers/object.js b/lib/matchers/object.js index 7660a41..f27a93c 100644 --- a/lib/matchers/object.js +++ b/lib/matchers/object.js @@ -9,7 +9,7 @@ module.exports = factory({ match: function(path, value) { var errors = []; var key; - if (value == null || typeof value !== 'object') { + if (value == null || typeof value !== 'object' || value instanceof Array) { return [{path: path, value: value, message: 'should be an object'}]; } for (key in this.fields) { diff --git a/test/matchers/object.spec.js b/test/matchers/object.spec.js index 99b122b..38cca45 100644 --- a/test/matchers/object.spec.js +++ b/test/matchers/object.spec.js @@ -20,7 +20,20 @@ describe('object matcher', function() { }]); }); - it('rejects anything that isnt an object', function() { + it('rejects array values', function() { + var schema = new object({ + name: new string(), + age: new number() + }); + + schema.match('path.to.something', [1,2,3]).should.eql([{ + path: 'path.to.something', + value: [1,2,3], + message: 'should be an object' + }]); + }); + + it('rejects anything that isnt an object', function() { var schema = new object({ name: new string(), age: new number()