我们可能会提交这样一个请求,一个 嵌套的 JSON 对象,那么如何对 JSON 对象的那边元素进行验证呢?
### Save Post Comments
POST {{API_BASE_URL}}/module/v1/posts/2/comments
Content-Type: application/json
Accept: application/json
Authorization: Bearer {{TOKEN}}
{
"data": [
{
"media_ids": [1,2,3],
"title": "Title A",
"status": "A",
"comment": "Objectively seize progressive ROI before unique bandwidth."
},
{
"media_ids": [4,5,6],
"title": "Title B",
"status": "B",
"comment": "Objectively mesh sticky value for global data. "
}
]
}
方法是:
$data = $this->validate($request, [
'data' => ['required', 'array'],
'data.*.media_ids' => ['array'],
'data.*.title' => ['required', 'string'],
'data.*.status' => ['required', Rule::in(['A', 'B', 'C', 'D'])],
'data.*.comment' => ['nullable', 'string']
]);
print_r($data);