Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Commits on Aug 28, 2012
Paul Serby bumping version 1ddf2cf
Commits on Aug 29, 2012
Paul Serby Refactor to validity 16e8242
Showing with 41 additions and 37 deletions.
  1. +15 −8 lib/schemata.js
  2. +2 −2 package.json
  3. +24 −27 test/schemata.test.js
23 lib/schemata.js
View
@@ -275,22 +275,29 @@ module.exports = function(schema) {
return seriesCallback();
}
var validators = property.validators[set]
- , propertyName = (property.name === undefined) ? stringUtils.decamelcase(key) : property.name
+ , errorName = (property.name === undefined) ? stringUtils.decamelcase(key) : property.name
;
async.forEach(validators, function(validator, validateCallback) {
if (errors[key]) {
- validateCallback();
+ return validateCallback();
} else {
- validator(propertyName, entityObject[key], validateCallback);
+
+ validator(key, errorName, entityObject, function(error, valid) {
+ if (error) {
+ return validateCallback(error);
+ }
+ if (valid) {
+ errors[key] = valid;
+ }
+ validateCallback();
+ });
}
- }, function(error) {
+
+ }, function(error, results) {
if (error) {
- // Backwards compatibility to allow Error objects to be
- // returned from validators as well as just strings
- errors[key] = error.message ? error.message : error;
+ return seriesCallback(error);
}
-
seriesCallback(true);
});
}
4 package.json
View
@@ -1,7 +1,7 @@
{
"name": "schemata",
"description": "Define, create, and validate your business objects, based on specified schema.",
- "version": "0.0.1",
+ "version": "0.0.2",
"author": "Paul Serby <paul@serby.net>",
"contributors": [
{
@@ -15,7 +15,7 @@
],
"dependencies": {
"async": "~0.1",
- "piton-validity": "~0.1",
+ "validity": "~0.0",
"piton-string-utils": "~0.3"
},
"devDependencies": {
51 test/schemata.test.js
View
@@ -1,6 +1,6 @@
var schemata = require('..')
- , validation = require('piton-validity').validation
- , createPropertyValidator = require('piton-validity').createPropertyValidator
+ , validity = require('validity')
+ , propertyValidator = require('validity/property-validator')
, should = require('should')
;
@@ -356,7 +356,7 @@ describe('schemata', function() {
schema.schema.name.should.not.have.property('validators');
schema.schema.name.validators = {
- all: [validation.required.validate]
+ all: [validity.required]
};
schema.validate(schema.makeDefault({ name: '' }), 'all', function(errors) {
@@ -367,9 +367,8 @@ describe('schemata', function() {
it('uses the [all] set by default', function(done) {
var schema = createContactSchema();
-
schema.schema.name.validators = {
- all: [validation.required.validate]
+ all: [validity.required]
};
schema.validate(schema.makeDefault({ name: '' }), '', function(errors) {
@@ -382,11 +381,11 @@ describe('schemata', function() {
var schema = createContactSchema();
schema.schema.name.validators = {
- all: [validation.required.validate]
+ all: [validity.required]
};
schema.schema.age.validators = {
- all: [validation.required.validate]
+ all: [validity.required]
};
schema.validate(schema.makeDefault({ name: '', age: 33 }), 'all', function(errors) {
@@ -399,11 +398,11 @@ describe('schemata', function() {
var schema = createContactSchema();
schema.schema.name.validators = {
- all: [validation.required.validate, validation.length(2, 4).validate]
+ all: [validity.required, validity.length(2, 4)]
};
schema.validate(schema.makeDefault({ name: 'A' }), 'all', function(errors) {
- errors.should.eql({name: "Full Name must be between 2 and 4 in length"});
+ errors.should.eql({name: 'Full Name must be between 2 and 4 in length'});
done();
});
});
@@ -413,12 +412,12 @@ describe('schemata', function() {
// Adding required validation to a schema property with a tag
schema.schema.name.validators = {
- all: [validation.required.validate]
+ all: [validity.required]
};
// Adding required validation to a schema property without a tag
schema.schema.age.validators = {
- all: [validation.required.validate]
+ all: [validity.required]
};
schema.validate(schema.makeBlank(), 'all', 'update', function(errors) {
@@ -433,13 +432,13 @@ describe('schemata', function() {
var schema = createContactSchema();
schema.schema.name.validators = {
- all: [validation.required.validate]
+ all: [validity.required]
};
schema.schema.name.tag = ['newTag'];
schema.schema.age.validators = {
- all: [validation.required.validate]
+ all: [validity.required]
};
schema.schema.age.tag = ['differentTag'];
@@ -457,11 +456,11 @@ describe('schemata', function() {
var schema = createContactSchema();
schema.schema.name.validators = {
- all: [validation.required.validate]
+ all: [validity.required]
};
schema.schema.age.validators = {
- all: [validation.required.validate]
+ all: [validity.required]
};
schema.validate(schema.makeBlank(), function(errors) {
@@ -476,7 +475,7 @@ describe('schemata', function() {
it('Validates sub-schemas', function() {
var schema = createBlogSchema();
schema.schema.author.type.schema.age.validators = {
- all: [validation.required.validate]
+ all: [validity.required]
};
schema.validate(schema.makeBlank(), function(errors) {
errors.should.eql({ author: {
@@ -495,9 +494,9 @@ describe('schemata', function() {
it('Validates any defined validators even on sub-schemas', function() {
var schema = createBlogSchema();
schema.schema.author.validators = {
- all: [createPropertyValidator(function(value, callback) {
+ all: [propertyValidator(function(key, object, callback) {
callback(false);
- }, 'Bad').validate]
+ }, 'Bad')]
};
schema.validate(schema.makeBlank(), function(errors) {
errors.should.eql({ author: 'Bad' });
@@ -507,32 +506,30 @@ describe('schemata', function() {
it('validators failure should prevent sub-schema validation', function() {
var schema = createBlogSchema();
schema.schema.author.type.schema.age.validators = {
- all: [createPropertyValidator(function(value, callback) {
+ all: [propertyValidator(function(key, object, callback) {
'This should not get called'.should.equal(false);
callback(false);
- }, 'This should not be seen').validate]
+ }, 'This should not be seen')]
};
schema.schema.author.validators = {
- all: [createPropertyValidator(function(value, callback) {
+ all: [propertyValidator(function(key, object, callback) {
callback(false);
- }, 'From one of the Validators').validate]
+ }, 'From one of the Validators')]
};
schema.validate(schema.makeBlank(), function(errors) {
errors.should.eql({ author: 'From one of the Validators' });
});
});
- it('Validates array sub-schemas', function() {
- //TODO:
- });
+ it('Validates array sub-schemas');
it('allows error response to be a string instead of Error object', function(done) {
var schema = createContactSchema()
;
schema.schema.name.validators = {
- all: [function(name, value, callback) {
- return callback(value ? undefined : name + ' is required');
+ all: [function(key, errorProperty, object, callback) {
+ return callback(undefined, object[key] ? undefined : errorProperty + ' is required');
}]
};

No commit comments for this range

Something went wrong with that request. Please try again.