Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly set the schema ID when passing it as assoc array #769

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/JsonSchema/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines 61 to 67
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe I've learned something more about the codebase today. These changes should be done respecting the CHECK_MODE_TYPE_CAST. Doing either a loose type check or a strict type check. I believe the following would do so (but requires some testing as well)

Suggested change
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;
}
$schemaURI = SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI;
if ($this->factory->getTypeCheck()::propertyExists($schema, 'id')) {
$schemaURI = $this->factory->getTypeCheck()::propertyGet($schema, 'id');
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<')) {
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/relative.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "tests/fixtures/relative.json",
"type": "object",
"properties": {
"foo": {
"$ref": "foobar.json"
}
},
"required": ["foo"],
"additionalProperties": false
}