The required attribute does not work properly on subdocuments
See original GitHub issueI have a schema containing a property that is an array of another schema (a subdocument), which I would like to be required. I’ve tried to set the required
attribute to true
for the property, but it does not work as I would like to.
I think it’s best that I show you an example test code:
var should = require('should');
var mongoose = require('mongoose');
// My subdocument schema simplified...
var ThingSchema = new mongoose.Schema({
name: String
});
// My main document schema...
var CollectionSchema = new mongoose.Schema({
name: String,
things: { type: [ThingSchema], required: true } // I don't want this property to be able to be empty...
});
var Collection = mongoose.model('Collection', CollectionSchema);
describe('Saving a Collection', function () {
var db;
before(function (done) {
db = mongoose.connect("localhost", "mongotests");
done();
});
it('fails when it has no things', function (done) {
var collection = new Collection({
name: "A collection"
// I do not set the things property here...
});
collection.save(function (err) {
should.exist(err); // This does not work, but I think it should...
done();
});
});
it('fails when it has NULL things', function (done) {
var collection = new Collection({
name: "A collection",
things: null
});
collection.save(function (err) {
should.exist(err); // This does work
done();
});
});
after(function (done) {
mongoose.disconnect(done);
});
});
I’ve also tried to set the default property to null
for things
in the example above, but that does not help either…
Issue Analytics
- State:
- Created 11 years ago
- Comments:5
Top Results From Across the Web
Mongoose subdocument validation based on required
I have an adress schema and models who using this schema (code below). In one case it is required and in other case...
Read more >Python sdk add new attribute to existing [RESOLVED]
I would like to be able to add some attribute on an existing document edit an existing attribute The values required on the...
Read more >Mongoose v6.8.1: SubDocuments
Subdocuments are documents embedded in other documents. In Mongoose, this means you can nest schemas in other schemas. Mongoose has two distinct notions...
Read more >Mongoose 101: An Introduction to the Basics, Subdocuments ...
Mongoose is a library that makes MongoDB easier to use. It does two things: 1. It gives structure to MongoDB Collections 2.
Read more >Explanation of the error messages for the W3C Markup Validator
You have used the attribute named above in your document, but the document type you are using does not support that attribute for...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve already tried that as well, like this:
But the validator function isn’t even executed…
Hello, I have a similar problem.
I have a sub document I cannot modify, because it’s a schema from a global package. In this case a few properties of the child schema are required. I have created a schematic reenactment of my case. It should be noted that the interleaving is lower than on the next level. I have installed Mongoose 3.8.9.
This is the definition of an external source.
This is my schema.
This is my test.