Skip to content

Commit 649793d

Browse files
Correctly set the schema ID when passing it as assoc array (#794)
Replaces #769 due to lack of permissions to update PR Closes #767. --------- Co-authored-by: Dalibor Karlović <dalibor.karlovic@sigwin.hr>
1 parent 4406698 commit 649793d

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Upgrade php cs fixer to latest ([#783](https://github.com/jsonrainbow/json-schema/pull/783))
1515
- Create deep copy before checking each sub schema in oneOf ([#791](https://github.com/jsonrainbow/json-schema/pull/791))
1616
- Create deep copy before checking each sub schema in anyOf ([#792](https://github.com/jsonrainbow/json-schema/pull/792))
17+
- Correctly set the schema ID when passing it as assoc array ([#794](https://github.com/jsonrainbow/json-schema/pull/794))
1718

1819
### Changed
1920
- Used PHPStan's int-mask-of<T> type where applicable ([#779](https://github.com/jsonrainbow/json-schema/pull/779))

src/JsonSchema/Constraints/TypeCheck/LooseTypeCheck.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function propertyExists($value, $property)
4444
return property_exists($value, $property);
4545
}
4646

47-
return array_key_exists($property, $value);
47+
return is_array($value) && array_key_exists($property, $value);
4848
}
4949

5050
public static function propertyCount($value)

src/JsonSchema/Validator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use JsonSchema\Constraints\BaseConstraint;
1515
use JsonSchema\Constraints\Constraint;
16+
use JsonSchema\Constraints\TypeCheck\LooseTypeCheck;
1617

1718
/**
1819
* A JsonSchema Constraint
@@ -59,10 +60,9 @@ public function validate(&$value, $schema = null, $checkMode = null)
5960
}
6061

6162
// add provided schema to SchemaStorage with internal URI to allow internal $ref resolution
62-
if (is_object($schema) && property_exists($schema, 'id')) {
63-
$schemaURI = $schema->id;
64-
} else {
65-
$schemaURI = SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI;
63+
$schemaURI = SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI;
64+
if (LooseTypeCheck::propertyExists($schema, 'id')) {
65+
$schemaURI = LooseTypeCheck::propertyGet($schema, 'id');
6666
}
6767
$this->factory->getSchemaStorage()->addSchema($schemaURI, $schema);
6868

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)