Repository navigation
fix(schema): clear enumValues when enum validator is removed with enum(false) - #16631
Conversation
There was a problem hiding this comment.
🟢 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
enumValueswhen 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
left a comment
There was a problem hiding this comment.
One minor comment and I also added a failing test which shows you also need to clear options.enum
| @@ -324,6 +324,7 @@ SchemaNumber.prototype.enum = function(values, message) { | |||
| } | |||
|
|
|||
| if (values === void 0 || values === false) { | |||
There was a problem hiding this comment.
We should update this to values == null so enum(null) works as well - more consistent because Mongoose typically makes undefined and null interchangeable.
… so clone() keeps it removed
|
Thanks for the review and for adding the clone test! Both are addressed in 09ca49f:
The schema test files ( |
Summary
Calling
enum(false)(orenum(undefined)) on a String or Number path removes the enum validator but leaves the old values inschemaType.enumValues. This causes two problems:toJSONSchema()still emitsenum: [...]for that path, so the generated schema rejects values Mongoose itself now accepts.enum()appends toenumValues, so re-enabling the enum brings the old values back. Afterenum(false)andenum('c'), the validator still allows'a'and'b'.This PR resets
enumValuesin the branch ofSchemaString.prototype.enum()andSchemaNumber.prototype.enum()that removes the validator. Callingenum()with values still appends, as before.Examples
Added a test in
test/schema.validation.test.jsthat fails on master and passes with this change.