PHPackages                             danilopolani/laravel-json-validation-testing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. danilopolani/laravel-json-validation-testing

ActiveLibrary[Testing &amp; Quality](/categories/testing)

danilopolani/laravel-json-validation-testing
============================================

A better JSON validation errors testing

v2.1.0(5mo ago)915.6k↓21.7%[1 PRs](https://github.com/danilopolani/laravel-json-validation-testing/pulls)MITPHPPHP ^8.2CI passing

Since Mar 2Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/danilopolani/laravel-json-validation-testing)[ Packagist](https://packagist.org/packages/danilopolani/laravel-json-validation-testing)[ Docs](https://github.com/danilopolani/laravel-json-validation-testing)[ Fund](https://www.buymeacoffee.com/theraloss)[ RSS](/packages/danilopolani-laravel-json-validation-testing/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (10)Versions (9)Used By (0)

JSON Validation errors testing helper
=====================================

[](#json-validation-errors-testing-helper)

[![Latest Version on Packagist](https://camo.githubusercontent.com/09eddd19c0a40560e7f85868c893644f376ee1be52defcbd28e5ebd0bb29e5d9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64616e696c6f706f6c616e692f6c61726176656c2d6a736f6e2d76616c69646174696f6e2d74657374696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/danilopolani/laravel-json-validation-testing)[![GitHub Tests Action Status](https://camo.githubusercontent.com/8e1e220298fcdd0320777b0257ac2c8f74288a2fae6b0d75821cbfe1946d28c5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f64616e696c6f706f6c616e692f6c61726176656c2d6a736f6e2d76616c69646174696f6e2d74657374696e672f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/danilopolani/laravel-json-validation-testing/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/c49d522e1685bdbd865b12958c545409a02c1a745f549cffb2f807c5b7088a83/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f64616e696c6f706f6c616e692f6c61726176656c2d6a736f6e2d76616c69646174696f6e2d74657374696e672f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/danilopolani/laravel-json-validation-testing/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/99e29f01a81f7a1568c396cf06460b295b831141e4f045c0bc9fd1eae511c3df/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64616e696c6f706f6c616e692f6c61726176656c2d6a736f6e2d76616c69646174696f6e2d74657374696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/danilopolani/laravel-json-validation-testing)

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](https://github.com/laravel/framework/pull/41239) in the old merge request for Laravel).

Installation
------------

[](#installation)

You can install the package via composer:

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

Usage
-----

[](#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

[](#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:

```
