diff --git a/features/json.feature b/features/json.feature index bc17487d..ba57beae 100644 --- a/features/json.feature +++ b/features/json.feature @@ -35,6 +35,10 @@ Feature: Testing JSONContext Given I am on "/json/imajson.json" Then the JSON should be valid according to the schema "fixtures/www/json/schema.json" + Scenario: Json validation with schema containing ref + Given I am on "/json/withref.json" + Then the JSON should be valid according to the schema "fixtures/www/json/schemaref.json" + Scenario: Json validation Given I am on "/json/imajson.json" Then the JSON should be valid according to this schema: diff --git a/fixtures/www/json/definitions.json b/fixtures/www/json/definitions.json new file mode 100644 index 00000000..5d226c1f --- /dev/null +++ b/fixtures/www/json/definitions.json @@ -0,0 +1,11 @@ +{ + "type": "object", + "$schema": "http://json-schema.org/draft-03/schema", + "required": true, + "definitions": { + "https": { + "type": "string", + "pattern": "^https?\\:\\/\\/.+\\.[a-zA-Z]{2,4}$" + } + } +} diff --git a/fixtures/www/json/schemaref.json b/fixtures/www/json/schemaref.json new file mode 100644 index 00000000..953cd769 --- /dev/null +++ b/fixtures/www/json/schemaref.json @@ -0,0 +1,15 @@ +{ + "type": "object", + "$schema": "http://json-schema.org/draft-03/schema", + "required": true, + "definitions": { + "url": { + "type": "string", + "pattern": "^https?\\:\\/\\/.+\\.[a-zA-Z]{2,4}$" + } + }, + "properties": { + "foo": { "$ref": "#/definitions/url"}, + "bar": { "$ref": "definitions.json#/definitions/https"} + } +} diff --git a/fixtures/www/json/withref.json b/fixtures/www/json/withref.json new file mode 100644 index 00000000..5be9cfeb --- /dev/null +++ b/fixtures/www/json/withref.json @@ -0,0 +1,4 @@ +{ + "foo": "https://perdu.com", + "bar": "https://woot.com" +} diff --git a/src/Sanpi/Behatch/Context/JsonContext.php b/src/Sanpi/Behatch/Context/JsonContext.php index 1c0f08ad..9c5109e3 100644 --- a/src/Sanpi/Behatch/Context/JsonContext.php +++ b/src/Sanpi/Behatch/Context/JsonContext.php @@ -147,7 +147,7 @@ public function theJsonShouldBeValidAccordingToTheSchema($filename) { if (is_file($filename)) { $schema = file_get_contents($filename); - $this->validate($schema); + $this->validate($schema, array('uri' => 'file://' . getcwd() . '/' . $filename )); } else { throw new \RuntimeException( @@ -208,7 +208,7 @@ private function evaluateJson($json, $expression) return $result; } - private function validate($schema) + private function validate($schema, $context = null) { try { $jsonSchema = $this->decode($schema); @@ -217,8 +217,22 @@ private function validate($schema) throw new \Exception('The schema is not a valid JSON'); } + $uri = null; + if (null !== $context) { + if (is_string($context)) { + $uri = $context; + } elseif (is_array($context) && array_key_exists('uri', $context)) { + $uri = $context['uri']; + } + } + + $retriever = new \JsonSchema\Uri\UriRetriever; + $refResolver = new \JsonSchema\RefResolver($retriever); + $refResolver->resolve($jsonSchema, $uri); + $validator = new \JsonSchema\Validator(); $validator->check($this->getJson(), $jsonSchema); + if (!$validator->isValid()) { $msg = "JSON does not validate. Violations:\n"; foreach ($validator->getErrors() as $error) {