Open danjohnson95 opened 7 years ago
Hi there!
I've got a schema - let's call it Article. Article can (but isn't required to) have a User embedded within it.
Article
User
class Article extends Document { constructor() { super(); this.title = String; this.user = { type: User, required: false default: null } } } class User extends EmbeddedDocument { constructor() { super(); this.name = { type: String, required: true } } }
Now if I try to save an Article with the user specified as null, I get a validation exception because the name in user is missing.
name
Article.findOneAndUpdate({_id: 1}, { title: "foo", user: null });
ValidationError: Key users.name is required, but got undefined
I want the users.name key to be required, but only when a user is specified.
users.name
Is this possible?
Thanks!
Hi there!
I've got a schema - let's call it
Article
.Article
can (but isn't required to) have aUser
embedded within it.Now if I try to save an
Article
with the user specified as null, I get a validation exception because thename
in user is missing.I want the
users.name
key to be required, but only when a user is specified.Is this possible?
Thanks!