15
15
use Opis \JsonSchema \Errors \ErrorFormatter ;
16
16
use Opis \JsonSchema \Errors \ValidationError ;
17
17
use Opis \JsonSchema \Uri ;
18
+ use Opis \JsonSchema \ValidationResult ;
18
19
use Opis \JsonSchema \Validator ;
19
20
use Symfony \Component \HttpFoundation \Response ;
20
21
@@ -23,12 +24,6 @@ class SchemaValidatorService
23
24
/** @var Validator */
24
25
protected $ validator = null ;
25
26
26
- /**
27
- * error from the most recent ->validate() call
28
- * @var ValidationError
29
- */
30
- protected $ error = null ;
31
-
32
27
/**
33
28
* As needed, build a validator in memory that knows how to load schema from local disk.
34
29
* @return Validator
@@ -59,12 +54,25 @@ protected function getValidator(): Validator
59
54
* @return bool
60
55
*/
61
56
public function validate ($ data , $ schema ): bool
57
+ {
58
+ $ result = $ this ->validationResult ($ data , $ schema );
59
+ return $ result ->isValid ();
60
+ }
61
+
62
+ /**
63
+ * Given data (could be a JsonModel, associative-array style JSON, primitive)
64
+ * and a schema (could be a URI, an object literal, a JSON-encoded string)
65
+ * return the ValidationResult of the upstream Opis library.
66
+ * This is a good approach for methods that want to do their own error formatting through a deeper relationship with Opis
67
+ * @param mixed $data
68
+ * @param mixed $schema
69
+ * @return ValidationResult
70
+ */
71
+ public function validationResult ($ data , $ schema ): ValidationResult
62
72
{
63
73
$ validator = $ this ->getValidator ();
64
74
$ data = $ this ->normalizeData ($ data );
65
- $ result = $ validator ->validate ($ data , $ schema );
66
- $ this ->error = $ result ->error ();
67
- return $ result ->isValid ();
75
+ return $ validator ->validate ($ data , $ schema );
68
76
}
69
77
70
78
/**
@@ -80,28 +88,6 @@ private function normalizeData($data)
80
88
return json_decode (json_encode ($ data , JSON_THROW_ON_ERROR ));
81
89
}
82
90
83
- /**
84
- * Return an array of errors from the last *Validation call
85
- * @return ValidationError|null
86
- */
87
- public function getError (): ?ValidationError
88
- {
89
- return $ this ->error ;
90
- }
91
-
92
- /**
93
- * Return an array of human readable errors from the last *Validation call
94
- * @return array
95
- */
96
- public function getFormattedError (): array
97
- {
98
- if (!$ this ->error ) {
99
- return [];
100
- }
101
-
102
- return (new ErrorFormatter ())->formatFlat ($ this ->error );
103
- }
104
-
105
91
/**
106
92
* This method will attempt to validate the provided data against the provided schema. If the data is found to be
107
93
* invalid, then a `JsonSchemaValidationException` exception is thrown with a default message of "Request body
@@ -122,23 +108,24 @@ public function validateOrThrow(
122
108
bool $ appendValidationDescriptions = false ,
123
109
int $ failureHttpStatusCode = Response::HTTP_BAD_REQUEST ,
124
110
): bool {
125
- if ($ this ->validate ($ data , $ schema ) === false ) {
111
+ $ results = $ this ->validationResult ($ data , $ schema );
112
+ if ($ results ->isValid () === false ) {
126
113
$ message = $ exceptionMessage ?: 'Request body contains invalid data! ' ;
127
114
128
115
if ($ appendValidationDescriptions ) {
129
116
$ prepend = "\r\n* " ;
130
- $ message .= $ prepend . implode ($ prepend , $ this -> getFormattedError ( ));
117
+ $ message .= $ prepend . implode ($ prepend , ( new ErrorFormatter ())-> formatFlat ( $ results -> error () ));
131
118
}
132
119
133
120
Log::debug (
134
121
"Json Schema Validation Error " ,
135
122
[
136
- 'error ' => (new ErrorFormatter ())->format ($ this ->error , true , null , null ),
123
+ 'error ' => (new ErrorFormatter ())->format ($ results ->error () , true , null , null ),
137
124
'data ' => $ data
138
125
]
139
126
);
140
127
141
- throw new JsonSchemaValidationException ($ message , $ this -> getError (), null , $ failureHttpStatusCode );
128
+ throw new JsonSchemaValidationException ($ message , $ results -> error (), null , $ failureHttpStatusCode );
142
129
}
143
130
144
131
return true ;
0 commit comments