diff --git a/lib/schema.js b/lib/schema.js index f7c5c95..9952402 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -63,7 +63,6 @@ Schema.prototype.validate = function(data) { var schema = this.schema[params[i]]; try { - // if undefined, don't store it. value = rules.create(params[i], schema).apply(get(params[i])); @@ -72,16 +71,21 @@ Schema.prototype.validate = function(data) { } // does this rule contain embedded schemas - if (typeof(schema.schema) == "object" && !_.isArray(schema.schema) && _.keys(schema.schema).length && !_.isUndefined(values[params[i]])) { - if (schema.type == "object") { - //checkObject(schema, params[i], errors, values, get); - var results = (new Schema(schema.schema)).validate(values[params[i]]); - - if(!results.valid) - errors[params[i]] = results.errors; - values[params[i]] = results.data; - } else if (schema.type == "array") { - checkArray(schema, params[i], errors, values, get); + if (typeof(schema.schema) == "object" && + !_.isArray(schema.schema) && + _.keys(schema.schema).length && + !_.isUndefined(values[params[i]])) { + + if(!(schema.allownull && _.isNull(values[params[i]]))) { + if (schema.type == "object") { + var results = (new Schema(schema.schema)).validate(values[params[i]]); + + if(!results.valid) + errors[params[i]] = results.errors; + values[params[i]] = results.data; + } else if (schema.type == "array") { + checkArray(schema, params[i], errors, values, get); + } } } } catch (error) { diff --git a/test/complex.js b/test/complex.js index 2dddf11..e8992dd 100644 --- a/test/complex.js +++ b/test/complex.js @@ -17,16 +17,33 @@ describe("complex schemas", function() } } }); + + var schema2 = schemajs.create( + { + input: + { + type:'array', + schema: + { + type: 'number' + }, + allownull: true + } + }); var input1 = schema.validate({input: 'username'}); var input2 = schema.validate({input: [112390123]}); var input3 = schema.validate({input: [112390123, 'username']}); var input4 = schema.validate({}); - + var input5 = schema.validate({input: null}); + var input6 = schema2.validate({input: null}); + expect(!input1.valid).to.be.ok; expect(input2.valid).to.be.ok; expect(!input3.valid).to.be.ok; expect(input4.valid).to.be.ok; + expect(!input5.valid).to.be.ok; + expect(input6.valid).to.be.ok; }); it("arrays and objects", function() @@ -76,15 +93,33 @@ describe("complex schemas", function() } } }); + + var schema2 = schemajs.create( + { + input: + { + type:'object', + schema: + { + name: { type: "string", properties: { max: 255 }, required: true}, + email: { type: "email", error: "email is not a valid email address"} + }, + allownull: true + } + }); var input1 = schema.validate({input: 'username'}); var input2 = schema.validate({input: {name:"x", email:"x@xyz.com"}}); var input3 = schema.validate({input: {name:123, email:"x@xyz.com"}}); var input4 = schema.validate({}); - + var input5 = schema.validate({input:null}); + var input6 = schema2.validate({input:null}); + expect(!input1.valid).to.be.ok; expect(input2.valid).to.be.ok; expect(input2.valid).to.be.ok; expect(input4.valid).to.be.ok; + expect(!input5.valid).to.be.ok; + expect(input6.valid).to.be.ok; }); });