Skip to content

Apply the same ECMA to PCRE pattern replacements everywhere - #943

Merged
DannyvdSluijs merged 5 commits into
jsonrainbow:mainfrom
Amoifr:fix-917-ecma-pcre-replacements
Oct 16, 2026
Merged

DannyvdSluijs merged 5 commits into
jsonrainbow:mainfrom
Amoifr:fix-917-ecma-pcre-replacements

Conversation

@Amoifr

@Amoifr Amoifr commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #917.

The issue asks for a decision on the commented-out replacements. Rather than pick one, here is what each line does, measured against PCRE on PHP 8.5.

Replacement Needed? Why
\d → [0-9] yes under /u, PCRE's \d matches U+0663 ٣; ECMA-262 defines \d as ASCII only
\D → [^0-9] yes the negation of the above
\w → [A-Za-z0-9_] yes under /u, PCRE's \w matches é; ECMA's does not
\W → [^A-Za-z0-9_] yes the negation
\p{digit} → … yes PCRE rejects \p{digit} outright, preg_match() returns false
\p{Letter} → \p{L} yes same, PCRE rejects the long name

So none of them is dead weight: each one exists because PCRE and ECMA disagree, and leaving it out changes what a schema means.

The bug it causes today

\d is commented out in AdditionalPropertiesConstraint but active in PatternPropertiesConstraint, and the two are consulted for the same property name. On main:

{"patternProperties": {"^\\d+$": {"type": "string"}}, "additionalProperties": false}

{"٣": "ok"} is accepted. Per ECMA \d is [0-9], so ٣ matches no pattern, is an additional property, and should be rejected. {"5": "ok"} is accepted either way. This is now covered by a test per draft, each of which fails without the change.

\p{digit}

The two mappings in the tree are not equivalent, so this was not just a cosmetic inconsistency. [0-9] drops non-ASCII digits, \p{Nd} keeps them, and \p{Nd} is what the ECMA property actually means. I converged on \p{Nd}, which is what AdditionalPropertiesConstraint already used.

One line I am not comfortable deciding for you

\s → [\s\x{200B}] widens \s beyond ECMA rather than narrowing it to match: U+200B is Cf, not Zs, so ECMA's \s does not include it. I kept the mapping exactly as PatternConstraint has it today, so pattern behaviour is unchanged, but propagating it to the other files does widen them. If the zero width space handling was added for a concrete reason, a comment saying so would settle it; if not, dropping it everywhere would be the more faithful reading. Happy to do either in a follow-up.

One parser instead of nine str_replace() calls

Review showed that a plain str_replace() breaks patterns it should not touch, and that pattern was already affected on main: [\w-] became [[A-Za-z0-9_]-], and [\d.], [^\d], an escaped backslash (\\d) and an escaped slash (a\/b) all failed to match.

The nine copies of createPregMatchPattern() are now a single JsonSchema\Tool\EcmaPatternConverter (marked @internal), which walks the pattern instead. Inside a character class a shorthand expands to ranges (\D and \W become explicit code point ranges), escapes are kept as they are, and only an unescaped / is escaped for the delimiter. EcmaPatternConverterTest covers 20 cases, 8 of which fail on the old str_replace().

Full suite green at 9850 tests on PHP 8.5, including the official JSON-Schema-Test-Suite. PHPStan clean, php-cs-fixer clean on the touched files.

@DannyvdSluijs DannyvdSluijs left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, it took me some time to get to the bigger PR's but I'm trying to make some time for them now. I did have some remarks though, hope you can process them?
This changes would resolve some test that are currently skipped. Can you also enable them as well to block future regression?

draft6 (lines 359–362)
359: '[draft6/optional/ecmascript-regex.json]: \d in patternProperties matches [0-9], not unicode digits: non-ascii digits (BENGALI DIGIT FOUR, BENGALI DIGIT TWO) is expected to be invalid',
360: '[draft6/optional/ecmascript-regex.json]: \w in patternProperties matches [A-Za-z0-9_], not unicode letters: literal unicode character in json string is expected to be invalid',
361: '[draft6/optional/ecmascript-regex.json]: \w in patternProperties matches [A-Za-z0-9_], not unicode letters: unicode character in hex format in string is expected to be invalid',
362: '[draft6/optional/ecmascript-regex.json]: pattern with non-ASCII digits: non-ascii digits (BENGALI DIGIT FOUR, BENGALI DIGIT TWO) is expected to be valid',

