fix(schema): Add enumValues property to Number enum for consistency with String enum #15824
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds the
enumValuesproperty toSchemaNumber.prototype.enum()to match the behavior ofSchemaString.prototype.enum(). This fixes an API inconsistency where users could accessschema.path('field').enumValuesfor String enums but not for Number enums.Problem → Root Cause → Solution
Problem:
SchemaNumber.prototype.enum()does not expose anenumValuesproperty on the schema type instance, whileSchemaString.prototype.enum()does. This inconsistency prevents users from accessing enum values for Number schema types.Root Cause:
In
/lib/schema/number.js:SchemaNumberconstructor doesn't initializethis.enumValues = [](String enum does this on line 29 ofstring.js).enum()method stores enum values only in the validator object, not as a persistent property on the schema type instance.false/undefined, unlike String enum.Solution:
this.enumValues = []in theSchemaNumberconstructor.enum()to maintainthis.enumValuesby casting and pushing values (matching String enum behavior).this.enumValuesin the validator closure instead of the localvaluesvariable.false/undefined(matching String enum).Before vs After Behavior
Before:
After:
Test Cases Added
Added comprehensive test in
/test/schema.validation.test.js:it('number enum', async function() { ... })The test verifies:
enumValuesproperty exists and is accessibleenum()multiple timesAll tests pass:
✔ number enum (56ms) ✔ string enum (55ms) # Existing test still passesWhy This Should Be Merged
Files Changed
/lib/schema/number.js- AddedenumValuesinitialization and maintenance/test/schema.validation.test.js- Added comprehensive test for Number enumenumValuespropertyReviewer Checklist