flyingpluto7
flyingpluto7

Reputation: 1099

How to make mongoose schema property of type (another) schema required

I have this mongoose post schema.

In validation, is the author with type UserSchema, required: true by default or false?

snippet 1

const PostSchema = new mongoose.Schema({
  ...
  author: UserSchema,
  ...
});

I want to have something like this where the author with type UserSchema is required: false

snippet 2

const PostSchema = new mongoose.Schema({
  author: {
    type: UserSchema,
    required: false
  }
});

Is there any other way to do something like this, schema property type another schema to be required true or false? --new to mongoose

Upvotes: 1

Views: 478

Answers (1)

nuxer
nuxer

Reputation: 478

This is very very old but I was working on something similar and I tested it myself and this indeed does work!

Upvotes: 1

Related Questions