2
I have an API that returns in error when I pass the payload parameter as: {}
. This happens because it enters the method, going through the validation of the Standard. The validation of the rule is configured below:
public function rules()
{
return [
'name' => ['required', 'string', Rule::in($this->events)],
'payload' => 'required'
];
}
This is another method I tried to validate the payload in case of an error or invalid request:
private function validatePayloadRequest($data)
{
if (isset($data['payload'])) {
try {
$request = json_decode($data['payload'], true);
} catch(Exception $e) {
$validateDataPayload['status'] = false;
$validateDataPayload['validate'] = 'The parameters are incorrect or incomplete';
}
if ($request !== null && is_array($request) && count($request) && !is_numeric(array_keys($request)[0])) {
//Estou tentando escapar dessa condição, mas não sei como fazer, ou se há uma maneira mais simples de resolver isso
$validateDataPayload = $this->request->validPayload($request);
} else {
$validateDataPayload['status'] = false;
$validateDataPayload['validate'] = 'Check the parameters. Are incorrect or incomplete';
}
}
if (!$validateDataPayload['status']) {
return $validateDataPayload;
}
return $request;
}
I’m new to the Laravel, so I’m trying to understand these validation filters... Is there any kind that filters when the value is: {}
, in php: Array(0)
.
It would have to return something like this, but only occurs if the parameter is not set:
{
"validate": {
"payload": [
"Payload is required."
]
},
"status": false
}
Obs: I did so:
'payload' => 'required|regex:/[^{}]/'
It works, however, when I pass a parameter that does not match the payload validation, the error persists, type : {"a": "qualquercoisa"}
, where the "a" key does not exist in my application... hence it does not even enter the secondary validation of the payload parameters:
public function validPayload($data)
{
$rules = [
'user_id' => 'required|integer',
'page' => 'required|integer',
'access_token' => ['required', 'string']
];
}
I don’t understand which validation you want to use. Your explanation was confusing
– novic
Exactly, @Virgilionovic, the problem was understanding why the validation wasn’t processing for the invalid payload values, but I figured out the problem, thanks.
– Ivan Ferrer