allow null on complex items, tests to prove it
This commit is contained in:
parent
ebe9d5d32a
commit
8f0a3ff1e2
|
@ -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 (typeof(schema.schema) == "object" &&
|
||||
!_.isArray(schema.schema) &&
|
||||
_.keys(schema.schema).length &&
|
||||
!_.isUndefined(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(!(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) {
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
});
|
||||
|
|
Reference in New Issue