I am trying to test validation on a very simple mongoose model, but I have some problem with exceptions.
//model.js
var ResourceDef = new Schema({
name: {type: String, required: true},
content: String,
});
var Resource = mongoose.model('Resources', ResourceDef);
//test.js
vows.describe('test model behavior').addBatch({
'validating a Resource object with name equals to null': {
topic: function() {
new model.Resource({
name: null,
content: '.....',
}).validate(this.callback);
},
'should throw an error': function(e) {
assert.ok(e);
}
}
}).export(module);
I get this error:
validating a Resource object with name equals to null ✗ should throw an error » An unexpected error was caught: ValidationError: Validator "required" failed for path name
I don't understand why mongoose is throwing that ValidationError!
On Saturday, November 3, 2012 9:39:31 AM UTC-7, Alberto Gori wrote:
> I am trying to test validation on a very simple mongoose model, but I have > some problem with exceptions.
> //model.js
> var ResourceDef = new Schema({
> name: {type: String, required: true},
> content: String,
> });
> var Resource = mongoose.model('Resources', ResourceDef);
> //test.js
> vows.describe('test model behavior').addBatch({
> 'validating a Resource object with name equals to null': {
> topic: function() {
> new model.Resource({
> name: null,
> content: '.....',
> }).validate(this.callback);
> },
> 'should throw an error': function(e) {
> assert.ok(e);
> }
> }
> }).export(module);
> I get this error:
> validating a Resource object with name equals to null > ✗ should throw an error > » An unexpected error was caught: ValidationError: Validator > "required" failed for path name
> I don't understand why mongoose is throwing that ValidationError!
I discovered that the problem is not inherent to mongoose validation method, but to vows. Incredibly vows fails to manage Error in callback. For example:
It's actually a conscious design choice in vows, but I agree that it's a mistake. In the async case (last time I checked) it looks at the arity of your vow function (function.length), and if it takes less than two arguments, it assumes that you don't want the error passed to your function, just the "result", and that you want to classify it as an error every time an error is passed to this.callback.
I guess the underlying assumption is that an async function will always deliver a result to its callback, but it's clearly too magic. It has caused a lot of wtfs for me and my team. The "fix" is to add a bogus 2nd argument to your vow function.
On Sunday, November 4, 2012 12:07:59 PM UTC+1, Alberto Gori wrote:
> I discovered that the problem is not inherent to mongoose validation > method, but to vows. Incredibly vows fails to manage Error in callback. For > example:
I was just having this exact issue the other day, thanks for the catch
Papandreou! You just saved me some serious time.
-Chad
From: nodejs@googlegroups.com [mailto:nodejs@googlegroups.com] On Behalf
Of papandreou
Sent: Monday, November 05, 2012 3:15 AM
To: nodejs@googlegroups.com
Subject: [nodejs] Re: testing mongoose validation with vows
It's actually a conscious design choice in vows, but I agree that it's a
mistake. In the async case (last time I checked) it looks at the arity
of your vow function (function.length), and if it takes less than two
arguments, it assumes that you don't want the error passed to your
function, just the "result", and that you want to classify it as an
error every time an error is passed to this.callback.
I guess the underlying assumption is that an async function will always
deliver a result to its callback, but it's clearly too magic. It has
caused a lot of wtfs for me and my team. The "fix" is to add a bogus 2nd
argument to your vow function.
Best regards,
Andreas Lind Petersen (papandreou)
On Sunday, November 4, 2012 12:07:59 PM UTC+1, Alberto Gori wrote:
I discovered that the problem is not inherent to mongoose validation
method, but to vows. Incredibly vows fails to manage Error in callback.
For example: