Skip to content

fix: use regex.source for pattern to handle regex flags correctly#108

Open
peekg wants to merge 1 commit into
Twipped:masterfrom
peekg:fix/regex-pattern-flags
Open

fix: use regex.source for pattern to handle regex flags correctly#108
peekg wants to merge 1 commit into
Twipped:masterfrom
peekg:fix/regex-pattern-flags

Conversation

@peekg

@peekg peekg commented Jun 23, 2026

Copy link
Copy Markdown

Description

Fixes regex pattern extraction to properly handle regex flags by using the .source property instead of manually slicing the string representation.

Problem

The previous implementation used .toString().slice(1, -1) to extract the pattern from a regex, which incorrectly included flags in the output when they were present (e.g., /^A$/u would produce ^A$/ instead of ^A$).

Solution

Use the standard .source property which returns only the pattern text without delimiters or flags.

Changes

  • Replace pattern.args.regex.toString().slice(1, -1) with pattern.args.regex.source
  • Add test case for regex with flags to prevent regression

Testing

Added test case verifying that joi.string().regex(/^A$/u) correctly produces pattern: '^A$' in the swagger output.

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.

1 participant