Search code examples
node.jsmongodbmongooseschema

Make all fields required in Mongoose


Mongoose seems to default to make all fields not required. Is there any way to make all the fields required without changing each of:

Dimension = mongoose.Schema(
  name: String
  value: String
)

to

Dimension = mongoose.Schema(
  name:
    type: String
    required: true
  value: 
    type: String
    required: true
)

It'll get really ugly since I have a lot of these.


Solution

  • I ended up doing this:

    r_string = 
      type: String
      required: true 
    
    r_number = 
      type: Number
      required: true
    

    and on for the other data types.