Skip to content

$ in a pattern also matches before a final newline, so "5\n" passes "^[0-9]+$" #1012

Backlog

Description

@dbuytaert

In ECMA-262, $ without the m flag matches only at the end of the input.

In PCRE, $ also matches before a final newline, unless the D modifier is set.

Steps to reproduce

$schema = (object) ['type' => 'string', 'pattern' => '^[0-9]+$'];
$validator = new JsonSchema\Validator();
$validator->validate($value, $schema);
var_dump($validator->isValid());
Value Expected 6.14.0
"5" valid valid
"5\n" invalid valid ❌

JavaScript agrees with the expected column: /^[0-9]+$/u.test("5\n") is false. On main, EcmaPatternConverter::toPcre('^[0-9]+$') returns /^[0-9]+$/u, which matches "5\n" too.

Proposed fix

Add the D modifier: '/' . $regex . '/uD'.

Found while working on Drupal: see https://www.drupal.org/node/3643182 for context.

Disclaimer: I generated the example with the help of Claude Code.

Activity

  1. github-actions commented on Dec 4, 2026

    @github-actions
    Contributor

    🚀 @dbuytaert, thanks for your contribution! Every idea, bug report, and discussion helps make this project better. Your input is invaluable, and we appreciate the time you took to share it. A maintainer will review it soon—stay awesome! 💡✨

    ⭐ Enjoying contributing? Star the project! ⭐ Your contributions help this project grow, and we'd love your support in another way too! If you find this repo helpful, consider leaving a star 🌟 on GitHub.

  2. harshbhx commented on Dec 5, 2026

    @harshbhx

    Hi, I can take this one. The fix is the D modifier on the regex built in EcmaPatternConverter::toPcre(), since every pattern / patternProperties check goes through it. I'll add tests at the converter level and for pattern and patternProperties in Draft-07 and Draft 2019-09. I already have it working on a branch, so I can open the PR today unless someone else is on it.

    The branch is ready on my fork: https://github.com/harshbhx/json-schema/tree/fix/1012-pattern-dollar-newline

  3. DannyvdSluijs commented on Dec 5, 2026

    @DannyvdSluijs
    Collaborator

    @dbuytaert thanks for the report. @harshbhx off course you can pick it up. Feel free to open the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions