Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #5 from domharrington/master

Array sub-schema validation
  • Loading branch information...
commit 8bf872deb90b96944c4395a79578056be64c690b 2 parents 8ad6a94 + efda084
Paul Serby authored
Showing with 79 additions and 34 deletions.
  1. +2 −1  .travis.yml
  2. +51 −30 lib/schemata.js
  3. +26 −3 test/schemata.test.js
3  .travis.yml
View
@@ -1,4 +1,5 @@
language: node_js
node_js:
- 0.6
- - 0.8
+ - 0.8
+ - "0.10"
81 lib/schemata.js
View
@@ -96,12 +96,12 @@ module.exports = function(schema) {
}
} else {
switch (type) {
- case Object:
- value = {};
- break;
- case Array:
- value = [];
- break;
+ case Object:
+ value = {};
+ break;
+ case Array:
+ value = [];
+ break;
}
}
newEntity[key] = value;
@@ -174,29 +174,29 @@ module.exports = function(schema) {
throw new Error('Missing type');
}
switch (type) {
- case Boolean:
- if (value === undefined || value === '' || value === null) {
- return null;
- }
- return !(value === false || value === 0 || value === '0' ||
- value === 'false' || value === 'off' || value === 'no');
- case Number:
- if (value === undefined || value === '' || value === null) {
- return null;
- }
- return +value;
- case String:
- if (value === undefined || value === '' || value === null) {
- return null;
- }
- return value.toString && value.toString();
- case Object:
- // typeof null === 'object', so the extra check is needed for null
- return (typeof value !== 'object' || value === null) ? {} : value;
- case Date:
- return (value === '' || value === null || value === undefined) ? null : (value instanceof Date ? value : new Date(value));
- case Array:
- return (value === '' || value === null || value === undefined) ? [] : (Array.isArray(value) ? value : [value]);
+ case Boolean:
+ if (value === undefined || value === '' || value === null) {
+ return null;
+ }
+ return !(value === false || value === 0 || value === '0' ||
+ value === 'false' || value === 'off' || value === 'no');
+ case Number:
+ if (value === undefined || value === '' || value === null) {
+ return null;
+ }
+ return +value;
+ case String:
+ if (value === undefined || value === '' || value === null) {
+ return null;
+ }
+ return value.toString && value.toString();
+ case Object:
+ // typeof null === 'object', so the extra check is needed for null
+ return (typeof value !== 'object' || value === null) ? {} : value;
+ case Date:
+ return (value === '' || value === null || value === undefined) ? null : (value instanceof Date ? value : new Date(value));
+ case Array:
+ return (value === '' || value === null || value === undefined) ? [] : (Array.isArray(value) ? value : [value]);
}
return value;
}
@@ -290,7 +290,7 @@ module.exports = function(schema) {
});
}
- }, function(error, results) {
+ }, function(error) {
seriesCallback(error || Object.keys(errors).length > 0);
});
}
@@ -308,6 +308,27 @@ module.exports = function(schema) {
seriesCallback();
}
}
+ , function(seriesCallback) {
+ // This property has an array subschema that needs validating
+ if ((typeof property.type !== 'undefined') && (typeof property.type.arraySchema !== 'undefined')) {
+ var index = 0
+
+ async.forEach(entityObject[key], function (i, next) {
+ property.type.arraySchema.validate(i, set, tag, function (error, subSchemaArrayErrors) {
+ if (Object.keys(subSchemaArrayErrors).length > 0) {
+ if (!errors[key]) errors[key] = {}
+ errors[key][index] = subSchemaArrayErrors;
+ }
+ index += 1
+ next(null)
+ })
+ }, function () {
+ seriesCallback(errors[key] && errors[key].length > 1 ? errors : null)
+ })
+ } else {
+ seriesCallback();
+ }
+ }
]
, function(error) {
propertyCallback(error === true ? undefined : error);
29 test/schemata.test.js
View
@@ -3,7 +3,6 @@ var schemata = require('..')
, propertyValidator = require('validity/property-validator')
, should = require('should')
-
function createContactSchema() {
var schema = schemata({
name: {
@@ -535,12 +534,36 @@ describe('schemata', function() {
})
})
- it('Validates array sub-schemas')
+ it('Validates array sub-schemas', function (done) {
+ var schema = createBlogSchema()
+ , model = schema.makeBlank()
+ , subSchema = schema.schema.comments.type.arraySchema.schema
+
+ subSchema.email.validators = {
+ all: [validity.required]
+ }
+
+ subSchema.comment.validators = {
+ all: [validity.required]
+ }
+
+ model.comments.push({ email: null, comment: null })
+ model.comments.push({ email: null, comment: 'comment' })
+ model.comments.push({ email: 'dom@test.com', comment: null })
+
+ schema.validate(model, function (error, errors) {
+ errors.comments[0].should.eql({ email: 'Email is required',comment: 'Comment is required' })
+ errors.comments[1].should.eql({ email: 'Email is required' })
+ errors.comments[2].should.eql({ comment: 'Comment is required' })
+
+ done()
+ })
+
+ })
it('allows error response to be a string instead of Error object', function(done) {
var schema = createContactSchema()
-
schema.schema.name.validators = {
all: [function(key, errorProperty, object, callback) {
return callback(undefined, object[key] ? undefined : errorProperty + ' is required')
Please sign in to comment.
Something went wrong with that request. Please try again.