From 15df8fbdf6cf0ae2e37d0a33a3ded05ed7bcb842 Mon Sep 17 00:00:00 2001 From: Erayd Date: Thu, 23 Feb 2017 08:56:06 +1300 Subject: [PATCH] Default to draft-04 if $schema is undefined Previous behavior was to throw a RuntimeException. --- .../Constraints/SchemaConstraint.php | 12 ++++++++---- tests/Constraints/SchemaValidationTest.php | 18 ++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/JsonSchema/Constraints/SchemaConstraint.php b/src/JsonSchema/Constraints/SchemaConstraint.php index fcf548fe..3fe09f13 100644 --- a/src/JsonSchema/Constraints/SchemaConstraint.php +++ b/src/JsonSchema/Constraints/SchemaConstraint.php @@ -23,6 +23,8 @@ */ class SchemaConstraint extends Constraint { + const DEFAULT_SCHEMA_SPEC = 'http://json-schema.org/draft-04/schema#'; + /** * {@inheritdoc} */ @@ -42,13 +44,16 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i = throw new InvalidArgumentException('no schema found to verify against'); } - // validate schema against whatever is defined in $validationSchema->$schema + // 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)) { if (!$this->getTypeCheck()->isObject($validationSchema)) { throw new RuntimeException('cannot validate non-object schema'); } - if (!$this->getTypeCheck()->propertyExists($validationSchema, '$schema')) { - throw new RuntimeException('$schema is not set'); + if ($this->getTypeCheck()->propertyExists($validationSchema, '$schema')) { + $schemaSpec = $this->getTypeCheck()->propertyGet($validationSchema, '$schema'); + } else { + $schemaSpec = self::DEFAULT_SCHEMA_SPEC; } // preload standard schema specs @@ -58,7 +63,6 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i = 'http://json-schema.org/draft-04/schema' => 'json-schema-draft-04.json', 'http://json-schema.org/draft-04/schema#' => 'json-schema-draft-04.json' ); - $schemaSpec = $this->getTypeCheck()->propertyGet($validationSchema, '$schema'); $schemaStorage = $this->factory->getSchemaStorage(); foreach ($preload as $schemaID => $schemaFile) { $schemaStorage->addSchema( diff --git a/tests/Constraints/SchemaValidationTest.php b/tests/Constraints/SchemaValidationTest.php index 6df61e01..f2f5e347 100644 --- a/tests/Constraints/SchemaValidationTest.php +++ b/tests/Constraints/SchemaValidationTest.php @@ -49,6 +49,18 @@ public function getValidTests() } }', '' + ), + array( // inline schema with default (undefined) spec + '{ + "$schema": { + "properties": { + "propertyOne": { + "type": "string" + } + } + } + }', + '' ) ); } @@ -64,10 +76,4 @@ public function testNonObjectSchema() $this->setExpectedException('JsonSchema\Exception\RuntimeException'); parent::testValidCases('{"$schema": "notAnObject"}', ''); } - - public function testMissingSchemaSpec() - { - $this->setExpectedException('JsonSchema\Exception\RuntimeException'); - parent::testValidCases('{"$schema":{}}', ''); - } }