PHPackages                             beblife/schema-validation-laravel - 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. beblife/schema-validation-laravel

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

beblife/schema-validation-laravel
=================================

Validate HTTP-requests using JSON-schema objects

v0.4.0(3y ago)011.3k1MITPHPPHP ^7.4|^8.0

Since May 1Pushed 3y ago1 watchersCompare

[ Source](https://github.com/beblife/schema-validation-laravel)[ Packagist](https://packagist.org/packages/beblife/schema-validation-laravel)[ RSS](/packages/beblife-schema-validation-laravel/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (10)Dependencies (7)Versions (12)Used By (0)

Schema Validation Laravel
=========================

[](#schema-validation-laravel)

Validate HTTP-requests using OpenAPI specification files or JSON-schema objects in Laravel.

[![Latest Version on Packagist](https://camo.githubusercontent.com/1adc410a101f622880435ff030b1330eaae1bef615855ce8162f3eedc555ddd0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6265626c6966652f736368656d612d76616c69646174696f6e2d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/beblife/schema-validation-laravel)[![PHP from Packagist](https://camo.githubusercontent.com/50daa11a79bf802bba681f5bc762dd4cdbc90982ec685bcbbc27ac146962376a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6265626c6966652f736368656d612d76616c69646174696f6e2d6c61726176656c3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/50daa11a79bf802bba681f5bc762dd4cdbc90982ec685bcbbc27ac146962376a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6265626c6966652f736368656d612d76616c69646174696f6e2d6c61726176656c3f7374796c653d666c61742d737175617265)[![CI](https://github.com/beblife/schema-validation-laravel/workflows/CI/badge.svg?style=flat-square)](https://github.com/beblife/schema-validation-laravel/workflows/CI/badge.svg?style=flat-square)

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

[](#installation)

This package can be installed through Composer:

```
composer require beblife/schema-validation-laravel

```

After that can publish the configuration file with this command:

```
php artisan vendor:publish --provider="Beblife\SchemaValidation\SchemaValidationServiceProvider"

```

Once published you will have a `config/schema-validation.php` file that looks like this:

```
return [
    'spec_path' => env('SCHEMA_VALIDATION_SPEC_PATH', ''),

    'response' => [
        'status' => 400,
    ],
];
```

You can define the spec path as a `.env` variable or hardcode the absolute path in the configuration file itself. The status code when a validation exception is thrown can also be customised here.

Usage
-----

[](#usage)

### Validating Requests

[](#validating-requests)

This package provides a macro on the `Illumnite\Http\Request::class` that will validate the request's schema.

```
use Illuminate\Http\Request;

class UserRegistrationRequestHandler extends Controller
{
    public function __invoke(Request $request)
    {
        $request = $request->validateSchema();

        // Validate any additional rules (if any) with $request->validate(...)

        // Process the valid request ...
    }
}
```

When the `validateSchema()` method is called the package will search for a matching path defined in the configured OpenAPI specification file and validate the request against the schema.

There are two possible exceptions that can occur when the validation takes place:

**UnableToValidateSchema**

When a path can't be found in the specification file this exception is thrown.

**InvalidSchema**

When the request does not match the schema defined in the specification file this exception is thrown. This exception extends the Laravel `ValidationException` which results in a *400 : Bad Request* with the following format:

```
{
    "message": "The given data is invalid.",
    "errors": {
        "property": [
            "The validation message",
        ]
    }
}
```

The package also provides a trait that can be added on a form request to validate the schema. Behind the scenes this trait hooks into the `prepareForValidation()` method to start validating the request's schema. Afterwards any additional validation defined in the `rules()` method will be handled by the Laravel framework.

```
use Beblife\SchemaValidation\ValidateSchema;
use Illuminate\Foundation\Http\FormRequest;

class UserRegistrationRequest extends FormRequest
{
    use ValidateSchema;

    public function rules(): array
    {
        return [
            // Your other validation rules ...
        ];
    }
}
```

The schema to use for validation will be resolved automatically from the configured OpenAPI specification file. This can be overwritten by defining a `schema()` on the `FormRequest::class`.

```
use Beblife\SchemaValidation\Schema;
use Beblife\SchemaValidation\ValidateSchema;
use Illuminate\Foundation\Http\FormRequest;

class UserRegistrationRequest extends FormRequest
{
    use ValidateSchema;

    public function schema(): Schema
    {
        return // Your custom defined schema ...
    }

    public function rules(): array
    {
        return [
            // Your other validation rules ...
        ];
    }
}
```

### Defining Schema's

[](#defining-schemas)

By default the package will use the schema's defined the configured specification when validating requests. There is also the option to pass a `Beblife\SchemaValidation\Schema::class` to the `validateSchema()` method using the provided facade:

#### From array

[](#from-array)

```
$schema = Beblife\SchemaValidation\Facades\Schema::fromArray([
    'type' => 'object',
    'properties' => [
        'field' => [
            'type' => 'string',
            'enum' => [
                'option 1',
                'option 2',
            ]
        ]
    ]
]);
```

#### From File

[](#from-file)

```
// from a JSON-file
$schema = Beblife\SchemaValidation\Facades\Schema::fromFile('/path/to/a/schema/file.json'));
// from a YAML-file
$schema = Beblife\SchemaValidation\Facades\Schema::fromFile('/path/to/a/schema/file.yaml'));
```

#### From Class

[](#from-class)

```
use Beblife\SchemaValidation\Schema;

class MyCustomSchema implements Schema
{
    public function toArray(): array
    {
        return [
            'type' => 'object',
            'properties' => [
                // ...
            ]
        ];
    }
}
```

License
-------

[](#license)

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

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.7% 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 ~49 days

Total

11

Last Release

1396d ago

PHP version history (2 changes)v0.1.1PHP ^7.2|^8.0

v0.3.0PHP ^7.4|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/f586584832cd3efc41160188f4c82f509a352f95d3c738a8087a2e96508a5eb4?d=identicon)[beblife](/maintainers/beblife)

---

Top Contributors

[![beblife](https://avatars.githubusercontent.com/u/9271492?v=4)](https://github.com/beblife "beblife (18 commits)")[![nickknissen](https://avatars.githubusercontent.com/u/204970?v=4)](https://github.com/nickknissen "nickknissen (1 commits)")

---

Tags

json-schemaopen-apischema validation laravel

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/beblife-schema-validation-laravel/health.svg)

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

###  Alternatives

[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.3k543.5M2.6k](/packages/aws-aws-sdk-php)[oat-sa/tao-core

TAO core extension

66143.7k120](/packages/oat-sa-tao-core)[spatie/laravel-honeypot

Preventing spam submitted through forms

1.6k6.8M75](/packages/spatie-laravel-honeypot)[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M46](/packages/spatie-laravel-pdf)[aporat/store-receipt-validator

PHP receipt validator for Apple App Store and Amazon Appstore

6544.0M12](/packages/aporat-store-receipt-validator)[eslazarev/wildberries-sdk

Wildberries OpenAPI clients (generated).

252.5k](/packages/eslazarev-wildberries-sdk)

PHPackages © 2026

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