PHPackages                             nestecha/laravel-json-api-validation - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. nestecha/laravel-json-api-validation

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

nestecha/laravel-json-api-validation
====================================

Lets you use Laravel native validation to return JSON API compliant errors.

5.0.0(2y ago)61.8kMITPHPPHP ^8.1|^8.2CI failing

Since Feb 9Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Nestecha/laravel-json-api-validation)[ Packagist](https://packagist.org/packages/nestecha/laravel-json-api-validation)[ Docs](https://github.com/nestecha/laravel-json-api-validation)[ RSS](/packages/nestecha-laravel-json-api-validation/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (14)Used By (0)

Creates JSON API compliant responses for errors
===============================================

[](#creates-json-api-compliant-responses-for-errors)

[![Latest Version on Packagist](https://camo.githubusercontent.com/eff4523b597f0ba5d959f114ffb044c893f83b06929b77faee18f976ae866339/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e657374656368612f6c61726176656c2d6a736f6e2d6170692d76616c69646174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nestecha/laravel-json-api-validation)[![Build Status](https://camo.githubusercontent.com/3672b1a0ff0a1518b25f7e6960ab98f831dadc1cc5a9be9d2d359f2a131ec3da/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6e657374656368612f6c61726176656c2d6a736f6e2d6170692d76616c69646174696f6e2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/nestecha/laravel-json-api-validation)[![Quality Score](https://camo.githubusercontent.com/64aabd58d4679a5795335ed386a8317970b9e31384c4e6b5e5e67276e4d45c93/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6e657374656368612f6c61726176656c2d6a736f6e2d6170692d76616c69646174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/nestecha/laravel-json-api-validation)[![Total Downloads](https://camo.githubusercontent.com/d37f82c308467ce26c6fd2f6a3e85eed7d78d68e5dcbfd6b6a6c6a6d40493f79/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e657374656368612f6c61726176656c2d6a736f6e2d6170692d76616c69646174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nestecha/laravel-json-api-validation)

This package helps returning JSON API compliant errors while using the native Laravel validation logic. Also, it lets you add unique codes to your validation rules which makes it easier on the consumer end.

Laravel / Lumen Versions
------------------------

[](#laravel--lumen-versions)

Laravel / LumenThis Package`^10.0``^5.0``^9.0``^4.0``^8.0``^3.0``^7.0``^2.0``^6.0``^1.0`Installation
------------

[](#installation)

You can install the package via composer:

- Laravel / Lumen 10

```
composer require "nestecha/laravel-json-api-validation":"^5.0"
```

- Laravel / Lumen 9

```
composer require "nestecha/laravel-json-api-validation":"^4.0"
```

- Laravel / Lumen 8

```
composer require "nestecha/laravel-json-api-validation":"^3.0"
```

- Laravel / Lumen 7

```
composer require "nestecha/laravel-json-api-validation":"^2.0"
```

- Laravel / Lumen 6

```
composer require "nestecha/laravel-json-api-validation":"^1.0"
```

Go in `App\Exceptions\Handler.php` and change the `render` method :

```
/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
 */
public function render($request, Exception $exception)
{
    if ($exception instanceof \Nestecha\LaravelJsonApiValidation\Exception\JsonApiValidationException) {
        $responseFactory = new \Nestecha\LaravelJsonApiValidation\ResponseFactory();
        return $responseFactory->fromErrors($exception->errors()->getArrayCopy());
    }

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

Then, in your controller :

```
public function home(Request $request)
{
    $validator = new \Nestecha\LaravelJsonApiValidation\JsonApiValidator();
    $validator->validateAsJsonApi($request->all(), ['title' => 'required']);

    // ...
}
```

This would yield :

```
{
    "errors": [
        {
            "status": "422",
            "title": "Unprocessable Entity",
            "detail": "The title field is required.",
            "source": {
                "pointer": "\/data\/attributes\/title",
                "value": ""
            },
            "meta": {
                "failed": {
                    "rule": "required"
                }
            }
        }
    ]
}
```

### For Laravel :

[](#for-laravel-)

To add a code to the errors, use this artisan command to copy the default config file to your config folder.

```
php artisan vendor:publish --tag=config
```

### For Lumen :

[](#for-lumen-)

To add a code to the errors, [a base config file is available](https://github.com/Nestecha/laravel-json-api-validation/blob/master/config/config.php), simply copy paste it into your config folder as `json-api-validation.php`. Then in `bootstrap/app.php` add this line :

```
$app->configure('json-api-validation');
```

### To customize the config filename :

[](#to-customize-the-config-filename-)

`json-api-validation.php` is the default config filename. You can customize the validator by passing a string in the constructor :

```
public function home(Request $request)
{
    $validator = new JsonApiValidator('name-of-your-config-file');
    $validator->validateAsJsonApi($request->all(), ['title' => 'required']);
}
```

### For custom rules :

[](#for-custom-rules-)

When using Laravel [custom rules](https://laravel.com/docs/master/validation#using-rule-objects) :

```
class UppercaseRule implements Rule
{
    /**
     * @inheritDoc
     */
    public function passes($attribute, $value)
    {
        return strtoupper($value) === $value;
    }

    /**
     * @inheritDoc
     */
    public function message()
    {
        return "The :attribute should be uppercase.";
    }
}
```

To add an error code in the config, you should use the name in kebab-case format :

```
return [
    'uppercase-rule' => ['code' => 'VALIDATION_ERROR_UPPERCASE'],
];
```

The error will format the rule name in kebab-case in the meta field :

```
{
    "errors": [
        {
            "status": "422",
            "title": "Unprocessable Entity",
            "detail": "The title should be uppercase.",
            "source": {
                "pointer": "\/data\/attributes\/title",
                "value": "lowercase_title"
            },
            "meta": {
                "failed": {
                    "rule": "uppercase-rule"
                }
            }
        }
    ]
}
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Chamaillard Steve](https://github.com/nestecha)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~112 days

Recently: every ~101 days

Total

12

Last Release

1048d ago

Major Versions

1.0.0 → 2.0.02020-08-17

2.0.0 → 3.0.02020-09-28

3.0.3 → 4.0.02022-05-16

4.0.4 → 5.0.02023-06-26

PHP version history (5 changes)1.0.0PHP ^7.1

2.0.0PHP ^7.2

3.0.1PHP ^7.3|^8.0

4.0.0PHP ^8.1

4.0.2PHP ^8.1|^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/5debbaea7130b783921a65d5561ebbe20ce55991f96c843a24ebb7763f9eab0e?d=identicon)[Nestecha](/maintainers/Nestecha)

---

Top Contributors

[![Nestecha](https://avatars.githubusercontent.com/u/7679436?v=4)](https://github.com/Nestecha "Nestecha (45 commits)")

---

Tags

nestechalaravel-json-api-validation

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nestecha-laravel-json-api-validation/health.svg)

```
[![Health](https://phpackages.com/badges/nestecha-laravel-json-api-validation/health.svg)](https://phpackages.com/packages/nestecha-laravel-json-api-validation)
```

###  Alternatives

[spatie/laravel-honeypot

Preventing spam submitted through forms

1.6k6.0M60](/packages/spatie-laravel-honeypot)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel and Lumen

3853.3M1](/packages/axlon-laravel-postal-code-validation)[sunspikes/clamav-validator

Custom Laravel 5 anti-virus validator for file uploads.

3651.8M3](/packages/sunspikes-clamav-validator)[laravel-validation-rules/credit-card

Validate credit card number, expiration date, cvc

2412.2M5](/packages/laravel-validation-rules-credit-card)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
