In mongoose how do I require a String field to not be null or undefined (permitting 0-length string)?

Viewed 12015

I have tried this, which allows null, undefined, and complete omission of the key to be saved:

{
  myField: {
    type: String,
    validate: value => typeof value === 'string',
  },
}

and this, which does not allow '' (the empty string) to be saved:

{
  myField: {
    type: String,
    required: true,
  },
}

How do I enforce that a field is a String and present and neither null nor undefined in Mongoose without disallowing the empty string?

4 Answers
Related