scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
556 stars 80 forks source link

Support for non-required embedded documents #106

Open danjohnson95 opened 7 years ago

danjohnson95 commented 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.

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!