fix: reject enum member names in @IsEnum for numeric enums#2685
Open
spokodev wants to merge 1 commit into
Open
fix: reject enum member names in @IsEnum for numeric enums#2685spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
TypeScript compiles a numeric enum to a bidirectional map, so
Object.keys(entity).map(k => entity[k]) included the reverse-map member
names in the accepted set. @IsEnum(NumericEnum) then accepted the member
NAMES (e.g. 'Up' for { Up = 1 }) as valid, letting a string through where
a number is expected. The default error message already lists only the
real values, built from validEnumValues, so the validator accepted values
its own message declared invalid.
Build the accepted set from validEnumValues, the same helper the decorator
already uses for the message. Fixes typestack#1060.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
@IsEnumandisEnum()accept the enum member NAME as a valid value for numeric and default-indexed enums. TypeScript compiles a numeric enum to a bidirectional map, soObject.keys(entity).map(k => entity[k])includes the reverse-map names in the accepted set.For
enum Direction { Up = 1, Down = 2 }the only valid values are1and2, but@IsEnum(Direction)also accepts the strings"Up"and"Down". A request body{ direction: "Up" }passes validation and reaches the handler as a string where the code expects a number. The library's own default error message already lists only1, 2(it is built fromvalidEnumValues), so the validator accepts values its own message declares invalid.This is the bug tracked in #1060 (open, labeled
type: fix).Fix
isEnumnow builds its accepted set fromvalidEnumValues, the same helper the@IsEnumdecorator already uses for the error message, which keeps only the entries whose key is non-numeric:Genuine values still match (runtime
includesuses strict equality, so numbers match numbers and strings match strings); the reverse-map names and numeric key strings no longer do.Added regression tests to the existing
IsEnumdescribe block: the member name and the reverse-mapped numeric key are rejected for numeric and default-indexed enums, genuine values still pass, and the decorator fails when a member name is passed. The full suite (810) stays green.This is distinct from #2099, which changes how companion-namespace function values appear in the message, not the accepted set.