From b5fd87e9f74234eb28e15158d6b1759aaa6e9396 Mon Sep 17 00:00:00 2001 From: Anthon Pang Date: Fri, 19 Mar 2021 17:24:20 -0400 Subject: [PATCH 1/5] fix phpdoc: missing typehints on $path and $value --- src/JsonSchema/Constraints/TypeConstraint.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/JsonSchema/Constraints/TypeConstraint.php b/src/JsonSchema/Constraints/TypeConstraint.php index ff4eba71..1c6ea1f2 100644 --- a/src/JsonSchema/Constraints/TypeConstraint.php +++ b/src/JsonSchema/Constraints/TypeConstraint.php @@ -81,11 +81,11 @@ public function check(&$value = null, $schema = null, JsonPointer $path = null, * of $isValid to true, if at least one $type mateches the type of $value or the value * passed as $isValid is already true. * - * @param mixed $value Value to validate - * @param array $type TypeConstraints to check against - * @param array $validTypesWording An array of wordings of the valid types of the array $type - * @param bool $isValid The current validation value - * @param $path + * @param mixed $value Value to validate + * @param array $type TypeConstraints to check against + * @param array $validTypesWording An array of wordings of the valid types of the array $type + * @param bool $isValid The current validation value + * @param JsonPointer|null $path */ protected function validateTypesArray(&$value, array $type, &$validTypesWording, &$isValid, $path, $coerce = false) { @@ -122,9 +122,9 @@ protected function validateTypesArray(&$value, array $type, &$validTypesWording, * difference, that, if $listEnd isn't false, the last element delimiter is $listEnd instead of * $delimiter. * - * @param array $elements The elements to implode - * @param string $delimiter The delimiter to use - * @param bool $listEnd The last delimiter to use (defaults to $delimiter) + * @param array $elements The elements to implode + * @param string $delimiter The delimiter to use + * @param bool|string $listEnd The last delimiter to use (defaults to $delimiter) * * @return string */ @@ -239,7 +239,7 @@ protected function validateType(&$value, $type, $coerce = false) /** * Converts a value to boolean. For example, "true" becomes true. * - * @param $value The value to convert to boolean + * @param mixed $value The value to convert to boolean * * @return bool|mixed */ From f4b197fca857abe8603dbd6796f587d154f08988 Mon Sep 17 00:00:00 2001 From: Anthon Pang Date: Fri, 19 Mar 2021 17:28:05 -0400 Subject: [PATCH 2/5] testing for !$isValid is redundant at this point --- src/JsonSchema/Constraints/TypeConstraint.php | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/JsonSchema/Constraints/TypeConstraint.php b/src/JsonSchema/Constraints/TypeConstraint.php index 1c6ea1f2..fe843f70 100644 --- a/src/JsonSchema/Constraints/TypeConstraint.php +++ b/src/JsonSchema/Constraints/TypeConstraint.php @@ -98,21 +98,17 @@ protected function validateTypesArray(&$value, array $type, &$validTypesWording, // $tp can be an object, if it's a schema instead of a simple type, validate it // with a new type constraint if (is_object($tp)) { - if (!$isValid) { - $validator = $this->factory->createInstanceFor('type'); - $subSchema = new \stdClass(); - $subSchema->type = $tp; - $validator->check($value, $subSchema, $path, null); - $error = $validator->getErrors(); - $isValid = !(bool) $error; - $validTypesWording[] = self::$wording['object']; - } + $validator = $this->factory->createInstanceFor('type'); + $subSchema = new \stdClass(); + $subSchema->type = $tp; + $validator->check($value, $subSchema, $path, null); + $error = $validator->getErrors(); + $isValid = !(bool) $error; + $validTypesWording[] = self::$wording['object']; } else { $this->validateTypeNameWording($tp); $validTypesWording[] = self::$wording[$tp]; - if (!$isValid) { - $isValid = $this->validateType($value, $tp, $coerce); - } + $isValid = $this->validateType($value, $tp, $coerce); } } } From a157dda7167ae1f082a4aa612984aa67a5afa136 Mon Sep 17 00:00:00 2001 From: Anthon Pang Date: Fri, 19 Mar 2021 17:29:47 -0400 Subject: [PATCH 3/5] $path enters this function as JsonPointer instance --- src/JsonSchema/Constraints/UndefinedConstraint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index bdb95f4a..d4200209 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -140,7 +140,7 @@ protected function validateCommonProperties(&$value, $schema, JsonPointer $path, if (!$this->getTypeCheck()->propertyExists($value, $required)) { $this->addError( ConstraintError::REQUIRED(), - $this->incrementPath($path ?: new JsonPointer(''), $required), array( + $this->incrementPath($path, $required), array( 'property' => $required ) ); From 46384b1277ec10d5526fa6061c312bc4bb2302a1 Mon Sep 17 00:00:00 2001 From: Anthon Pang Date: Fri, 19 Mar 2021 17:39:29 -0400 Subject: [PATCH 4/5] add addSchema() unit test to trigger error when schema is missing "properties" ``` There was 1 error: 1) JsonSchema\Tests\SchemaStorageTest::testMissingProperties Creating default object from empty value /Development/json-schema/src/JsonSchema/SchemaStorage.php:64 /Development/json-schema/tests/SchemaStorageTest.php:334 ``` --- tests/SchemaStorageTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/SchemaStorageTest.php b/tests/SchemaStorageTest.php index ebbc9477..9ca7e2c2 100644 --- a/tests/SchemaStorageTest.php +++ b/tests/SchemaStorageTest.php @@ -323,4 +323,14 @@ public function testNoDoubleResolve() $schemas['test/schema']->{'$ref'} ); } + + public function testAddSchemaMissingProperties() + { + $schema_03 = array('id' => 'http://json-schema.org/draft-03/schema#'); + $schema_04 = array('id' => 'http://json-schema.org/draft-04/schema#'); + + $s = new SchemaStorage(); + $s->addSchema($schema_03['id'], $schema_03); + $s->addSchema($schema_04['id'], $schema_04); + } } From fbf27f58aa36d3cacdf7f5e0d1fe6ca7e65ac1b4 Mon Sep 17 00:00:00 2001 From: Anthon Pang Date: Fri, 19 Mar 2021 18:05:12 -0400 Subject: [PATCH 5/5] fix php warning --- src/JsonSchema/SchemaStorage.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/JsonSchema/SchemaStorage.php b/src/JsonSchema/SchemaStorage.php index 81b3508c..1ec301e3 100644 --- a/src/JsonSchema/SchemaStorage.php +++ b/src/JsonSchema/SchemaStorage.php @@ -60,11 +60,25 @@ public function addSchema($id, $schema = null) // workaround for bug in draft-03 & draft-04 meta-schemas (id & $ref defined with incorrect format) // see https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/177#issuecomment-293051367 if (is_object($schema) && property_exists($schema, 'id')) { - if ($schema->id == 'http://json-schema.org/draft-04/schema#') { - $schema->properties->id->format = 'uri-reference'; - } elseif ($schema->id == 'http://json-schema.org/draft-03/schema#') { - $schema->properties->id->format = 'uri-reference'; - $schema->properties->{'$ref'}->format = 'uri-reference'; + $draft03 = false; + if ( + $schema->id === 'http://json-schema.org/draft-04/schema#' || + ($schema->id === 'http://json-schema.org/draft-03/schema#' && $draft03 = true) + ) { + if (!property_exists($schema, 'properties')) { + $schema->properties = new \stdClass; + } + if (!property_exists($schema->properties, 'id')) { + $schema->properties->id = new \stdClass; + } + $schema->properties->id->format = 'uri-reference'; + + if ($draft03) { + if (!property_exists($schema->properties, '$ref')) { + $schema->properties->{'$ref'} = new \stdClass; + } + $schema->properties->{'$ref'}->format = 'uri-reference'; + } } }