Skip to content

Commit 673037d

Browse files
committed
refactor: Resolve hadrcoded draft6 solutions
1 parent 38ebf94 commit 673037d

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

tests/JsonSchemaTestSuiteTest.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function testTestCaseValidatesCorrectly(
2727
string $testDescription,
2828
$schema,
2929
$data,
30+
int $checkMode,
3031
bool $expectedValidationResult,
3132
bool $optional
3233
): void {
@@ -37,7 +38,7 @@ public function testTestCaseValidatesCorrectly(
3738
$validator = new Validator(new Factory($schemaStorage));
3839

3940
try {
40-
$validator->validate($data, $schema, Constraint::CHECK_MODE_NORMAL | Constraint::CHECK_MODE_STRICT);
41+
$validator->validate($data, $schema, $checkMode);
4142
} catch (\Exception $e) {
4243
if ($optional) {
4344
$this->markTestSkipped('Optional test case throws exception during validate() invocation: "' . $e->getMessage() . '"');
@@ -63,10 +64,11 @@ public function casesDataProvider(): \Generator
6364
$drafts = array_filter(glob($testDir . '/*'), static function (string $filename) {
6465
return is_dir($filename);
6566
});
66-
$skippedDrafts = ['draft3', 'draft4', 'draft7', 'draft2019-09', 'draft2020-12', 'draft-next', 'latest'];
67+
$skippedDrafts = ['draft7', 'draft2019-09', 'draft2020-12', 'draft-next', 'latest'];
6768

6869
foreach ($drafts as $draft) {
69-
if (in_array(basename($draft), $skippedDrafts, true)) {
70+
$baseDraftName = basename($draft);
71+
if (in_array($baseDraftName, $skippedDrafts, true)) {
7072
continue;
7173
}
7274

@@ -82,8 +84,10 @@ function ($file) {
8284
foreach ($files as $file) {
8385
$contents = json_decode(file_get_contents($file->getPathname()), false);
8486
foreach ($contents as $testCase) {
85-
if (is_object($testCase->schema)) {
86-
$testCase->schema->{'$schema'} = 'http://json-schema.org/draft-06/schema#'; // Hardcode $schema property in schema
87+
// Since draft6 can only be validated using the strict check mode we need to ensure the $schema
88+
// property is set in the test schema
89+
if ($baseDraftName === 'draft6' && is_object($testCase->schema)) {
90+
$testCase->schema->{'$schema'} = 'http://json-schema.org/draft-06/schema#';
8791
}
8892
foreach ($testCase->tests as $test) {
8993
$name = sprintf(
@@ -105,6 +109,7 @@ function ($file) {
105109
'testDescription' => $test->description,
106110
'schema' => $testCase->schema,
107111
'data' => $test->data,
112+
'checkMode' => $this->getCheckModeForDraft($baseDraftName),
108113
'expectedValidationResult' => $test->valid,
109114
'optional' => str_contains($file->getPathname(), '/optional/')
110115
];
@@ -168,4 +173,17 @@ private function is32Bit(): bool
168173
{
169174
return PHP_INT_SIZE === 4;
170175
}
176+
177+
/**
178+
* @phpstan-return int-mask-of<Validator::ERROR_*>
179+
*/
180+
private function getCheckModeForDraft(string $draft): int
181+
{
182+
switch ($draft) {
183+
case 'draft6':
184+
return Constraint::CHECK_MODE_NORMAL | Constraint::CHECK_MODE_STRICT;
185+
default:
186+
return Constraint::CHECK_MODE_NORMAL;
187+
}
188+
}
171189
}

0 commit comments

Comments
 (0)