json-schema icon indicating copy to clipboard operation
json-schema copied to clipboard

ErrorException: StringConstraint.php:42 -- preg_match(): Unknown modifier 'r'

Open claudiofreitas opened this issue 6 years ago • 2 comments

Hello,

I have a schema.json like below, and when I try to validate my json (also below) I'm receiving the Exception from the title.

schema.json:

{
    "$schema": "http://json-schema.org/schema#",
    "type": "string",
    "pattern": "^\\#ref\\(.+\\)$"
}

json:

"ref"

Investigating the StringConstraint.php, on line 42 it has this code:

if (isset($schema->pattern) && !preg_match('#' . str_replace('#', '\\#', $schema->pattern) . '#u', $element)) {

which is replacing # by \\# and concatenating everything with #-prefix and #u-suffix.

Am I going something wrong or is it a bug?

claudiofreitas avatar Aug 07 '18 06:08 claudiofreitas

Yes, this is a bug.

The logic is supposed to replace # with \#, and then wrap with #<pattern>#u (i.e. #-delimited pattern with the unicode modifier). This behavior is deliberate, but the bug is that it seems to be still interpreting the # from your pattern as a delimiter, rather than an escaped literal.

erayd avatar Aug 07 '18 10:08 erayd

@erayd I see. If I have some time, I will try to investigate further and see if I can come with some fix and PR it.

Thank you very much!

claudiofreitas avatar Aug 09 '18 10:08 claudiofreitas