From 9119798e2b4e4f45edba3535b1495a3d02efd208 Mon Sep 17 00:00:00 2001 From: Erayd Date: Wed, 5 Apr 2017 20:04:18 +1200 Subject: [PATCH 1/3] Cast root to object --- src/JsonSchema/Constraints/BaseConstraint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonSchema/Constraints/BaseConstraint.php b/src/JsonSchema/Constraints/BaseConstraint.php index a473edac..e620aec3 100644 --- a/src/JsonSchema/Constraints/BaseConstraint.php +++ b/src/JsonSchema/Constraints/BaseConstraint.php @@ -152,6 +152,6 @@ public static function arrayToObjectRecursive($array) throw new InvalidArgumentException($message); } - return json_decode($json); + return (object) json_decode($json); } } From d2c0baba026f4df34bfad76aeea0e28878cfb2bf Mon Sep 17 00:00:00 2001 From: Erayd Date: Wed, 5 Apr 2017 20:04:39 +1200 Subject: [PATCH 2/3] Use function_exists to allow polyfill compatibility --- src/JsonSchema/Constraints/BaseConstraint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonSchema/Constraints/BaseConstraint.php b/src/JsonSchema/Constraints/BaseConstraint.php index e620aec3..13168d71 100644 --- a/src/JsonSchema/Constraints/BaseConstraint.php +++ b/src/JsonSchema/Constraints/BaseConstraint.php @@ -146,7 +146,7 @@ public static function arrayToObjectRecursive($array) $json = json_encode($array); if (json_last_error() !== \JSON_ERROR_NONE) { $message = 'Unable to encode schema array as JSON'; - if (version_compare(phpversion(), '5.5.0', '>=')) { + if (function_exists('json_last_error_msg')) { $message .= ': ' . json_last_error_msg(); } throw new InvalidArgumentException($message); From edb168ace73a109042335f65d7f7b69cb6d5ec7e Mon Sep 17 00:00:00 2001 From: Erayd Date: Wed, 5 Apr 2017 20:42:13 +1200 Subject: [PATCH 3/3] Move array->object conversion to SchemaConstraint & SchemaStorage Fixes issue #408 --- src/JsonSchema/Constraints/SchemaConstraint.php | 11 ++++++----- src/JsonSchema/SchemaStorage.php | 7 +++++++ src/JsonSchema/Validator.php | 10 ++++------ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/JsonSchema/Constraints/SchemaConstraint.php b/src/JsonSchema/Constraints/SchemaConstraint.php index fad577b7..8218d256 100644 --- a/src/JsonSchema/Constraints/SchemaConstraint.php +++ b/src/JsonSchema/Constraints/SchemaConstraint.php @@ -36,16 +36,17 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i = // passed schema $validationSchema = $schema; } elseif ($this->getTypeCheck()->propertyExists($element, $this->inlineSchemaProperty)) { - $inlineSchema = $this->getTypeCheck()->propertyGet($element, $this->inlineSchemaProperty); - if (is_array($inlineSchema)) { - $inlineSchema = json_decode(json_encode($inlineSchema)); - } // inline schema - $validationSchema = $inlineSchema; + $validationSchema = $this->getTypeCheck()->propertyGet($element, $this->inlineSchemaProperty); } else { throw new InvalidArgumentException('no schema found to verify against'); } + // cast array schemas to object + if (is_array($validationSchema)) { + $validationSchema = BaseConstraint::arrayToObjectRecursive($validationSchema); + } + // validate schema against whatever is defined in $validationSchema->$schema. If no // schema is defined, assume self::DEFAULT_SCHEMA_SPEC (currently draft-04). if ($this->factory->getConfig(self::CHECK_MODE_VALIDATE_SCHEMA)) { diff --git a/src/JsonSchema/SchemaStorage.php b/src/JsonSchema/SchemaStorage.php index 3ee081e3..3c17c8c4 100644 --- a/src/JsonSchema/SchemaStorage.php +++ b/src/JsonSchema/SchemaStorage.php @@ -2,6 +2,7 @@ namespace JsonSchema; +use JsonSchema\Constraints\BaseConstraint; use JsonSchema\Entity\JsonPointer; use JsonSchema\Exception\UnresolvableJsonPointerException; use JsonSchema\Iterator\ObjectIterator; @@ -51,6 +52,12 @@ public function addSchema($id, $schema = null) // schemas do not have an associated URI when passed via Validator::validate(). $schema = $this->uriRetriever->retrieve($id); } + + // cast array schemas to object + if (is_array($schema)) { + $schema = BaseConstraint::arrayToObjectRecursive($schema); + } + $objectIterator = new ObjectIterator($schema); foreach ($objectIterator as $toResolveSchema) { if (property_exists($toResolveSchema, '$ref') && is_string($toResolveSchema->{'$ref'})) { diff --git a/src/JsonSchema/Validator.php b/src/JsonSchema/Validator.php index 49fec4c1..8d1b50c9 100644 --- a/src/JsonSchema/Validator.php +++ b/src/JsonSchema/Validator.php @@ -45,11 +45,6 @@ public function validate(&$value, $schema = null, $checkMode = null) // reset errors prior to validation $this->reset(); - // make sure $schema is an object - if (is_array($schema)) { - $schema = self::arrayToObjectRecursive($schema); - } - // set checkMode $initialCheckMode = $this->factory->getConfig(); if ($checkMode !== null) { @@ -60,7 +55,10 @@ public function validate(&$value, $schema = null, $checkMode = null) $this->factory->getSchemaStorage()->addSchema(SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI, $schema); $validator = $this->factory->createInstanceFor('schema'); - $validator->check($value, $schema); + $validator->check( + $value, + $this->factory->getSchemaStorage()->getSchema(SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI) + ); $this->factory->setConfig($initialCheckMode);