Skip to content

Commit

Permalink
Updated validation library
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Dec 2, 2013
1 parent 081e765 commit aa7e173
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 51 deletions.
4 changes: 2 additions & 2 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ Model.prototype.insert = Model.prototype.create = function (object, callback) {

var toInsert = object.shift();
doHook(toInsert);
}
};

var doInsert = function(err, object, next) {
var doInsert = function(err, object) {
if(err) return next(err);

var validation = validate($.options.schema, object);
Expand Down
6 changes: 2 additions & 4 deletions lib/utils/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,12 @@ var validateType = (require.modules || {}).validation = module.exports = functio
/// <param name="got" type="String">The acctual value</param>
/// </signature>

if (condition) return {
passed: true
};
if (condition) return pass;

return fail(expected, got);
};

var propertyPrefix = property ? (property + '.') : 'object.';
var propertyPrefix = property ? (property + '.') : '';

if (!schemaType) return pass;

Expand Down
56 changes: 56 additions & 0 deletions test/transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/// <reference path="../nodelib/node.js"/>
/// <reference path="../nodelib/lodash.js"/>
/// <reference path="../nodelib/mocha.js"/>
/// <reference path="../nodelib/should.js"/>
/// <reference path="../lib/utils/validation.js"/>
/// <reference path="../lib/utils/transforms.js"/>

var _ = require('lodash');
var should = require('should');

describe('utils', function () {
describe('transforms', function () {
var transform = require('../lib/utils/transforms');

it('should allow the transformation of an object\'s properties', function () {
var original = {
a: 'a',
b: 1,
c: 'c',
d: 'd'
};

var expected = {
a: 'aa',
b: 1,
c: 'c',
d: 'd'
};

var trans = {
a: { $t: function (value) { return value + value; } },
b: false,
c: { $t: false }
};

transform(trans, '$t', original);

original.should.eql(expected);
});

it('should remove properties where the transform returns undefined', function () {
var original = {
a: 1
};

var expected = {
b: '1'
};

var trans = {
a: { $t: function (value) { return undefined; } },
b: { $t: function (value) { return '1'; } }
};
});
});
});
53 changes: 8 additions & 45 deletions test/utils.js → test/validation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="../nodelib/node.js"/>
/// <reference path="../nodelib/node.js"/>
/// <reference path="../nodelib/lodash.js"/>
/// <reference path="../nodelib/mocha.js"/>
/// <reference path="../nodelib/should.js"/>
Expand All @@ -13,7 +13,9 @@ describe('utils', function () {
describe('validation', function () {
var validation = require('../lib/utils/validation');
function validate(schema, value, pass, message) {
validation(schema, value).should.have.ownProperty('passed', pass, message);
var result = validation(schema, value);
result.should.have.ownProperty('passed', pass, message);
return result;
}

it('should allow validation of basic types', function () {
Expand Down Expand Up @@ -81,50 +83,11 @@ describe('utils', function () {
b: 'b'
}, false);
});
});

describe('transforms', function () {
var transform = require('../lib/utils/transforms');

it('should allow the transformation of an object\'s properties', function () {
var original = {
a: 'a',
b: 1,
c: 'c',
d: 'd'
};

var expected = {
a: 'aa',
b: 1,
c: 'c',
d: 'd'
};

var trans = {
a: { $t: function (value) { return value + value; } },
b: false,
c: { $t: false }
};

transform(trans, '$t', original);

original.should.eql(expected);
});

it('should remove properties where the transform returns undefined', function () {
var original = {
a: 1
};

var expected = {
b: '1'
};

var trans = {
a: { $t: function (value) { return undefined; } },
b: { $t: function (value) { return '1'; } }
};
it('should allow conversion of a failure to an error object', function() {
var result = validate({ name: Number }, { name: 'Test' }, false);
result.should.have.ownProperty('toError');
should(result.toError() instanceof Error);
});
});
});

0 comments on commit aa7e173

Please sign in to comment.