Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow null values for string enum #13620

Merged
merged 3 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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