Skip to content

Commit 0b6707c

Browse files
authored
Update JsonSchema.php
1 parent d65b47c commit 0b6707c

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/Codeception/Module/JsonSchema.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@
1010
*/
1111
class JsonSchema extends Module
1212
{
13-
1413
/**
1514
* Validate response by json schema
16-
* @param string $schemaRef object or json string
15+
* @param string|object|array|null $schemaRef object or json string
16+
* @return void
1717
*/
18-
public function seeResponseIsValidOnSchema($schemaRef)
18+
public function seeResponseIsValidOnSchema($schemaRef, $response = null): void
1919
{
20-
$response = $this->getModule('REST')->response;
20+
if(!isset($response)) {
21+
$response = $this->getModule('REST')->grabResponse();
22+
}
23+
if(is_array($response) || is_object($response)) {
24+
$response = json_encode($response);
25+
}
2126

2227
$validator = new Validator();
2328
$decodedResponse = json_decode($response);
@@ -26,6 +31,7 @@ public function seeResponseIsValidOnSchema($schemaRef)
2631
$message = '';
2732
$isValid = $validator->isValid();
2833
if (!$isValid) {
34+
$this->debug($response);
2935
$message = 'JSON does not validate. Violations:' . PHP_EOL;
3036
foreach ($validator->getErrors() as $error) {
3137
$message .= $error['property'] . ' ' . $error['message'] . PHP_EOL;
@@ -39,9 +45,20 @@ public function seeResponseIsValidOnSchema($schemaRef)
3945
* Validate response by json schema
4046
* @param string $schema path to json schema file
4147
*/
42-
public function seeResponseIsValidOnSchemaFile($schema)
48+
public function seeResponseIsValidOnSchemaFile(string $schema)
4349
{
4450
$schemaRef = (object)['$ref' => 'file://' . realpath($schema)];
4551
$this->seeResponseIsValidOnSchema($schemaRef);
4652
}
53+
54+
/**
55+
* Validate response by json schema
56+
* @param string|array|null $json json data
57+
* @param string $schema path to json schema file
58+
*/
59+
public function seeJsonValidOnSchemaFile($json, string $schema)
60+
{
61+
$schemaRef = (object)['$ref' => 'file://' . realpath($schema)];
62+
$this->seeResponseIsValidOnSchema($schemaRef, $json);
63+
}
4764
}

0 commit comments

Comments
 (0)