Skip to content

Commit 4d83fbf

Browse files
committed
fix: correctly set the schema ID when passing it as assoc array
1 parent 3772314 commit 4d83fbf

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/JsonSchema/Validator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public function validate(&$value, $schema = null, $checkMode = null)
6060
// add provided schema to SchemaStorage with internal URI to allow internal $ref resolution
6161
if (is_object($schema) && property_exists($schema, 'id')) {
6262
$schemaURI = $schema->id;
63+
} elseif (is_array($schema) && array_key_exists('id', $schema)) {
64+
$schemaURI = $schema['id'];
6365
} else {
6466
$schemaURI = SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI;
6567
}

tests/ValidatorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ public function testValidateWithAssocSchema(): void
1818
$this->assertFalse($validator->isValid(), 'Validation succeeded, but should have failed.');
1919
}
2020

21+
public function testValidateWithAssocSchemaWithRelativeRefs(): void
22+
{
23+
$schema = json_decode(file_get_contents(__DIR__.'/fixtures/relative.json'), true);
24+
$data = json_decode('{"foo":{"foo": "bar"}}', false);
25+
26+
$validator = new Validator();
27+
$validator->validate($data, $schema);
28+
29+
$this->assertTrue($validator->isValid(), 'Validation failed, but should have succeeded.');
30+
}
31+
2132
public function testBadAssocSchemaInput(): void
2233
{
2334
if (version_compare(phpversion(), '5.5.0', '<')) {

tests/fixtures/relative.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"id": "tests/fixtures/relative.json",
3+
"type": "object",
4+
"properties": {
5+
"foo": {
6+
"$ref": "foobar.json"
7+
}
8+
},
9+
"required": ["foo"],
10+
"additionalProperties": false
11+
}

0 commit comments

Comments
 (0)