Mongoose make Array required

AFAIK, you need to set the type to Array and add a custom validator to make sure that each entry is a String:

tags : {
  type     : Array,
  required : true,
  validate : {
    validator : function(array) {
      return array.every((v) => typeof v === 'string');
    }
  }
}

OK I tried a new method and it seems to work just fine for mongoose ^5.11.15 I'm not really sure if it's a proper answer in terms of clean code but as far as the functionality which is to make an array of Numbers/Strings required (meaning it won't accept an empty array) it works OK so it's as follows

size: [{
        type: Number,
        required: true
}],

instead of

size: {
        type: [Number],
        required: true
},

Instead of defining the type as an array of numbers/strings, I defined the size to be an array of numbers and then the required attribute works as it should it doesn't accept an empty array and raises an error to illustrate that. again I'm not really sure if this is the best way to define a required array but as far as the functionality it works just fine


Mongoose 5.x

https://mongoosejs.com/docs/migrating_to_5.html#array-required

tags: {
    type: [String],
    validate: v => Array.isArray(v) && v.length > 0,
}

Mongoose 4.x

One-liner would be:

tags: {type: [String], required: true}

SchemaTypes