How to validate an empty payload from an API?

Asked

Viewed 227 times

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

  • 1

    Exactly, @Virgilionovic, the problem was understanding why the validation wasn’t processing for the invalid payload values, but I figured out the problem, thanks.

1 answer

0

I solved the problem, the validation was already working.

The problem was elsewhere, which I have corrected. Was passing the $request straight to the getDataUser method, so I wasn’t checking the status... so I never entered the validation.

 public function getUsers(Request $r)
 {
     $data = $r->all();

     $validate = $this->request->validate($data);
     /* faltou isso */
     if (!$validate['status']) {
        return $validate;
     }

     $request = $this->validatePayloadRequest($data);
     //dd($request);

     if (!$request['status']) {
        return $request;
     }

     return $this->user->getDataUser($request);
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.