From 9e55278ab2d080aa94993865e075f7b00ed59652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Tue, 26 Nov 2024 13:08:12 +0100 Subject: [PATCH 1/4] fix: correctly set the schema ID when passing it as assoc array --- src/JsonSchema/Validator.php | 2 ++ tests/ValidatorTest.php | 11 +++++++++++ tests/fixtures/relative.json | 11 +++++++++++ 3 files changed, 24 insertions(+) create mode 100644 tests/fixtures/relative.json diff --git a/src/JsonSchema/Validator.php b/src/JsonSchema/Validator.php index a02c2e5d..48814952 100644 --- a/src/JsonSchema/Validator.php +++ b/src/JsonSchema/Validator.php @@ -61,6 +61,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..d49a482f 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 +} From c1ced785379691a58c7840c23fc6a7291cc1e786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Wed, 27 Nov 2024 11:42:36 +0100 Subject: [PATCH 2/4] code style --- tests/ValidatorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index d49a482f..9f536384 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -20,12 +20,12 @@ public function testValidateWithAssocSchema(): void public function testValidateWithAssocSchemaWithRelativeRefs(): void { - $schema = json_decode(file_get_contents(__DIR__.'/fixtures/relative.json'), true); + $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.'); } From fbf015613b0e292ac06b6201e15d1aa59e0de1e0 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Wed, 26 Feb 2025 16:28:37 +0100 Subject: [PATCH 3/4] refactor: simplify schema id value fetching --- .../Constraints/TypeCheck/LooseTypeCheck.php | 2 +- src/JsonSchema/Validator.php | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/JsonSchema/Constraints/TypeCheck/LooseTypeCheck.php b/src/JsonSchema/Constraints/TypeCheck/LooseTypeCheck.php index 18057512..8f9f349b 100644 --- a/src/JsonSchema/Constraints/TypeCheck/LooseTypeCheck.php +++ b/src/JsonSchema/Constraints/TypeCheck/LooseTypeCheck.php @@ -44,7 +44,7 @@ public static function propertyExists($value, $property) return property_exists($value, $property); } - return array_key_exists($property, $value); + return is_array($value) && array_key_exists($property, $value); } public static function propertyCount($value) diff --git a/src/JsonSchema/Validator.php b/src/JsonSchema/Validator.php index 48814952..bf172640 100644 --- a/src/JsonSchema/Validator.php +++ b/src/JsonSchema/Validator.php @@ -13,6 +13,7 @@ use JsonSchema\Constraints\BaseConstraint; use JsonSchema\Constraints\Constraint; +use JsonSchema\Constraints\TypeCheck\LooseTypeCheck; /** * A JsonSchema Constraint @@ -59,12 +60,9 @@ 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; + $schemaURI = SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI; + if (LooseTypeCheck::propertyExists($schema, 'id')) { + $schemaURI = LooseTypeCheck::propertyGet($schema, 'id'); } $this->factory->getSchemaStorage()->addSchema($schemaURI, $schema); From d27590a3d6bd4ee03760f02ed518c0ac4a614526 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Wed, 26 Feb 2025 16:31:55 +0100 Subject: [PATCH 4/4] docs: add changelog entry https://github.com/jsonrainbow/json-schema/pull/769 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2365ad9c..40c06e51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Upgrade php cs fixer to latest ([#783](https://github.com/jsonrainbow/json-schema/pull/783)) - Create deep copy before checking each sub schema in oneOf ([#791](https://github.com/jsonrainbow/json-schema/pull/791)) - Create deep copy before checking each sub schema in anyOf ([#792](https://github.com/jsonrainbow/json-schema/pull/792)) +- Correctly set the schema ID when passing it as assoc array ([#794](https://github.com/jsonrainbow/json-schema/pull/794)) ### Changed - Used PHPStan's int-mask-of type where applicable ([#779](https://github.com/jsonrainbow/json-schema/pull/779))