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

Support for non-required embedded documents #106

Open
danjohnson95 opened this issue May 12, 2017 · 0 comments
Open

Support for non-required embedded documents #106

danjohnson95 opened this issue May 12, 2017 · 0 comments

Comments

@danjohnson95
Copy link

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.

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.

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.

Is this possible?

Thanks!

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

1 participant