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

pn523's avatar
Level 2

Json Validation fails in Laravel 6.x API

I am using laravel for backend API and postman to test it, my json validation fails.

My validation :

$validator = Validator::make($request->all(), [
            'name' => 'required|json|string|max:255|unique:users,name',
            'email' => 'required|json|email|unique:users,email',
            'password' => 'required|json|string|min:8',
            'phone' => 'required|json|numeric|digits:10|unique:users,phone',
            'country' => 'required|json|string|exists:countries,name',
            
]);

Postman request parameters :

{
    "name": "Joe",
    "email": "[email protected]",
    "password": "fe41e#tgdr5",
    "phone": "1234567890",
    "country": "US"
}

Postman response with error code 400 :

[
    "The name must be a valid JSON string.",
    "The email must be a valid JSON string.",
    "The password must be a valid JSON string.",
    "The country must be a valid JSON string."
]

I don't understand what's wrong here ?

0 likes
8 replies
slev1n's avatar

Try to var_dump($request->all()) and you will see, that your json input converted to ordinary key => value during request.

// post
{
    "name": "Joe",
    "email": "[email protected]",
    "password": "fe41e#tgdr5",
    "phone": "1234567890",
    "country": "US"
}

// controller
$request->name // Joe
$request->country // US

Json type parameter is

{
    "name": {"first": "Joe", "last": "Doe"},
    "email": "[email protected]",
    "password": "fe41e#tgdr5",
    "phone": "1234567890",
    "country": {"main": "US", "birth": "UK"}
}

or

{
    "data": {
        "name": "Joe",
        "email": "[email protected]",
        "password": "fe41e#tgdr5",
        "phone": "1234567890"
    }
}

In that case you can validate it like json type, otherwise it's just plain key -> (string) value

pn523's avatar
Level 2

I tried like this in postman :

{
    "data": {
        "name": "Joe",
        "email": "[email protected]",
        "password": "fe41e#tgdr5",
        "phone": "1234567890",
        "country": "US"
    }
}

My validation :

$validator = Validator::make($request->all(), [
            'data.name' => 'required|json|string|max:255|unique:users,name',
            'data.email' => 'required|json|email|unique:users,email',
            'data.password' => 'required|json|string|min:8',
            'data.phone' => 'required|json|numeric|digits:10|unique:users,phone',
            'data.country' => 'required|json|string|exists:countries,name',
            
        ]);

And I still get same response :

[
    "The data.name must be a valid JSON string.",
    "The data.email must be a valid JSON string.",
    "The data.password must be a valid JSON string.",
    "The data.country must be a valid JSON string."
]
Nakov's avatar

@pn The json rule expects the value that you are validating to be JSON object, not to be part of a JSON object, so as @slev1n showed you above, what you need is something like this:

"name": {"first": "Joe", "last": "Doe"},

Or just add a rule that the data needs to be JSON, but not each separate column, because "Joe" is not a valid JSON string as the error says.

pn523's avatar
Level 2

@Nakov here name is username and not a fully qualified name, in that case I would not have further parameters like first or last

Also as I am using API so data will be json but is it a good Idea to skip json validation ?

Nakov's avatar
Nakov
Best Answer
Level 73

@pn then don't add the rule json to the name, as it is not JSON but a normal string.

pn523's avatar
Level 2

Thank you guys for your help, it works as expected now

Nakov's avatar

@pn you can mark the answer that you found helpful as the "Best Answer". Happy that it is solved now.

Albertiss's avatar

@pn523 can you share with us the solution to fixe that, i have the same problem to validate à json data into sent json data.

Please or to participate in this conversation.