Skip to content

Omit regex flags from emitted patterns - #401

Open
chenxin-yan wants to merge 1 commit into
asteasolutions:masterfrom
chenxin-yan:fix/regex-pattern-flags
Open

chenxin-yan wants to merge 1 commit into
asteasolutions:masterfrom
chenxin-yan:fix/regex-pattern-flags

Conversation

@chenxin-yan

Copy link
Copy Markdown

Fixes #400.

A .regex() check whose RegExp has flags was emitted with the flags inside pattern, because the pattern came from RegExp#toString() with only the leading and trailing / stripped. /^\p{Lu}+$/u became "^\\p{Lu}+$/u", and a client enforcing that pattern rejects every valid value.

This uses RegExp#source, which never includes the delimiters or the flags. Patterns without flags are emitted unchanged, as the existing test covers.

Flags that change matching, such as i, m and s, can't be expressed in a JSON Schema pattern and are now omitted rather than emitted as literal text. I've left handling them differently out of scope.

Tests: added a spec for a u-flag regex; the full jest suite, test:types and prettier --check pass.

RegExp#toString() appends the flags after the closing slash, so a regex with flags such as /u was emitted with the flags inside the pattern. Use RegExp#source instead. Fixes asteasolutions#400.
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.

Regex flags are left inside the emitted pattern (e.g. /u → \^...$/u\)

1 participant