murilo's avatar
Level 10

Pest assertJsonValidationErrors it is not working , showing errors

hello I saw an example of working with laravel errors i pest , to verify those errors that are vaidating . so it should get the erors in my request here -

I have in my Form , a resquest that is validation the password - and it shold show here that the password is required

   postJson(config('app.url').'/api/admin/users/store', [] )->assertStatus(422)->assertJsonValidationErrors([
        'password' => trans('validation.required', ['attribute' => 'password'])
    ]);

if I give a test , it shows this error -

 39▕ test('store user verify errors', function () {
  ➜  40▕     postJson(config('app.url').'/api/admin/users/store', [] )->assertStatus(422)->assertJsonValidationErrors([
     41▕         'password' => trans('validation.required', ['attribute' => 'password'])
     42▕     ]);
     43▕ });

if I dump , it shows -

 postJson(config('app.url').'/api/admin/users/store', [] )->dump();

{#2678
  +"message": {#2655
    +"password": array:1 [
      0 => "The password field is required."
    ]
  }
  +"code": 422
} 

well , at the example , that I am following , the author that developed the tutorial , the dump was like that ( with a ERROR KEY ) , and in my validation does not has this ERROR KEY -


{#2678
  +"message": {#2655
 "errors": {
    +"password": array:1 [
      0 => "The password field is required."
    ]
}
  }
  +"code": 422
} 

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It seems like the issue you're encountering is related to the structure of the JSON response your test is receiving. The assertJsonValidationErrors method expects the validation errors to be nested under an errors key, which is the default structure Laravel uses when handling validation errors. However, your application's error response structure does not include this errors key, which is why the test is failing.

To resolve this issue, you can either modify your application's error response format to include the errors key or adjust your test to correctly handle the current format of your JSON response.

Option 1: Modify the JSON response format

You can adjust your exception handler to format validation errors with an errors key. This can typically be done in the render method of your App\Exceptions\Handler class. Here's a basic example:

public function render($request, Throwable $exception)
{
    if ($exception instanceof ValidationException) {
        return response()->json([
            'message' => 'The given data was invalid.',
            'errors' => $exception->errors(),
        ], 422);
    }

    return parent::render($request, $exception);
}

Option 2: Adjust the test to match the current response format

If you prefer to keep the current error format, you need to adjust your test to correctly assert the structure of the JSON. Since your errors are directly under the message key, you can use assertJson to manually check for the presence of the password error:

test('store user verify errors', function () {
    postJson(config('app.url').'/api/admin/users/store', [])
        ->assertStatus(422)
        ->assertJson([
            'message' => [
                'password' => [
                    'The password field is required.'
                ]
            ]
        ]);
});

This test will directly check that the password validation error is present in the message key of your JSON response.

Choose the option that best fits your project's requirements. If you are following a tutorial or a certain coding standard that expects the errors key, modifying the application's error response format might be the better approach.

Please or to participate in this conversation.