Skip to content

Commit 328bbe7

Browse files
committed
fixed bug: validate would catch error if a callback is supplied which throws an error, and might also call the callback twice (issue #24)
1 parent b25af9e commit 328bbe7

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

lib/valid-object.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,15 +368,16 @@ function validateSchema(obj, schema, names) {
368368
module.exports = function(obj, schema, done) {
369369
try {
370370
validateSchema(obj, schema, []);
371-
372-
if (done) {
373-
done(null, obj);
374-
}
375371
} catch(err) {
376372
if (done) {
377373
done(err);
374+
return;
378375
} else {
379376
throw err;
380377
}
378+
}
379+
380+
if (done) {
381+
done(null, obj);
381382
}
382383
};

test/async-error-test.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// this test is run by Vows (as all files matching *test.js)
2+
3+
var vows = require('vows'),
4+
should = require('should');
5+
6+
var createSchema = require('..').createSchema,
7+
config = require('./config');
8+
9+
var schemaSimple = {
10+
type: 'string'
11+
};
12+
13+
vows.describe('Async Error').addBatch({
14+
'when object is valid and callback does not throw an error': {
15+
topic: function () {
16+
var schema = createSchema(schemaSimple);
17+
var count = 0;
18+
try {
19+
schema.validate('a string', function nice() {
20+
++count;
21+
if (count === 1) {
22+
// do nothing, play nice
23+
} else {
24+
this.callback(new Error('Callback was called twice'));
25+
}
26+
}.bind(this));
27+
this.callback();
28+
} catch(err) {
29+
this.callback(err);
30+
}
31+
},
32+
'we get no error': function (err, result) {
33+
if (err) {
34+
console.error(err.toString());
35+
}
36+
should.not.exist(err);
37+
should.not.exist(result);
38+
}
39+
},
40+
'when object is valid and callback throws an error': {
41+
topic: function () {
42+
var schema = createSchema(schemaSimple);
43+
var count = 0;
44+
try {
45+
schema.validate('a string', function naughty() {
46+
++count;
47+
if (count === 1) {
48+
throw new Error('I am a naughty callback function');
49+
} else {
50+
this.callback(new Error('Callback was called twice'));
51+
}
52+
}.bind(this));
53+
this.callback(new Error('we did not get an error'));
54+
} catch(err) {
55+
// error is expected
56+
this.callback();
57+
}
58+
},
59+
'we get an error': function (err, result) {
60+
if (err) {
61+
console.error(err.toString());
62+
}
63+
should.not.exist(err);
64+
should.not.exist(result);
65+
}
66+
},
67+
'when object is invalid and callback throws an error': {
68+
topic: function () {
69+
var schema = createSchema(schemaSimple);
70+
var count = 0;
71+
try {
72+
schema.validate(42, function naughty() {
73+
++count;
74+
if (count === 1) {
75+
throw new Error('I am a naughty callback function');
76+
} else {
77+
this.callback(new Error('Callback was called twice'));
78+
}
79+
}.bind(this));
80+
this.callback(new Error('we did not get an error'));
81+
} catch(err) {
82+
// error is expected
83+
this.callback();
84+
}
85+
},
86+
'we get an error': function (err, result) {
87+
if (err) {
88+
console.error(err.toString());
89+
}
90+
should.not.exist(err);
91+
should.not.exist(result);
92+
}
93+
}
94+
}).export(module);

0 commit comments

Comments
 (0)