Merge pull request #12 from ekeitho/master

fixed custom filter bug with arrays #9
This commit is contained in:
eleith 2014-08-23 23:34:15 -07:00
commit ed6305c707
2 changed files with 65 additions and 17 deletions

View File

@ -6,7 +6,8 @@ var Schema = function(schema) {
};
var checkArray = function(schema, key, errors, values) {
values[key].forEach(function(value, index) {
var valid = true;
values.forEach(function(value, index) {
try {
if(schema.schema.schema) {
// if array has another schema, then we want objects!
@ -16,20 +17,22 @@ var checkArray = function(schema, key, errors, values) {
{
errors[key] = errors[key] || [];
errors[key][index] = results.errors;
} else {
values[index] = results.data;
}
values[key][index] = results.data;
}
else
values[key][index] = rules.create(key + "[" + index + "]", schema.schema).apply(value);
values[index] = rules.create(key + "[" + index + "]", schema.schema).apply(value);
} catch (error) {
valid = false;
if (!_.isArray(errors[key])) errors[key] = [];
errors[key][index] = error;
}
});
return valid;
};
/*jshint loopfunc:true*/
Schema.prototype.validate = function(data) {
var params = _.keys(this.schema);
var errors = {};
@ -46,28 +49,30 @@ Schema.prototype.validate = function(data) {
// if undefined, don't store it.
value = rules.create(params[i], schema).apply(get(params[i]));
if (!_.isUndefined(value)) {
values[params[i]] = value;
}
// does this rule contain embedded schemas
if (typeof(schema.schema) == "object" &&
!_.isArray(schema.schema) &&
_.keys(schema.schema).length &&
!_.isUndefined(values[params[i]])) {
!_.isUndefined(value)) {
if(!(schema.allownull && _.isNull(values[params[i]]))) {
if(!(schema.allownull && _.isNull(value))) {
if (schema.type == "object") {
var results = (new Schema(schema.schema)).validate(values[params[i]]);
if(!results.valid)
var results = (new Schema(schema.schema)).validate(value);
if(!results.valid) {
errors[params[i]] = results.errors;
values[params[i]] = results.data;
} else {
values[params[i]] = results.data;
}
} else if (schema.type == "array") {
checkArray(schema, params[i], errors, values, get);
if( checkArray(schema, params[i], errors, value, get)){
values[params[i]] = value;
}
}
}
} else if (!_.isUndefined(value)) {
values[params[i]] = value;
}
} catch (error) {
errors[params[i]] = error;
}

View File

@ -3,7 +3,6 @@ describe("filter schemas", function()
/*jshint expr:true*/
var schemajs = (typeof window === 'undefined') ? require('../schema') : window.schema;
var expect = (typeof window === 'undefined') ? require('chai').expect : window.chai.expect;
it("minimum string length property", function()
{
var schema = schemajs.create(
@ -68,7 +67,51 @@ describe("filter schemas", function()
expect(!fruit2.valid).to.be.ok;
expect(fruit3.valid).to.be.ok;
});
it("testing nested properties", function()
{
var schema = schemajs.create({
alphabet: {
'type':"array",
'properties':{'min':1,'max':3},
'required':true,
schema: {
'type':"string",
'properties':{'min':1, 'max':3},
'required':true
}
}
});
var abc1 = schema.validate({alphabet:["a","b","c"]});
var abc2 = schema.validate({alphabet:["aaaaa","bbbbbb","ccccc"]});
//test validation
expect(abc1.valid).to.be.ok;
expect(abc2.valid).to.be.false;
//test data
expect(abc1.data).to.deep.equal({alphabet:["a","b","c"]});
//if false valid, should not be any data
expect(Object.keys(abc2.data).length).to.equal(0);
var schema = schemajs.create({
work: {
'type':'string',
'properties':{'min':1,'max':10}
}
});
var job = schema.validate({work:"engineer"});
var job2 = schema.validate({work:""});
expect(job.valid).to.be.ok;
expect(job.data).to.be.ok;
expect(job2.valid).to.be.false;
expect(Object.keys(job2.data).length).to.equal(0);
});
it("testing custom property", function()
{
schemajs.properties.notIn = function(value, list)