PHPackages                             jtmcc/lara-schema-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. [Testing &amp; Quality](/categories/testing)
4. /
5. jtmcc/lara-schema-validation

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

jtmcc/lara-schema-validation
============================

Validate laravel json responses against JSON Schemas in pest or phpunit

v0.0.1(1y ago)014MITPHPCI passing

Since Jun 14Pushed 1y agoCompare

[ Source](https://github.com/J-T-McC/lara-schema-validation)[ Packagist](https://packagist.org/packages/jtmcc/lara-schema-validation)[ RSS](/packages/jtmcc-lara-schema-validation/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Lara Schema Validation
======================

[](#lara-schema-validation)

Validate Laravel JSON responses against [JSON Schemas](https://json-schema.org/specification).

This package leverages [Opis JSON Schema](https://opis.io/json-schema/) for schema validation and provides convenient methods for validating API responses in Laravel applications.

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

[](#installation)

Install the package via Composer:

```
composer require --dev jtmcc/lara-schema-validation
```

Usage
-----

[](#usage)

### PHPUnit Example

[](#phpunit-example)

You can use the `SchemaValidator` class to validate JSON responses against a schema file:

```
use JTMcC\LaraSchemaValidation\SchemaValidator;

public function testIndexValidation()
{
    // Act
    $response = $this->getJson(route('api.posts.index'));

    // Assert
    SchemaValidator::validateResponseCollection($response, 'post.json');
}

public function testShowValidation()
{
    // Act
    $response = $this->getJson(route('api.posts.show', 1));

    // Assert
    SchemaValidator::validateResponse($response, 'post.json');
}
```

### Pest Example

[](#pest-example)

This package extends Pest with custom expectations for schema validation:

```
test('it validates schema', function () {
    $response = $this->getJson(route('api.posts.show', 1));

    expect($response)->toMatchSchema('post.json');
});

test('it validates schema collection', function () {
    $response = $this->getJson(route('api.posts.index'));

    expect($response)->toMatchSchemaCollection('post.json');
});
```

### Schema Files

[](#schema-files)

Place your JSON schema files in the `tests/schemas` directory. For example: `tests/schemas/post.json`.

```
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "title": "Post",
  "properties": {
    "id": {
      "type": "integer",
      "description": "Primary ID of the post"
    },
    "uuid": {
      "type": "string",
      "format": "uuid",
      "description": "Unique identifier for the post"
    },
    "slug": {
      "type": "string",
      "description": "URL-friendly identifier for the post"
    },
    "status": {
      "type": "string",
      "enum": ["draft", "published", "archived"],
      "description": "Status of the post"
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "Tags associated with the post"
    },
    "published_at": {
      "type": "string",
      "format": "date-time",
      "description": "Publication timestamp"
    },
    "created_at": {
      "type": "string",
      "format": "date-time",
      "description": "Creation timestamp"
    },
    "updated_at": {
      "type": "string",
      "format": "date-time",
      "description": "Last update timestamp"
    },
    "comments" : {
      "type": "array",
      "items": {
        "$ref": "comment.json"
      },
      "description": "Comments relations associated with the post"
    }
  },
  "required": ["id", "uuid", "slug", "status", "tags", "published_at", "created_at", "updated_at"]
}
```

### Error Handling

[](#error-handling)

When validation fails, an `AssertionFailedError` is thrown with detailed information about the validation errors.

Example error message:

```
JSON schema validation failed at /tests/Feature/SchemaValidatorTest.php:52

Schema: /tests/schemas/post.json

message: The properties must match schema: uuid, slug, status, tags, published_at, created_at, updated_at
keyword: properties
path: /
errors:
  0:
    message: The data must match the 'uuid' format
    keyword: format
    path: /uuid
  1:
    message: The data (null) must match the type: string
    keyword: type
    path: /slug
  2:
    message: The data should match one item from enum
    keyword: enum
    path: /status
  3:
    message: The data (string) must match the type: array
    keyword: type
    path: /tags
  4:
    message: The data must match the 'date-time' format
    keyword: format
    path: /published_at
  5:
    message: The data must match the 'date-time' format
    keyword: format
    path: /created_at
  6:
    message: The data must match the 'date-time' format
    keyword: format
    path: /updated_at

```

Credits
-------

[](#credits)

- [Opis JSON Schema](https://opis.io/json-schema/) for the underlying validation library.

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](LICENSE).

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance48

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity27

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

385d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/73d0ea4ea29db1a282a4c71e5d5af7aedd30b9261bc9862043dc25d41af9d5ea?d=identicon)[J-T-McC](/maintainers/J-T-McC)

---

Top Contributors

[![J-T-McC](https://avatars.githubusercontent.com/u/48730964?v=4)](https://github.com/J-T-McC "J-T-McC (8 commits)")

---

Tags

json-schemalaravelpestphpunittesting

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jtmcc-lara-schema-validation/health.svg)

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

###  Alternatives

[hotmeteor/spectator

Testing helpers for your OpenAPI spec

3051.6M1](/packages/hotmeteor-spectator)[mcp/sdk

Model Context Protocol SDK for Client and Server applications in PHP

1.5k1.5M88](/packages/mcp-sdk)[friendsoftypo3/content-blocks

TYPO3 CMS Content Blocks - Content Types API | Define reusable components via YAML

103519.9k53](/packages/friendsoftypo3-content-blocks)[dms/phpunit-arraysubset-asserts

This package provides ArraySubset and related asserts once deprecated in PHPUnit 8

14429.2M361](/packages/dms-phpunit-arraysubset-asserts)[spurwork/spectator

Testing helpers for your OpenAPI spec

3051.5k](/packages/spurwork-spectator)[carsdotcom/laravel-json-schema

Json Schema validation for Laravel projects

1043.3k6](/packages/carsdotcom-laravel-json-schema)

PHPackages © 2026

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