feat(decorators): accept RegExp in ApiProperty({ pattern }) while keeping schema pattern string#3543
Open
Kimxxunu wants to merge 1 commit intonestjs:masterfrom
Open
Conversation
e20bb4b to
dd15257
Compare
…ping schema pattern string
Allow passing a RegExp to ApiProperty({ pattern }). The decorator normalizes the RegExp to an
OpenAPI-compatible string (using .source), so the generated schema stays compliant where `pattern`
must be a string.
- Input DX: `pattern?: string | RegExp` (options only)
- Output schema: `pattern` is always a string
- No breaking changes
- Add unit tests (5) for normalization and immutability
Closes nestjs#3374
25dc94b to
0b24901
Compare
1 task
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.
Allow passing a RegExp to
ApiProperty({ pattern }). The decorator normalizes the RegExp to anOpenAPI-compatible string by stripping slashes and flags (e.g.,
/^\w+$/gi→^\\w+$). This keepsthe generated schema compliant with OpenAPI/JSON Schema where
patternmust be a string.pattern?: string | RegExp(options only)patternis always a stringCloses #3374
PR Checklist
PR Type
What kind of change does this PR introduce?
What is the current behavior?
ApiProperty({ pattern })only accepts a string. When developers want to reuse an app-wideRegExp,they must convert it to a string and manually strip the leading/trailing slashes and ignore flags,
which is repetitive and error-prone.
Issue Number: #3374
What is the new behavior?
ApiProperty({ pattern })now acceptsstring | RegExp. When aRegExpis provided, the decoratornormalizes it to a plain string (no slashes/flags) before storing metadata, keeping the generated
OpenAPI schema valid.
Does this PR introduce a breaking change?
Other information
Implementation localized to the decorator options processing:
lib/decorators/api-property.decorator.ts: acceptpattern?: string | RegExpat options level,normalize via
RegExp#toString()and remove slashes/flags before emitting metadata.patternremains a string.Tests:
test/decorators/api-property.decorator.spec.tsnew RegExp(...)