Mongoose: a required String field rejects ""; make 'optional but present' with a required function
@lebyy▲ 0 net votes
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.