I have this squema. I need a custom validation like this, but doesn't work, the MonographSchema.path('v1').validate() is never executed.
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var MonographSchema = new Schema({
v1: {
type: String,
trim: true
},
v3: {
_: String,
a: String,
b: String,
c: [String],
t: Number
}
});
MonographSchema.path('v1').validate(function (value) {
if(this.v3.a === 'MC'){
if(!value){
return false;
}
}
return true;
}, 'The field "v1" is obligatory');
mongoose.model('Monograph', MonographSchema);
var monograph = new Monograph({
v3: {'_': 'BR1.1', 'a': 'MC', 'b': 'T17a', 'c': ['v.1', 'e.2'], 't': '1001'}
});
monograph.save(); //The value is inserted without problems, the validation isn't trigger.
Is there any way to do this validation to the String in the squema correctly?