Skip to content

Commit aa7e173

Browse files
committed
Updated validation library
1 parent 081e765 commit aa7e173

File tree

4 files changed

+68
-51
lines changed

4 files changed

+68
-51
lines changed

lib/Model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ Model.prototype.insert = Model.prototype.create = function (object, callback) {
161161

162162
var toInsert = object.shift();
163163
doHook(toInsert);
164-
}
164+
};
165165

166-
var doInsert = function(err, object, next) {
166+
var doInsert = function(err, object) {
167167
if(err) return next(err);
168168

169169
var validation = validate($.options.schema, object);

lib/utils/validation.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ var validateType = (require.modules || {}).validation = module.exports = functio
5757
/// <param name="got" type="String">The acctual value</param>
5858
/// </signature>
5959

60-
if (condition) return {
61-
passed: true
62-
};
60+
if (condition) return pass;
6361

6462
return fail(expected, got);
6563
};
6664

67-
var propertyPrefix = property ? (property + '.') : 'object.';
65+
var propertyPrefix = property ? (property + '.') : '';
6866

6967
if (!schemaType) return pass;
7068

test/transforms.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// <reference path="../nodelib/node.js"/>
2+
/// <reference path="../nodelib/lodash.js"/>
3+
/// <reference path="../nodelib/mocha.js"/>
4+
/// <reference path="../nodelib/should.js"/>
5+
/// <reference path="../lib/utils/validation.js"/>
6+
/// <reference path="../lib/utils/transforms.js"/>
7+
8+
var _ = require('lodash');
9+
var should = require('should');
10+
11+
describe('utils', function () {
12+
describe('transforms', function () {
13+
var transform = require('../lib/utils/transforms');
14+
15+
it('should allow the transformation of an object\'s properties', function () {
16+
var original = {
17+
a: 'a',
18+
b: 1,
19+
c: 'c',
20+
d: 'd'
21+
};
22+
23+
var expected = {
24+
a: 'aa',
25+
b: 1,
26+
c: 'c',
27+
d: 'd'
28+
};
29+
30+
var trans = {
31+
a: { $t: function (value) { return value + value; } },
32+
b: false,
33+
c: { $t: false }
34+
};
35+
36+
transform(trans, '$t', original);
37+
38+
original.should.eql(expected);
39+
});
40+
41+
it('should remove properties where the transform returns undefined', function () {
42+
var original = {
43+
a: 1
44+
};
45+
46+
var expected = {
47+
b: '1'
48+
};
49+
50+
var trans = {
51+
a: { $t: function (value) { return undefined; } },
52+
b: { $t: function (value) { return '1'; } }
53+
};
54+
});
55+
});
56+
});

test/utils.js renamed to test/validation.js

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="../nodelib/node.js"/>
1+
/// <reference path="../nodelib/node.js"/>
22
/// <reference path="../nodelib/lodash.js"/>
33
/// <reference path="../nodelib/mocha.js"/>
44
/// <reference path="../nodelib/should.js"/>
@@ -13,7 +13,9 @@ describe('utils', function () {
1313
describe('validation', function () {
1414
var validation = require('../lib/utils/validation');
1515
function validate(schema, value, pass, message) {
16-
validation(schema, value).should.have.ownProperty('passed', pass, message);
16+
var result = validation(schema, value);
17+
result.should.have.ownProperty('passed', pass, message);
18+
return result;
1719
}
1820

1921
it('should allow validation of basic types', function () {
@@ -81,50 +83,11 @@ describe('utils', function () {
8183
b: 'b'
8284
}, false);
8385
});
84-
});
85-
86-
describe('transforms', function () {
87-
var transform = require('../lib/utils/transforms');
88-
89-
it('should allow the transformation of an object\'s properties', function () {
90-
var original = {
91-
a: 'a',
92-
b: 1,
93-
c: 'c',
94-
d: 'd'
95-
};
96-
97-
var expected = {
98-
a: 'aa',
99-
b: 1,
100-
c: 'c',
101-
d: 'd'
102-
};
103-
104-
var trans = {
105-
a: { $t: function (value) { return value + value; } },
106-
b: false,
107-
c: { $t: false }
108-
};
109-
110-
transform(trans, '$t', original);
111-
112-
original.should.eql(expected);
113-
});
114-
115-
it('should remove properties where the transform returns undefined', function () {
116-
var original = {
117-
a: 1
118-
};
119-
120-
var expected = {
121-
b: '1'
122-
};
12386

124-
var trans = {
125-
a: { $t: function (value) { return undefined; } },
126-
b: { $t: function (value) { return '1'; } }
127-
};
87+
it('should allow conversion of a failure to an error object', function() {
88+
var result = validate({ name: Number }, { name: 'Test' }, false);
89+
result.should.have.ownProperty('toError');
90+
should(result.toError() instanceof Error);
12891
});
12992
});
13093
});

0 commit comments

Comments
 (0)