Permalink
Browse files

fix bug where object in schema but scalar in value crashes

  • Loading branch information...
1 parent 3de5628 commit b3df86dad07b41a7db9808f8898d51b07470a37c @bronson committed Oct 6, 2011
Showing with 7 additions and 0 deletions.
  1. +5 −0 lib/valid.js
  2. +2 −0 test/validjson.test.js
View
@@ -261,6 +261,11 @@ Valid.JsonError = function(path, value, message) {
Valid.JsonObject = function(path, value, schema) {
+ if(typeof value !== 'object') {
+ this.JsonError(path, value, "must be an object");
+ return;
+ }
+
for(var key in schema) {
if(!schema.hasOwnProperty(key)) continue;
if(key in value) {
View
@@ -102,6 +102,8 @@ Valid.json({} ).assert({abc: 123}, {".":{message: "can't includ
Valid.json({a: {b: {c: 1}}}).assert({a: {b: {c: 2}}}, {'a.b.c': {message: "must equal 1", value: 2}});
Valid.json({a: {b: /wut/i}}).assert({a: {b: "NOWUTY"}});
+Valid.json({a:{}}).assert({a:1}, {"a":{"value":1,"message":"must be an object"}});
+Valid.json({a:1,b:{c:{d:2}}}).assert({a:1,b:{c:1}}, {"b.c":{"value":1,"message":"must be an object"}});
// Valid chains
Valid.json(Valid.optional().string()).assert(undefined);

0 comments on commit b3df86d

Please sign in to comment.