1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RealPage\JsonApi\Validation; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Validation\Factory; |
6
|
|
|
use Neomerx\JsonApi\Exceptions\ErrorCollection; |
7
|
|
|
use RealPage\JsonApi\Requests\Request; |
8
|
|
|
use Neomerx\JsonApi\Document\Error; |
9
|
|
|
|
10
|
|
|
trait ValidatesRequests |
11
|
|
|
{ |
12
|
|
|
/** @var ErrorCollection */ |
13
|
|
|
protected $errors; |
14
|
|
|
|
15
|
|
|
/** @var Factory */ |
16
|
|
|
protected $validatorFactory; |
17
|
|
|
|
18
|
|
|
public function isValid(Request $request) : bool |
19
|
|
|
{ |
20
|
|
|
$this->errors = new ErrorCollection(); |
21
|
|
|
|
22
|
|
|
/** @var \Illuminate\Contracts\Validation\Validator $validator */ |
23
|
|
|
$validator = $this->validatorFactory->make($request->json(), $this->rules(), $this->messages()); |
24
|
|
|
if ($validator->fails()) { |
25
|
|
|
// @todo put this somewhere else, this is getting messy |
26
|
|
|
// @see https://jsonapi.org/examples/#error-objects-basic |
27
|
|
|
foreach ($validator->errors()->all() as $field => $errorMessage) { |
|
|
|
|
28
|
|
|
|
29
|
|
|
// get the pointer for an array so we can pinpoint the section |
30
|
|
|
// of json where the error occurred |
31
|
|
|
$field = '/'.str_replace('.', '/', $field).'/'; |
32
|
|
|
|
33
|
|
|
$this->errors->add(new Error( |
34
|
|
|
null, |
35
|
|
|
null, |
36
|
|
|
422, |
37
|
|
|
null, |
38
|
|
|
'Invalid Attribute', |
39
|
|
|
$errorMessage, |
40
|
|
|
[ |
41
|
|
|
'pointer' => $field, |
42
|
|
|
] |
43
|
|
|
)); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function rules(array $rules = null) : array |
49
|
|
|
{ |
50
|
|
|
$rules = $rules ?? []; |
51
|
|
|
|
52
|
|
|
return array_merge([ |
53
|
|
|
'data' => 'required', |
54
|
|
|
'data.type' => 'required|in:'.$this->type(), |
55
|
|
|
], $rules); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function messages(array $messages = null) : array |
59
|
|
|
{ |
60
|
|
|
$messages = $messages ?? []; |
61
|
|
|
|
62
|
|
|
return array_merge([ |
63
|
|
|
'data.required' => 'Data is required in a valid json api format.', |
64
|
|
|
'data.type.required' => 'A valid resource type must be provided.', |
65
|
|
|
'data.type.in' => 'The resource type provided does not match the expected type of "'.$this->type().'".', |
66
|
|
|
], $messages); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function errors() : ErrorCollection |
70
|
|
|
{ |
71
|
|
|
return $this->errors; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The type of entity this request is for. |
76
|
|
|
*/ |
77
|
|
|
abstract public function type() : string; |
78
|
|
|
|
79
|
|
|
public function setValidatorFactory(Factory $factory) |
80
|
|
|
{ |
81
|
|
|
$this->validatorFactory = $factory; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.