1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Rules\Json\JsonSchema; |
4
|
|
|
|
5
|
|
|
use whm\Smoke\Http\Response; |
6
|
|
|
use whm\Smoke\Rules\StandardRule; |
7
|
|
|
use whm\Smoke\Rules\ValidationFailedException; |
8
|
|
|
use JsonSchema\Validator; |
9
|
|
|
use JsonSchema\SchemaStorage; |
10
|
|
|
use JsonSchema\Constraints\Constraint; |
11
|
|
|
use JsonSchema\Constraints\Factory; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This rule checks if a JSON file is valid for a JSON schema file |
15
|
|
|
*/ |
16
|
|
|
class JsonSchemaRule extends StandardRule |
17
|
|
|
{ |
18
|
|
|
private $jsonSchemaFiles; |
19
|
|
|
|
20
|
|
|
protected $contentTypes = array('json'); |
21
|
|
|
|
22
|
|
|
private $json_errors = array( |
23
|
|
|
JSON_ERROR_NONE => 'No Error', |
24
|
|
|
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
25
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', |
26
|
|
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
27
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
28
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
public function init($jsonSchemaFiles) |
32
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$this->jsonSchemaFiles = $jsonSchemaFiles; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function doValidation(Response $response) |
38
|
|
|
{ |
39
|
|
|
$data = json_decode($response->getBody()); |
40
|
|
|
if ($data === null) { |
41
|
|
|
throw new ValidationFailedException("The given JSON data can not be validated (last error: '" . $this->json_errors[json_last_error()] . "')."); |
42
|
|
|
} |
43
|
|
|
else { |
44
|
|
|
$error = false; |
45
|
|
|
$messageParts = array(); |
46
|
|
|
|
47
|
|
|
foreach ($this->jsonSchemaFiles AS $jsonSchemaFile) { |
48
|
|
|
$factory = new Factory( null, null, Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_COERCE ); |
49
|
|
|
$validator = new Validator($factory); |
50
|
|
|
|
51
|
|
|
$jsonSchemaObject = (object)json_decode(file_get_contents($jsonSchemaFile['jsonschemafileurl'])); |
52
|
|
|
|
53
|
|
|
$validator->check($data, $jsonSchemaObject); |
54
|
|
|
|
55
|
|
|
if (!$validator->isValid()) { |
56
|
|
|
$error = true; |
57
|
|
|
$errorMessage = ''; |
58
|
|
|
foreach ($validator->getErrors() as $error) { |
59
|
|
|
$errorMessage = $errorMessage . sprintf("[%s] %s\n", $error['property'], $error['message']); |
60
|
|
|
} |
61
|
|
|
$messageParts[] = $jsonSchemaFile['jsonschemafilename'] . ' - ' . $jsonSchemaFile['jsonschemafileurl'] . '(last error: ' . $errorMessage . ').'; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
if ($error == true) { |
|
|
|
|
66
|
|
|
$message = 'JSON file (' . (string)$response->getUri() . ') does not validate against the following JSON Schema files: ' . implode(", ", $messageParts); |
67
|
|
|
throw new ValidationFailedException($message); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.