Why doesn't Mongoose acknowledge an empty document?

Let's say I have a very simple circuit with a custom validation function that always returns false.

var MofoSchema = new mongoose.Schema({
  name: String
});

MofoSchema.path('name').validate(function (value) {
  console.log("I'm validating");
  return false;
}, 'No pasaran');

mongoose.model('Mofo', MofoSchema);

Then I create a new instance of my document and I check it:

var Mofo = mongoose.model('Mofo');
var mofo = new Mofo({name: "Tarte flambée"});

mofo.validate(function(err) {
  console.log(err);
});

Perfect, the custom validator function is called and populated err.

But now I am doing the same without data :

var Mofo = mongoose.model('Mofo');
var mofo = new Mofo({});

mofo.validate(function(err) {
  console.log(err);
});

The custom function of the validator is not called , but erris undefined. What for? I do not understand why Mongoose does not start a custom validator.

Is this design behavior? This is mistake? Should I hack turnover? Do I have to manually check for empty data before validation?

Am I doing something wrong?

PS: save, MongoDB, .

+2
2

, .

, "null"

var mofo = new Mofo({name: null});

undefined

var MofoSchema = new mongoose.Schema({
  name: {type: String, required: true}
});

MofoSchema.path('name').validate(function (value) {...}, 'no pasaran');
+3

:

var mofo = new Mofo({ name: { type: String, default: ''} });

, . .

0

All Articles