Mar 8, 2024
0
Level 1
How to do assertJson validation on array elements that can sometimes be empty?
I have API response:
[
{
"title": "title",
"gallery": [
{
"src": "fsdg.jpg"
}
]
},
{
"title": "title2",
"gallery": null
}
]
In my test, I want to test each type of element. Here is my code:
->assertJson(fn (AssertableJson $json) =>
$json->whereAllType([
'0.title' => 'string',
'0.gallery' => 'array|null',
'0.gallery.0.src' => 'string'
])
);
But I'm getting an error: Property [0.gallery.0.src] does not exist. Failed asserting that false is true.
I understand the meaning of the error, but please help me how do I check the properties inside an array(gallery) that may be empty (nullable)?
Please or to participate in this conversation.