From c5079685bf1a99d582ceee022d6c787754b08596 Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Tue, 18 Jul 2023 11:48:08 -0400 Subject: [PATCH 1/3] allow null values for enum --- lib/schema/string.js | 2 +- test/schema.validation.test.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/schema/string.js b/lib/schema/string.js index bf1b6d85749..cb8b866c115 100644 --- a/lib/schema/string.js +++ b/lib/schema/string.js @@ -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, diff --git a/test/schema.validation.test.js b/test/schema.validation.test.js index 22fab457bd1..24f7d8d9125 100644 --- a/test/schema.validation.test.js +++ b/test/schema.validation.test.js @@ -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', null] + } + }); + 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: [{ From 886c941047e413b50def247377fbaaf77e65b0a0 Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Tue, 18 Jul 2023 11:49:14 -0400 Subject: [PATCH 2/3] fix:lint --- test/schema.validation.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/schema.validation.test.js b/test/schema.validation.test.js index 24f7d8d9125..73412694e2b 100644 --- a/test/schema.validation.test.js +++ b/test/schema.validation.test.js @@ -837,7 +837,7 @@ describe('schema', function() { 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) + assert.equal(err, null); }); it('should allow an array of subdocuments with enums (gh-3521)', async function() { From 760c18724b654b184946e540fc8f7cbf0e6024f7 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 18 Jul 2023 16:39:31 -0400 Subject: [PATCH 3/3] Update schema.validation.test.js --- test/schema.validation.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/schema.validation.test.js b/test/schema.validation.test.js index 73412694e2b..e467f6ea088 100644 --- a/test/schema.validation.test.js +++ b/test/schema.validation.test.js @@ -831,7 +831,7 @@ describe('schema', function() { const testSchema = new Schema({ name: { type: String, - enum: ['test', null] + enum: ['test'] } }); const Test = mongoose.model('allow-null' + random(), testSchema);