Nael.Saeed
2 months ago
24
11
Laravel

Form Request Validation for JSON Parameter

Posted 2 months ago by Nael.Saeed

Hi there,

I have an AJAX request sent to my API. This request has a parameter that should contain a JSON formatted data. I want to validate that it is actually JSON. Laravel form request has a 'json' validation rule, but the problem I am facing is that the parameter that I want to validate for JSON gets converted into an array before validation. So how am I supposed to prevent this conversion and do the validation?

Example request data:


{
    "id": 1,
    "name": "John",
    "json_data": {
        "data1": [

        ],
        "data2": [

        ]
    }
}


validation:

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{

    return [
        'id' => [
            'required'  
        ],
        'title' => [
            'required',
            'string',
        ],
        'json_data' => [
            'required',
            'json',
        ],
    ];
}

Please sign in or create an account to participate in this conversation.