Skip to content

fix(schema): clear enumValues when enum validator is removed with enum(false) - #16631

Merged
vkarpov15 merged 3 commits into
Automattic:masterfrom
Enesuygurs:fix/enum-false-clear-enum-values
Nov 2, 2026
Merged

vkarpov15 merged 3 commits into
Automattic:masterfrom
Enesuygurs:fix/enum-false-clear-enum-values

Conversation

@Enesuygurs

Copy link
Copy Markdown
Contributor

Summary

Calling enum(false) (or enum(undefined)) on a String or Number path removes the enum validator but leaves the old values in schemaType.enumValues. This causes two problems:

  • toJSONSchema() still emits enum: [...] for that path, so the generated schema rejects values Mongoose itself now accepts.
  • enum() appends to enumValues, so re-enabling the enum brings the old values back. After enum(false) and enum('c'), the validator still allows 'a' and 'b'.

This PR resets enumValues in the branch of SchemaString.prototype.enum() and SchemaNumber.prototype.enum() that removes the validator. Calling enum() with values still appends, as before.

Examples

const schema = new Schema({ status: { type: String, enum: ['a', 'b'] } });
schema.path('status').enum(false);

schema.path('status').enumValues; // ['a', 'b'] before, [] now
schema.toJSONSchema().properties.status; // still had enum: ['a', 'b', null] before

Added a test in test/schema.validation.test.js that fails on master and passes with this change.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the changes that clear stale enum values when enum validation is disabled.

No open issues 🎉

What changed in this PR

Fixes stale enum values on String and Number schema paths after enum(false) or enum(undefined), keeping JSON Schema output and subsequent enum validation consistent.

Changes:

  • Clears enumValues when removing an enum validator.
  • Adds regression coverage for clearing values, removing JSON Schema enum restrictions, and re-enabling validation without restoring old values.
File Description
test/​schema.validation.test.js Tests enum removal and re-enabling for String and Number paths.
lib/​schema/​string.js Clears stored enum values when disabling String enum validation.
lib/​schema/​number.js Clears stored enum values when disabling Number enum validation.

Review effort: Balanced

Added a test to verify that enum values remain cleared after cloning the schema.

@vkarpov15 vkarpov15 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor comment and I also added a failing test which shows you also need to clear options.enum

Comment thread lib/schema/number.js Outdated
@@ -324,6 +324,7 @@ SchemaNumber.prototype.enum = function(values, message) {
}

if (values === void 0 || values === false) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should update this to values == null so enum(null) works as well - more consistent because Mongoose typically makes undefined and null interchangeable.

@Enesuygurs

Copy link
Copy Markdown
Contributor Author

Thanks for the review and for adding the clone test! Both are addressed in 09ca49f:

  • enum() now checks values == null, so enum(null) removes the validator just like enum(false). I made the same change in SchemaString, so the two types stay consistent. There's a new test for enum(null) on both.
  • Removing the enum now also deletes options.enum. clone() copies the options and passes them to the new schematype's constructor, so the enum came back from there. Your clone test passes now.

The schema test files (test/schema*.test.js, test/schematype*.test.js) pass locally, and eslint is clean on the changed files.

@vkarpov15 vkarpov15 added this to the 9.11.5 milestone Nov 2, 2026

@vkarpov15 vkarpov15 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@vkarpov15
vkarpov15 merged commit 3bb3c71 into Automattic:master Nov 2, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants