Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Commit

Permalink
allow null on complex items, tests to prove it
Browse files Browse the repository at this point in the history
  • Loading branch information
eleith committed Oct 9, 2012
1 parent ebe9d5d commit 8f0a3ff
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
26 changes: 15 additions & 11 deletions lib/schema.js
Expand Up @@ -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]));

Expand All @@ -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) {
Expand Down
39 changes: 37 additions & 2 deletions test/complex.js
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
});
});

0 comments on commit 8f0a3ff

Please sign in to comment.