1
votes

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

1
btw is this possible anyone?Vadamadafaka
I think you may need to create a custom schema type to do that: mongoosejs.com/docs/customschematypes.htmlAndrew Cameron

1 Answers

1
votes

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