danilopolani/laravel-json-validation-testing

A better JSON validation errors testing
20,199 9
Install
composer require danilopolani/laravel-json-validation-testing
Latest Version:v2.2.0
PHP:^8.2
License:MIT
Last Updated:Apr 17, 2026
Links: GitHub  ·  Packagist
Maintainer: Theraloss

JSON Validation errors testing helper

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A simple library to help testing JSON validation errors by rules.

The current way to test HTTP errors is broken: it tests that the validation fails or you have to manually specify the error message. With this package you'll just need to specify the validation rule that fails and it builds the error message to be sure at 100% that it fails and what fails. (Further reading here in the old merge request for Laravel).

Installation

You can install the package via composer:

composer require --dev danilopolani/laravel-json-validation-testing

Usage

The package provides an helper to retrieve a compiled error message:

use DaniloPolani\JsonValidation\JsonValidation;

JsonValidation::getRuleErrorMessage('foo', 'required');

// => ["The foo field is required."]

However, if you need to test that your HTTP APIs, the package ships with a brand new assertJsonValidationErrorRule assertion to make your life easier:

it('throws validation error', function () {
    $this->postJson('/')
        ->assertJsonValidationErrorRule('foo', 'required');
});

It supports as well dynamic rules, such as between, size, max etc. You just need to specify the type of rule you want to apply:

it('throws validation error', function () {
    $this->postJson('/')
        ->assertJsonValidationErrorRule('foo', 'between.string:1,5') // The foo must be between 1 and 5 characters.
        ->assertJsonValidationErrorRule('foo', 'size.array:3'); // The foo must contain 3 items.
});

You can even test multiple validation errors at once by providing an array of field => rule as argument:

use DaniloPolani\JsonValidation\JsonValidation;

it('throws validation error', function () {
    $this->postJson('/')
        ->assertJsonValidationErrorRule([
            'foo' => 'required',
            'bar' => 'required_array_keys:foo,baz',
        ]);
});

Custom rules

If you want to test a custom rule, make sure it implements the interface DaniloPolani\JsonValidation\Contracts\HasRuleMessage, needed to extract the failing message to check against.

For example, a custom Rule would look like this:

<?php
 
namespace App\Rules;
 
use Closure;
use DaniloPolani\JsonValidation\Contracts\HasRuleMessage;
use Illuminate\Contracts\Validation\ValidationRule;
 
class Uppercase implements ValidationRule, HasRuleMessage
{
    /**
     * Run the validation rule.
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (strtoupper($value) !== $value) {
            $fail($this->message());
        }
    }

    public function message(): string
    {
        return 'The :attribute must be uppercase.';
    }
}

And then you can use it in your assert function:

Of course you can provide your own custom validation Rules:

it('throws validation error', function () {
    $this->postJson('/')
        ->assertJsonValidationErrorRule('foo', new Uppercase());
});

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

641,106,524 9,700
laravel/framework

The Laravel Framework.

593,882,149 34,959
laravel/tinker

Powerful REPL for the Laravel framework.

502,645,077 7,441
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

418,291,778 609
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

394,964,158 4,660

Version History

Version Released PHP Laravel License
v2.2.0 ^8.2 ^10.0|^11.0|^12.0|^13.0 MIT
v2.1.0 ^8.2 ^10.0|^11.0|^12.0 MIT
v2.0.0 ^8.2 ^10.0|^11.0 MIT
v1.0.3 ^8.0 ^9.0|^10.0 MIT
v1.0.2 ^8.0 ^9.0 MIT