Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

secondman's avatar

Validating Remote API Data

Hey All.

I'm building a service that fetches an array from a user's remote account through a RESTful API.

The data comes in as a large nested array, and I'd like to use a Validator instance with rules and messages to show errors to the user, basically items that we require, that may not have been sent.

I have the basics started, but I'm wondering how to loop through nested items and create appropriate messages to pass back to Vuex.

The data is structured similar to this:

[
    'item' => [
        "name" => "Item name",
        "notes" => "Here are some notes.",
        "nested_1" => [
            0 => [
                "name" => "Item name",
                "notes" => "Here are some notes.",
                "nested_2" => [
                    0 => [
                        "name" => "Nested item name",
                        "notes" => "Here are notes.",
                        "nested_3" => [
                            0 => [
                                "name" => "Nested item",
                                "notes" => "Here are notes."
                            ]
                        ]
                    ]
                ]
            ],
            1 => [
                "name" => "Nested item name",
                "notes" => "No notes other than this.",
                "nested_2" => [
                    0 => [
                        "name" => "Nested item name",
                        "notes" => "Here are notes.",
                        "nested_3" => [
                            0 => [
                                "name" => "Nested item",
                                "notes" => "Here are notes."
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
]

There can be any number of arrays that are nested up to 3 levels deep, so is there an article or technique anyone can point me to that will explain how to validate all these items while still being able to keep all the errors coupled to their given item?

Yes I've Googled. I can't find anything except how to validate forms in your own Laravel API. And of course I also have the documentation open as I go so please don't point me there.

Thanks

0 likes
7 replies
bugsysha's avatar
bugsysha
Best Answer
Level 61

You would have to use recursion. But I would not do that. Do not validate what you do not own cause it can break down pretty quickly. Instead use Value/DataTransfer objects and just use them to fetch the data.

secondman's avatar

@bugsysha

Thanks for the response, but I have no experience with using those and this entire service is already built so I can't spend the time or resources to go back and replace the work already completed.

The service simply provides a user based preview of the data, so using Validator will be plenty since all I need to do is display the data, not persist it.

Thanks for the tip though, this gives me a new design pattern to learn.

secondman's avatar

@bugsysha

So we have our local app, and we have a remote app that we pull data from via api.

Our local user makes a request to a member of another company, which creates an account and a request object on the remote app. A member of the other company fills in information about their requested product on the remote app and submits it.

Our original user on the local app then previews the data submitted and we need to check that all the data required for our local app has been sent. If not they can then send a reply to the remote user saying, hey, please fill in these items and resubmit.

Yes, having the field required on the remote app would resolve this issue, I know. But that option is not readily available so I have to find a way to validate what's sent, and show some sort of warning for the local user.

In any event, I've been reading about DTO's per your suggestion and pulled in the Spatie package.

This does nothing really for me other than standardize the data types, and strip away any data we don't need. It doesn't do ay sort of checking or validating whether given fields have been sent, which is really what I'm after.

If for instance I have this as my fromRequest method:

public static function fromRequest(array $request): self
{
    return new self([
        'rid'           => $request['rid'],
        'name'          => $request['name'],
        'supplier_name' => $request['supplier_name'],
        'notes'         => $request['notes'],
        'mask'          => $request['mask'],
    ]);
}

If any of the $request info is not sent, I get a fatal error ... and that's exactly what Im trying to avoid.

Any other suggestions would be appreciated.

bugsysha's avatar

@vkronlein

There is no need to use Spatie package for this but as you wish.

If any of the $request info is not sent, I get a fatal error

You can use ?? or Arr::get($array, 'products.desk.discount', 0) where the third argument is default value.

secondman's avatar

I built a simply validation piece to check that the items in a "required" array are set in the array being passed in.

Thanks for the suggestion, kinda seems like overkill, but it's a nice abstraction to pull those big arrays out of my code.

bugsysha's avatar

Yeah, it is easier to work with encapsulated data like that than to repeat same process in who knows how many places in the system.

Please or to participate in this conversation.