Problem/Motivation

The decimal check in PrimitiveTypeConstraintValidator ends its regular expression with $. In PHP, $ also matches before a final newline, so Drupal accepts "5\n" as a decimal.

Proposed resolution

  • In PrimitiveTypeConstraintValidator, end the decimal regular expression with \z instead of $.
  • Add decimal rows to PrimitiveTypeConstraintValidatorTest::provideTestValidate(), which has none today, including "5\n".

Issue fork drupal-3644214

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

dries created an issue. See original summary.

dries’s picture

I opened the MR and added a test that reproduces the bug. As expected, the tests failed.

dries’s picture

I already pushed a fix as it was simple: the decimal regex in PrimitiveTypeConstraintValidator now ends with \z instead of $.

https://www.php.net/manual/en/regexp.reference.escape.php explains that \z matches only at the end of the string, while $ also matches before a final newline.

That change did the trick. Setting to 'Needs review'.

ronaldtebrake’s picture

Assigned: Unassigned » ronaldtebrake
ronaldtebrake’s picture

Assigned: ronaldtebrake » Unassigned
Status: Active » Reviewed & tested by the community

Looks good to me, this approach matches what I found while testing #3643182: Decimal JSON Schema accepts any string

AI disclosure: I used Claude Code (Opus 5.5) to help review the MR and run the tests.