I have an Schema model like this:
var propertySchema = new Schema({
name: {type: String, required: true},
surname: String
});
var objSchema = new Schema({
properties: [prepertySchema]
});
var accountSchema = new Schema({
objects: [objSchema]
});
mongoose.model('account', accountSchema);
Then i have the operations:
account.objects.push(null);
account.save(function(error, account) {
//Error checking and response
})
In that case, i'm getting a ValidationError because of the null value. This is expected. But, in the next operations:
var obj = {properties: null}
account.objects.push(obj);
account.save(function(error, account) {
//Error checking and response
})
Here the value is stored on database, and then i have an unexpected null value where it had been an array. Doing that with objects like this,
var obj = {
properties: [{name:'randname'}, null]
}
Also saves null values in the database that are prohibited to the data model.
I've read about validators, and middleware for checking things. Is there anyway to do this directly on the schema, or i have to parse the received object before i save it in the database? What is the best approach for this?