PHPackages                             carsdotcom/laravel-json-schema - 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. carsdotcom/laravel-json-schema

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

carsdotcom/laravel-json-schema
==============================

Json Schema validation for Laravel projects

v2.4.1(2mo ago)1036.7k—1.9%2[1 issues](https://github.com/carsdotcom/laravel-json-schema/issues)2MITPHPPHP ^8.1CI passing

Since Jul 6Pushed 1mo ago17 watchersCompare

[ Source](https://github.com/carsdotcom/laravel-json-schema)[ Packagist](https://packagist.org/packages/carsdotcom/laravel-json-schema)[ RSS](/packages/carsdotcom-laravel-json-schema/feed)WikiDiscussions development Synced 1mo ago

READMEChangelog (10)Dependencies (18)Versions (18)Used By (2)

laravel-json-schema
===================

[](#laravel-json-schema)

Use JsonSchema in Laravel apps

Purpose
-------

[](#purpose)

This library builds on the *outstanding* JsonSchema validator [opis/json-schema](https://opis.io/json-schema/)

The entire intent of this library is to make JsonSchema feel like a first class citizen in a Laravel project.

- Adds a new config file, `config/json-schema.php`, to configure your root directory for self-hosted schema files.
- Adds the `SchemaValidator` facade that can be used to instantiate the validator with appropriate loaders.
- Adds new PhpUnit assertions in the `Carsdotcom\JsonSchemaValidation\Traits\JsonSchemaAssertions` trait, such as validating that a mixed item validates for a specific schema.
- Most interestingly, it lets you use JsonSchema to validate incoming Requests bodies, and/or validate your own outgoing response bodies, all using JsonSchema schemas that you can then export into OpenAPI documentation.

Laravel Version Compatibility
-----------------------------

[](#laravel-version-compatibility)

This package supports Laravel `v9+`.

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

[](#installation)

```
composer require carsdotcom/laravel-json-schema

```

Using Laravel JSON Schema
-------------------------

[](#using-laravel-json-schema)

### Setup

[](#setup)

#### Config File

[](#config-file)

Copy the `json-schema.php` file from the `vendor/carsdotcom/laravel-json-schema/config` folder to your application's `config` folder.

#### Schema Storage

[](#schema-storage)

1. Create a `Schemas` folder under your application root folder, such as `app/Schemas`.
2. Create a new storage disk under the `disks` key within your application's `config/filesystem.php` file:

```
'disks' => [
    'schemas' => [
        'driver' => 'local',
        'root' => app_path('app/Schemas'), // must match the 'config.json-schema.local_base_prefix' value
    ]
]

```

3. Add your schema files to the `app/Schemas` folder. You may create subfolders to keep things organized.

#### Generate Enum Schemas

[](#generate-enum-schemas)

*This is an optional step, but can be super helpful.*

Note: Enums must be created either as a built-in PHP `enum` object or a `MyCLabs\Enum\Enum` class.

1. Add `use Carsdotcom\JsonSchemaValidation\Traits\GeneratesSchemaTrait;` to the declarations in the Enum.
2. Add a `SCHEMA` constant to the enum. It's value will be the relative path to your schema file, such as: `const SCHEMA = '/Acme/Enums/item_type.json';`
3. Run the `schema:generate` Artisan command.

Validating JSON Data Against a Schema
-------------------------------------

[](#validating-json-data-against-a-schema)

For this example, we'll be using these objects:

### Hosted JSON Schema File

[](#hosted-json-schema-file)

This is assumed to be stored in your `app/Schemas` folder as `Product.json`.

```
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/product.schema.json",
  "title": "Product",
  "description": "A product from Acme's catalog",
  "type": "object",
  "properties": {
    "productId": {
      "description": "The unique identifier for a product",
      "type": "integer"
    },
    "productName": {
      "description": "Name of the product",
      "type": "string"
    },
    "price": {
      "description": "The price of the product",
      "type": "number",
      "exclusiveMinimum": 0
    }
  },
  "required": [ "productId", "productName", "price" ]
}

```

### JSON Data to be Validated

[](#json-data-to-be-validated)

```
{
  "productId": 1,
  "productName": "An ice sculpture",
  "price": 12.50
}

```

### Application Code for Validation

[](#application-code-for-validation)

```
use Carsdotcom\JsonSchemaValidation\SchemaValidator;

SchemaValidator::validateOrThrow($json, 'Product.json');

```

Additional Functionality
------------------------

[](#additional-functionality)

### Getting the Content of a Schema File

[](#getting-the-content-of-a-schema-file)

```
use Carsdotcom\JsonSchemaValidation\SchemaValidator;

SchemaValidator::getSchemaContents('Product.json');

```

### Storing a Schema File at a Specific Location

[](#storing-a-schema-file-at-a-specific-location)

```
use Carsdotcom\JsonSchemaValidation\SchemaValidator;

SchemaValidator::getSchemaContents('Customer.json', $jsonSchemaForCustomer);

```

### Adding an In-Memory Schema File

[](#adding-an-in-memory-schema-file)

```
$schemaKey = (new SchemaValidatorService)->registerRawSchema($jsonSchema);

```

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance87

Actively maintained with recent releases

Popularity36

Limited adoption so far

Community24

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~122 days

Recently: every ~151 days

Total

9

Last Release

69d ago

Major Versions

v1.0.2 → v2.0.02024-06-26

### Community

Maintainers

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

![](https://www.gravatar.com/avatar/9c6c9dd822416d8cec97ed1b44d5f943aeb39b1adf2980cb819fc111c5cfaa61?d=identicon)[bbene](/maintainers/bbene)

---

Top Contributors

[![jwadhams](https://avatars.githubusercontent.com/u/1613739?v=4)](https://github.com/jwadhams "jwadhams (17 commits)")[![bbene](https://avatars.githubusercontent.com/u/502853?v=4)](https://github.com/bbene "bbene (15 commits)")[![skybluesofa](https://avatars.githubusercontent.com/u/1657128?v=4)](https://github.com/skybluesofa "skybluesofa (10 commits)")[![mattwills23](https://avatars.githubusercontent.com/u/26020585?v=4)](https://github.com/mattwills23 "mattwills23 (3 commits)")[![johnnybenson](https://avatars.githubusercontent.com/u/238022?v=4)](https://github.com/johnnybenson "johnnybenson (1 commits)")

---

Tags

laravelvalidationlibraryJsonSchema

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/carsdotcom-laravel-json-schema/health.svg)

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

###  Alternatives

[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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