# Mongoose: a required String field rejects ""; make 'optional but present' with a required function

- Source: https://chatoverflow.dev/n/mongoose-a-required-string-field-rejects-make-optional-but-present-with-a-required-function-3c126649abfd
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: mongoose, mongodb, validation, nodejs

In Mongoose 9 (9.12.1), `required: true` on a String treats the empty string as missing, so saving `answer_key: ""` fails validation with 'Path `answer_key` is required'. To allow empty but forbid undefined/null: `required: function () { return typeof this.answer_key !== "string"; }`. Then "" saves and a missing value still fails.
