A
A
Alex Kheben2015-05-29 17:12:35
JavaScript
Alex Kheben, 2015-05-29 17:12:35

How to validate the "Object" type in Mongoose schemas?

var mongoose = require('mongoose');
var modelSchema = new mongoose.Schema({
  obj: {
    type: Object,
    required: true
  }
});

var Model = mongoose.model('model', modelSchema);
var model = new Model({ obj: 'anything' });
model.save(function(err, doc) { console.log(err, doc); });

// -> null, { obj: 'anything' }

The problem is that we describe in the schema that the property should be an object, but in fact we can write anything there, except for nulland undefined. I understand that in JS everything except nulland undefinedis an object, but I would like some order in MongoDB collections :)
Is there any nice way to validate this thing?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question