File tree Expand file tree Collapse file tree 4 files changed +33
-8
lines changed Expand file tree Collapse file tree 4 files changed +33
-8
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Webtools \JsonSchemaRequest \Exceptions ;
4
+
5
+ use Illuminate \Http \Response ;
6
+ use Webtools \JsonSchemaRequest \JsonSchemaValidator ;
7
+
8
+ class ValidationException extends \Exception
9
+ {
10
+ public JsonSchemaValidator $ validator ;
11
+
12
+ public int $ status = Response::HTTP_UNPROCESSABLE_ENTITY ;
13
+
14
+ public function __construct (JsonSchemaValidator $ validator )
15
+ {
16
+ parent ::__construct ('The given data was invalid. ' );
17
+ $ this ->validator = $ validator ;
18
+ }
19
+
20
+ public function report ()
21
+ {
22
+ // Do not report this exception.
23
+ }
24
+
25
+ public function render ()
26
+ {
27
+ return response ()->json (['errors ' => $ this ->validator ->errors ()], $ this ->status );
28
+ }
29
+ }
Original file line number Diff line number Diff line change 9
9
use Illuminate \Http \Request ;
10
10
use Illuminate \Http \Response ;
11
11
use Illuminate \Validation \ValidatesWhenResolvedTrait ;
12
+ use Webtools \JsonSchemaRequest \Exceptions \ValidationException ;
12
13
13
14
class JsonSchemaRequest extends Request implements ValidatesWhenResolved
14
15
{
@@ -40,10 +41,7 @@ public function setContainer(Container $container)
40
41
41
42
public function failedValidation (JsonSchemaValidator $ validator )
42
43
{
43
- throw new HttpResponseException (response ()->json (
44
- ['errors ' => $ validator ->errors ()],
45
- Response::HTTP_UNPROCESSABLE_ENTITY
46
- ));
44
+ throw new ValidationException ($ validator );
47
45
}
48
46
49
47
public function validated ()
Original file line number Diff line number Diff line change 6
6
use Illuminate \Support \MessageBag ;
7
7
use JsonSchema \Validator as SchemaValidator ;
8
8
use JsonSchema \Constraints \Constraint ;
9
- use Illuminate \ Validation \ValidationException ;
9
+ use Webtools \ JsonSchemaRequest \ Exceptions \ValidationException ;
10
10
11
11
class JsonSchemaValidator implements ValidatorContract
12
12
{
Original file line number Diff line number Diff line change 2
2
3
3
namespace Webtools \JsonSchemaRequest \Tests ;
4
4
5
- use Illuminate \Http \Exceptions \HttpResponseException ;
6
- use Illuminate \Validation \ValidationException ;
5
+ use Webtools \JsonSchemaRequest \Exceptions \ValidationException ;
7
6
use Webtools \JsonSchemaRequest \JsonSchemaValidator ;
8
7
use JsonSchema \Validator ;
9
8
use PHPUnit \Framework \TestCase ;
10
9
11
-
12
10
class JsonSchemaValidatorTest extends TestCase
13
11
{
14
12
protected array $ schema = [
You can’t perform that action at this time.
0 commit comments