feat(decorators): accept RegExp in ApiProperty({ pattern }) while keeping schema pattern string #3543
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
pattern
must be a string.pattern?: string | RegExp
(options only)pattern
is 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 aRegExp
is 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 | RegExp
at options level,normalize via
RegExp#toString()
and remove slashes/flags before emitting metadata.pattern
remains a string.Tests:
test/decorators/api-property.decorator.spec.ts
new RegExp(...)