Skip to content

Commit a9722e6

Browse files
committed
[BUGFIX] Cast empty schema arrays to object (jsonrainbow#409)
* Cast root to object * Use function_exists to allow polyfill compatibility * Move array->object conversion to SchemaConstraint & SchemaStorage Fixes issue jsonrainbow#408
1 parent 082e6e9 commit a9722e6

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/JsonSchema/Constraints/BaseConstraint.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ public static function arrayToObjectRecursive($array)
137137
$json = json_encode($array);
138138
if (json_last_error() !== \JSON_ERROR_NONE) {
139139
$message = 'Unable to encode schema array as JSON';
140-
if (version_compare(phpversion(), '5.5.0', '>=')) {
140+
if (function_exists('json_last_error_msg')) {
141141
$message .= ': ' . json_last_error_msg();
142142
}
143143
throw new InvalidArgumentException($message);
144144
}
145145

146-
return json_decode($json);
146+
return (object) json_decode($json);
147147
}
148148
}

src/JsonSchema/Constraints/SchemaConstraint.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
3535
// passed schema
3636
$validationSchema = $schema;
3737
} elseif ($this->getTypeCheck()->propertyExists($element, $this->inlineSchemaProperty)) {
38-
$inlineSchema = $this->getTypeCheck()->propertyGet($element, $this->inlineSchemaProperty);
39-
if (is_array($inlineSchema)) {
40-
$inlineSchema = json_decode(json_encode($inlineSchema));
41-
}
4238
// inline schema
43-
$validationSchema = $inlineSchema;
39+
$validationSchema = $this->getTypeCheck()->propertyGet($element, $this->inlineSchemaProperty);
4440
} else {
4541
throw new InvalidArgumentException('no schema found to verify against');
4642
}
4743

44+
// cast array schemas to object
45+
if (is_array($validationSchema)) {
46+
$validationSchema = BaseConstraint::arrayToObjectRecursive($validationSchema);
47+
}
48+
4849
// validate schema against whatever is defined in $validationSchema->$schema. If no
4950
// schema is defined, assume self::DEFAULT_SCHEMA_SPEC (currently draft-04).
5051
if ($this->factory->getConfig(self::CHECK_MODE_VALIDATE_SCHEMA)) {

src/JsonSchema/SchemaStorage.php

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace JsonSchema;
44

5+
use JsonSchema\Constraints\BaseConstraint;
56
use JsonSchema\Entity\JsonPointer;
67
use JsonSchema\Exception\UnresolvableJsonPointerException;
78
use JsonSchema\Iterator\ObjectIterator;
@@ -51,6 +52,12 @@ public function addSchema($id, $schema = null)
5152
// schemas do not have an associated URI when passed via Validator::validate().
5253
$schema = $this->uriRetriever->retrieve($id);
5354
}
55+
56+
// cast array schemas to object
57+
if (is_array($schema)) {
58+
$schema = BaseConstraint::arrayToObjectRecursive($schema);
59+
}
60+
5461
$objectIterator = new ObjectIterator($schema);
5562
foreach ($objectIterator as $toResolveSchema) {
5663
if (property_exists($toResolveSchema, '$ref') && is_string($toResolveSchema->{'$ref'})) {

src/JsonSchema/Validator.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public function validate(&$value, $schema = null, $checkMode = null)
5757
$this->factory->getSchemaStorage()->addSchema(SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI, $schema);
5858

5959
$validator = $this->factory->createInstanceFor('schema');
60-
$validator->check($value, $schema);
60+
$validator->check(
61+
$value,
62+
$this->factory->getSchemaStorage()->getSchema(SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI)
63+
);
6164

6265
$this->factory->setConfig($initialCheckMode);
6366

0 commit comments

Comments
 (0)