draft7 (lines 365–368)
365: '[draft7/optional/ecmascript-regex.json]: \d in patternProperties matches [0-9], not unicode digits: non-ascii digits (BENGALI DIGIT FOUR, BENGALI DIGIT TWO) is expected to be invalid',
366: '[draft7/optional/ecmascript-regex.json]: \w in patternProperties matches [A-Za-z0-9_], not unicode letters: literal unicode character in json string is expected to be invalid',
367: '[draft7/optional/ecmascript-regex.json]: \w in patternProperties matches [A-Za-z0-9_], not unicode letters: unicode character in hex format in string is expected to be invalid',
368: '[draft7/optional/ecmascript-regex.json]: pattern with non-ASCII digits: non-ascii digits (BENGALI DIGIT FOUR, BENGALI DIGIT TWO) is expected to be valid',

draft2019-09 (lines 374–377)
374: '[draft2019-09/optional/ecmascript-regex.json]: pattern with non-ASCII digits: non-ascii digits (BENGALI DIGIT FOUR, BENGALI DIGIT TWO) is expected to be valid',
375: '[draft2019-09/optional/ecmascript-regex.json]: \w in patternProperties matches [A-Za-z0-9_], not unicode letters: literal unicode character in json string is expected to be invalid',
376: '[draft2019-09/optional/ecmascript-regex.json]: \w in patternProperties matches [A-Za-z0-9_], not unicode letters: unicode character in hex format in string is expected to be invalid',
377: '[draft2019-09/optional/ecmascript-regex.json]: \d in patternProperties matches [0-9], not unicode digits: non-ascii digits (BENGALI DIGIT FOUR, BENGALI DIGIT TWO) is expected to be invalid',

Amoifr and others added 5 commits October 16, 2026 15:52
The nine copies of createPregMatchPattern() are removed in favour of
EcmaPatternConverter, which walks the pattern instead of replacing substrings:

- a shorthand class inside a bracket expression is expanded into ranges, so
  "[\w-]" no longer becomes the broken "[[A-Za-z0-9_]-]";
- an escaped backslash is no longer read as the start of a shorthand class;
- an escaped slash is no longer escaped a second time.
…9-09

The EcmaPatternConverter now correctly handles \d, \w and non-ASCII
digit patterns, so the 12 corresponding tests can be enabled.

Draft4 still uses raw PCRE without conversion, so its tests remain
skipped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DannyvdSluijs
DannyvdSluijs force-pushed the fix-917-ecma-pcre-replacements branch from 757a7bc to 420bf32 Compare October 16, 2026 13:52
@DannyvdSluijs
DannyvdSluijs merged commit d780c22 into jsonrainbow:main Oct 16, 2026
18 checks passed
github-actions Bot added a commit that referenced this pull request Oct 16, 2026
Roman3349 added a commit to Roman3349/json-schema that referenced this pull request Oct 17, 2026
Bring Draft 2020-12 in line with the changes merged into main since the
branch was created:

- Support unevaluatedProperties (jsonrainbow#931). $dynamicRef is not followed,
  the same way $recursiveRef is not in Draft 2019-09.
- Use EcmaPatternConverter for pattern, patternProperties,
  additionalProperties and unevaluatedProperties (jsonrainbow#943).
- Add error path and ECMA pattern tests (jsonrainbow#967, jsonrainbow#943).
- Drop skip list entries that now pass, including the bignum float
  comparisons fixed by jsonrainbow#982.

Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
@Amoifr
Amoifr deleted the fix-917-ecma-pcre-replacements branch October 18, 2026 10:01
Roman3349 added a commit to Roman3349/json-schema that referenced this pull request Oct 24, 2026
Bring Draft 2020-12 in line with the changes merged into main since the
branch was created:

- Support unevaluatedProperties (jsonrainbow#931). $dynamicRef is not followed,
  the same way $recursiveRef is not in Draft 2019-09.
- Use EcmaPatternConverter for pattern, patternProperties,
  additionalProperties and unevaluatedProperties (jsonrainbow#943).
- Add error path and ECMA pattern tests (jsonrainbow#967, jsonrainbow#943).
- Drop skip list entries that now pass, including the bignum float
  comparisons fixed by jsonrainbow#982.

Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
@Roman3349 Roman3349 mentioned this pull request Oct 24, 2026
7 of 12 tasks
Roman3349 added a commit to Roman3349/json-schema that referenced this pull request Oct 24, 2026
Bring Draft 2020-12 in line with the changes merged into main since the
branch was created:

- Support unevaluatedProperties (jsonrainbow#931). $dynamicRef is not followed,
  the same way $recursiveRef is not in Draft 2019-09.
- Use EcmaPatternConverter for pattern, patternProperties,
  additionalProperties and unevaluatedProperties (jsonrainbow#943).
- Add error path and ECMA pattern tests (jsonrainbow#967, jsonrainbow#943).
- Reject a trailing newline in idn-hostname (jsonrainbow#990).
- Drop skip list entries that now pass, including the bignum float
  comparisons fixed by jsonrainbow#982.

Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent and incomplete ECMA-to-PCRE pattern replacements in createPregMatchPattern

2 participants