Skip to content

Commit 79d2d9a

Browse files
authored
Merge pull request #6 from tyx/feature/json-schema-validation
:neckbeard: Add a factory to create JsonSchema class
2 parents 9c6b24c + c1dbf61 commit 79d2d9a

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

JsonSchemaTools.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rezzza\SymfonyRestApiJson;
4+
5+
use JsonSchema\Validator;
6+
use JsonSchema\RefResolver;
7+
8+
/**
9+
* Used as factory because JsonSchema\Validator and JsonSchema\RefResolver are not stateless
10+
*/
11+
class JsonSchemaTools
12+
{
13+
public function createValidator()
14+
{
15+
return new Validator;
16+
}
17+
18+
public function createRefResolver()
19+
{
20+
return new RefResolver;
21+
}
22+
}

PayloadValidator.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22

33
namespace Rezzza\SymfonyRestApiJson;
44

5-
use JsonSchema\Validator;
6-
use JsonSchema\RefResolver;
7-
85
class PayloadValidator
96
{
10-
private $delegateValidator;
11-
12-
private $refResolver;
7+
private $jsonSchemaTools;
138

14-
public function __construct(Validator $delegateValidator, RefResolver $refResolver)
9+
public function __construct(JsonSchemaTools $jsonSchemaTools)
1510
{
16-
$this->delegateValidator = $delegateValidator;
17-
$this->refResolver = $refResolver;
11+
$this->jsonSchemaTools = $jsonSchemaTools;
1812
}
1913

2014
public function validate($payload, $jsonSchemaFilename)
@@ -23,13 +17,16 @@ public function validate($payload, $jsonSchemaFilename)
2317
throw new \UnexpectedValueException(sprintf('Cannot validate payload through "%s" json schema. File does not exist.', $jsonSchemaFilename));
2418
}
2519

26-
$this->delegateValidator->check(
20+
$delegateValidator = $this->jsonSchemaTools->createValidator();
21+
$refResolver = $this->jsonSchemaTools->createRefResolver();
22+
23+
$delegateValidator->check(
2724
json_decode($payload),
28-
$this->refResolver->resolve('file://' . realpath($jsonSchemaFilename))
25+
$refResolver->resolve('file://' . realpath($jsonSchemaFilename))
2926
);
3027

31-
if (!$this->delegateValidator->isValid()) {
32-
throw InvalidPayload::withErrors($payload, $jsonSchemaFilename, $this->delegateValidator->getErrors());
28+
if (!$delegateValidator->isValid()) {
29+
throw InvalidPayload::withErrors($payload, $jsonSchemaFilename, $delegateValidator->getErrors());
3330
}
3431
}
3532
}

tests/Units/PayloadValidator.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public function test_validation_should_be_delegated_to_internal_validator()
1414
$this->calling($validator)->check = null,
1515
$refResolver = $this->mockJsonSchemaRefResolver(),
1616
$this->calling($refResolver)->resolve = 'resolvedJsonSchema',
17-
$this->newTestedInstance($validator, $refResolver)
17+
$jsonSchemaTools = $this->mockJsonSchemaTools($validator, $refResolver),
18+
$this->newTestedInstance($jsonSchemaTools)
1819
)
1920
->when(
2021
$this->testedInstance->validate('"json"', __DIR__.'/../Fixtures/mySchema.json')
@@ -31,7 +32,8 @@ public function test_unknown_json_schema_lead_to_exception()
3132
{
3233
$this
3334
->given(
34-
$this->newTestedInstance($this->mockJsonSchemaValidator(), $this->mockJsonSchemaRefResolver())
35+
$jsonSchemaTools = $this->mockJsonSchemaTools(),
36+
$this->newTestedInstance($jsonSchemaTools)
3537
)
3638
->exception(function () {
3739
$this->testedInstance->validate('"json"', 'hello.json');
@@ -50,7 +52,8 @@ public function test_invalid_internal_validation_lead_to_exception()
5052
$this->calling($validator)->getErrors = ['error1', 'error2'],
5153
$refResolver = $this->mockJsonSchemaRefResolver(),
5254
$this->calling($refResolver)->resolve = 'resolvedJsonSchema',
53-
$this->newTestedInstance($validator, $refResolver)
55+
$jsonSchemaTools = $this->mockJsonSchemaTools($validator, $refResolver),
56+
$this->newTestedInstance($jsonSchemaTools)
5457
)
5558
->exception(function () {
5659
$this->testedInstance->validate('"json"', __DIR__.'/../Fixtures/mySchema.json');
@@ -74,4 +77,14 @@ private function mockJsonSchemaRefResolver()
7477

7578
return new \mock\JsonSchema\RefResolver;
7679
}
80+
81+
private function mockJsonSchemaTools($validator = null, $refResolver = null)
82+
{
83+
$this->mockGenerator->orphanize('__construct');
84+
$mock = new \mock\Rezzza\SymfonyRestApiJson\JsonSchemaTools;
85+
$this->calling($mock)->createValidator = $validator;
86+
$this->calling($mock)->createRefResolver = $refResolver;
87+
88+
return $mock;
89+
}
7790
}

0 commit comments

Comments
 (0)