Skip to content

Commit 32a72c3

Browse files
committed
fix: fix optional regex testcase for ecmascript support
1 parent 81493a2 commit 32a72c3

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

src/JsonSchema/Constraints/Drafts/Draft06/AdditionalPropertiesConstraint.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
4747

4848
foreach ($additionalProperties as $key => $_) {
4949
foreach ($patterns as $pattern) {
50-
if (preg_match("/{$pattern}/", (string) $key)) {
50+
if (preg_match($this->createPregMatchPattern($pattern), (string) $key)) {
5151
unset($additionalProperties[$key]);
5252
break;
5353
}
@@ -69,4 +69,26 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
6969
$this->addError(ConstraintError::ADDITIONAL_PROPERTIES(), $path, ['found' => $additionalPropertiesValue]);
7070
}
7171
}
72+
73+
private function createPregMatchPattern(string $pattern): string
74+
{
75+
$replacements = [
76+
// '\D' => '[^0-9]',
77+
// '\d' => '[0-9]',
78+
'\p{digit}' => '\p{Nd}',
79+
// '\w' => '[A-Za-z0-9_]',
80+
// '\W' => '[^A-Za-z0-9_]',
81+
// '\s' => '[\s\x{200B}]' // Explicitly include zero width white space,
82+
'\p{Letter}' => '\p{L}', // Map ECMA long property name to PHP (PCRE) Unicode property abbreviations
83+
84+
];
85+
//
86+
$pattern = str_replace(
87+
array_keys($replacements),
88+
array_values($replacements),
89+
$pattern
90+
);
91+
92+
return '/' . str_replace('/', '\/', $pattern) . '/u';
93+
}
7294
}

src/JsonSchema/Constraints/Drafts/Draft06/PatternConstraint.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,32 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
3232
return;
3333
}
3434

35-
if (preg_match('/' . str_replace('/', '\/', $schema->pattern) . '/', $value) === 1) {
35+
$matchPattern = $this->createPregMatchPattern($schema->pattern);
36+
if (preg_match($matchPattern, $value) === 1) {
3637
return;
3738
}
3839

3940
$this->addError(ConstraintError::PATTERN(), $path, ['found' => $value, 'pattern' => $schema->pattern]);
4041
}
42+
43+
private function createPregMatchPattern(string $pattern): string
44+
{
45+
$replacements = [
46+
'\D' => '[^0-9]',
47+
'\d' => '[0-9]',
48+
'\p{digit}' => '[0-9]',
49+
'\w' => '[A-Za-z0-9_]',
50+
'\W' => '[^A-Za-z0-9_]',
51+
'\s' => '[\s\x{200B}]', // Explicitly include zero width white space
52+
'\p{Letter}' => '\p{L}', // Map ECMA long property name to PHP (PCRE) Unicode property abbreviations
53+
];
54+
55+
$pattern = str_replace(
56+
array_keys($replacements),
57+
array_values($replacements),
58+
$pattern
59+
);
60+
61+
return '/' . str_replace('/', '\/', $pattern) . '/u';
62+
}
4163
}

src/JsonSchema/Constraints/Drafts/Draft06/PatternPropertiesConstraint.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
3535

3636
foreach ($properties as $propertyName => $propertyValue) {
3737
foreach ($schema->patternProperties as $patternPropertyRegex => $patternPropertySchema) {
38-
if (preg_match('/' . str_replace('/', '\/', $patternPropertyRegex) . '/', (string) $propertyName)) {
38+
$matchPattern = $this->createPregMatchPattern($patternPropertyRegex);
39+
if (preg_match($matchPattern, (string) $propertyName)) {
3940
$schemaConstraint = $this->factory->createInstanceFor('schema');
4041
$schemaConstraint->check($propertyValue, $patternPropertySchema, $path, $i);
4142
if ($schemaConstraint->isValid()) {
@@ -47,4 +48,25 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
4748
}
4849
}
4950
}
51+
52+
private function createPregMatchPattern(string $pattern): string
53+
{
54+
$replacements = [
55+
// '\D' => '[^0-9]',
56+
'\d' => '[0-9]',
57+
'\p{digit}' => '[0-9]',
58+
// '\w' => '[A-Za-z0-9_]',
59+
// '\W' => '[^A-Za-z0-9_]',
60+
// '\s' => '[\s\x{200B}]' // Explicitly include zero width white space
61+
'\p{Letter}' => '\p{L}', // Map ECMA long property name to PHP (PCRE) Unicode property abbreviations
62+
];
63+
//
64+
$pattern = str_replace(
65+
array_keys($replacements),
66+
array_values($replacements),
67+
$pattern
68+
);
69+
70+
return '/' . str_replace('/', '\/', $pattern) . '/u';
71+
}
5072
}

tests/JsonSchemaTestSuiteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testTestCaseValidatesCorrectly(
4040
$validator->validate($data, $schema, Constraint::CHECK_MODE_NORMAL | Constraint::CHECK_MODE_STRICT);
4141
} catch (\Exception $e) {
4242
if ($optional) {
43-
$this->markTestSkipped('Optional test case would during validate() invocation');
43+
$this->markTestSkipped('Optional test case throws exception during validate() invocation');
4444
}
4545

4646
throw $e;

0 commit comments

Comments
 (0)