diff --git a/src/JsonSchema/Validator.php b/src/JsonSchema/Validator.php index 86440949..b2cff492 100644 --- a/src/JsonSchema/Validator.php +++ b/src/JsonSchema/Validator.php @@ -60,6 +60,8 @@ public function validate(&$value, $schema = null, $checkMode = null) // add provided schema to SchemaStorage with internal URI to allow internal $ref resolution if (is_object($schema) && property_exists($schema, 'id')) { $schemaURI = $schema->id; + } elseif (is_array($schema) && array_key_exists('id', $schema)) { + $schemaURI = $schema['id']; } else { $schemaURI = SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI; } diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 26a8069d..9f536384 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -18,6 +18,17 @@ public function testValidateWithAssocSchema(): void $this->assertFalse($validator->isValid(), 'Validation succeeded, but should have failed.'); } + public function testValidateWithAssocSchemaWithRelativeRefs(): void + { + $schema = json_decode(file_get_contents(__DIR__ . '/fixtures/relative.json'), true); + $data = json_decode('{"foo":{"foo": "bar"}}', false); + + $validator = new Validator(); + $validator->validate($data, $schema); + + $this->assertTrue($validator->isValid(), 'Validation failed, but should have succeeded.'); + } + public function testBadAssocSchemaInput(): void { if (version_compare(phpversion(), '5.5.0', '<')) { diff --git a/tests/fixtures/relative.json b/tests/fixtures/relative.json new file mode 100644 index 00000000..f07531b2 --- /dev/null +++ b/tests/fixtures/relative.json @@ -0,0 +1,11 @@ +{ + "id": "tests/fixtures/relative.json", + "type": "object", + "properties": { + "foo": { + "$ref": "foobar.json" + } + }, + "required": ["foo"], + "additionalProperties": false +}