Permalink
Browse files

Merge pull request #6 from domharrington/master

Add a fix for failing tests
  • Loading branch information...
2 parents 5d3d0c7 + 1e06d3b commit a51b0fb2ec86655a524c43065db12637a4c6af03 @serby committed Mar 26, 2013
Showing with 25 additions and 1 deletion.
  1. +5 −1 lib/schemata.js
  2. +20 −0 test/schemata.test.js
View
6 lib/schemata.js
@@ -310,7 +310,11 @@ module.exports = function(schema) {
}
, function(seriesCallback) {
// This property has an array subschema that needs validating
- if ((typeof property.type !== 'undefined') && (typeof property.type.arraySchema !== 'undefined')) {
+ if ((typeof property.type !== 'undefined')
+ && (typeof property.type.arraySchema !== 'undefined')
+ && entityObject[key]
+ && entityObject[key].length > 0) {
+
var index = 0
async.forEach(entityObject[key], function (i, next) {
View
20 test/schemata.test.js
@@ -2,6 +2,7 @@ var schemata = require('..')
, validity = require('validity')
, propertyValidator = require('validity/property-validator')
, should = require('should')
+ , async = require('async')
function createContactSchema() {
var schema = schemata({
@@ -558,7 +559,26 @@ describe('schemata', function() {
done()
})
+ })
+
+ it('does not try and validate array sub-schemas that are falsy or []', function (done) {
+ var schema = createBlogSchema()
+ , model = schema.makeBlank()
+ , testValues = [undefined, null, '', 0, []]
+ , subSchema = schema.schema.comments.type.arraySchema.schema
+ subSchema.email.validators = {
+ all: [validity.required]
+ }
+
+ async.forEach(testValues, function (value, next) {
+ model.comments = value
+
+ schema.validate(model, function (error, errors) {
+ errors.should.eql({})
+ next()
+ })
+ }, done)
})
it('allows error response to be a string instead of Error object', function(done) {

0 comments on commit a51b0fb

Please sign in to comment.