Skip to content

Conversation

Kimxxunu
Copy link

Allow passing a RegExp to ApiProperty({ pattern }). The decorator normalizes the RegExp to an
OpenAPI-compatible string by stripping slashes and flags (e.g., /^\w+$/gi^\\w+$). This keeps
the generated schema compliant with OpenAPI/JSON Schema where pattern must be a string.

  • Input DX: pattern?: string | RegExp (options only)
  • Output schema: pattern is always a string
  • No breaking changes
  • Added unit tests (5 cases) for normalization and immutability

Closes #3374

PR Checklist

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

ApiProperty({ pattern }) only accepts a string. When developers want to reuse an app-wide RegExp,
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 accepts string | RegExp. When a RegExp is provided, the decorator
normalizes it to a plain string (no slashes/flags) before storing metadata, keeping the generated
OpenAPI schema valid.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

  • Implementation localized to the decorator options processing:

    • lib/decorators/api-property.decorator.ts: accept pattern?: string | RegExp at options level,
      normalize via RegExp#toString() and remove slashes/flags before emitting metadata.
    • Does not change OpenAPI types: schema pattern remains a string.
    • Does not mutate the caller’s options object.
  • Tests:

    • test/decorators/api-property.decorator.spec.ts
      • converts RegExp → string (removing slashes/flags)
      • keeps string pattern as-is
      • supports new RegExp(...)
      • preserves escaped slashes inside the body
      • does not mutate the original options object

@Kimxxunu Kimxxunu force-pushed the feat/api-property-regexp-pattern branch from e20bb4b to dd15257 Compare August 18, 2025 19:22
…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
@Kimxxunu Kimxxunu force-pushed the feat/api-property-regexp-pattern branch from 25dc94b to 0b24901 Compare August 18, 2025 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow to pass RegExp to @ApiProperty({ pattern })

1 participant