Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strict: true for embedded documents #717

Closed
timfinity opened this issue Feb 5, 2012 · 4 comments
Closed

strict: true for embedded documents #717

timfinity opened this issue Feb 5, 2012 · 4 comments

Comments

@timfinity
Copy link

I've noticed that strict: true doesn't work for embedded documents. I wasn't sure if this was by design or not.

A quick example:

var Comments = new Schema({
    title     : String
  , body      : String
  , date      : Date
}, {strict: true});

var BlogPost = new Schema({
  , title     : String
  , body      : String
  , comments  : [Comments]
}, {strict: true});

var Post = mongoose.model('BlogPost', BlogPost);

var post = new Post();

post.title = 'Saves';
post.fake = 'Doesnt save';

post.comments.push({
  title: 'Saves',
  fake: 'Shouldnt save but does'
});

post.save(function(err){

});
@aheckmann
Copy link
Collaborator

well that sucks

@albrow
Copy link

albrow commented Jun 2, 2013

An important note:

You have to put child schema before the parent schema for strict mode to work properly with subdocuments. For this example, that means putting the Comments declaration before the BlogPost declaration. If you don't, strict mode will not work for subdocuments.

Hopefully this will help people avoid the same problem I just ran into. Perhaps a note in the documentation would be helpful? In general, javascript doesn't care what order you declare functions and classes, so I found this behavior a little surprising.

@offminded
Copy link

So it seems mongoose is setting strict mode to true for all but for some reason it is failing to enforce it when it comes to subdocuments.

Even when I put child schema before parent schema it is not working for me, I guess I will post an issue with an example.

@vkarpov15
Copy link
Collaborator

@offminded please do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants