Skip to content

Commit

Permalink
Merge pull request #13620 from Automattic/IslandRhythms/gh-3044
Browse files Browse the repository at this point in the history
allow null values for string enum
  • Loading branch information
vkarpov15 committed Jul 18, 2023
2 parents 1a69e7c + 760c187 commit fffa85d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/schema/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ SchemaString.prototype.enum = function() {

const vals = this.enumValues;
this.enumValidator = function(v) {
return undefined === v || ~vals.indexOf(v);
return null == v || ~vals.indexOf(v);
};
this.validators.push({
validator: this.enumValidator,
Expand Down
13 changes: 13 additions & 0 deletions test/schema.validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,19 @@ describe('schema', function() {
}
});

it('should allow null values for enum gh-3044', async function() {
const testSchema = new Schema({
name: {
type: String,
enum: ['test']
}
});
const Test = mongoose.model('allow-null' + random(), testSchema);
const a = new Test({ name: null });
const err = await a.validate().then(() => null, err => err);
assert.equal(err, null);
});

it('should allow an array of subdocuments with enums (gh-3521)', async function() {
const coolSchema = new Schema({
votes: [{
Expand Down

0 comments on commit fffa85d

Please sign in to comment.