3

I would like to filter some data coming from an API payload in which i have to check if some certain part of the data is an object, such as this:

"object"{
  "propety":value,
  "another_propety":value,
}

I wanna be sure that the "object" that comes from the payload is actually an object, which holds properties and not an integer, neither, an array or any other type...but an object. Is there any way i can solve this by using Laravel's native validator i have to create a custom rule?

Thank you

1
  • check this answer if useful Commented Apr 1, 2020 at 11:05

1 Answer 1

5

Considering the laravel lifecycle, By the time the request payload reaches validation the object has already changed to a php array, use the below to validate your key.

$this->validate($request, [
    'object' => 'required|array',
    'object.property' => 'required|string',
]);

https://laravel.com/docs/5.8/validation#rule-array

also in case it is somehow going to remain a JSON object, check the official documentation for doing so -> https://laravel.com/docs/5.8/validation#rule-json

This will help you identify the object key as JSON while the request gets vaildated.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.