Skip to content
This repository was archived by the owner on Apr 20, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions features/json.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions fixtures/www/json/definitions.json
Original file line number Diff line number Diff line change
@@ -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}$"
}
}
}
15 changes: 15 additions & 0 deletions fixtures/www/json/schemaref.json
Original file line number Diff line number Diff line change
@@ -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"}
}
}
4 changes: 4 additions & 0 deletions fixtures/www/json/withref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"foo": "https://perdu.com",
"bar": "https://woot.com"
}
18 changes: 16 additions & 2 deletions src/Sanpi/Behatch/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down