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(10mo ago)09MITPHPCI passing

Since Jun 14Pushed 10mo 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 1mo ago

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

24

—

LowBetter than 32% of packages

Maintenance55

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity26

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

329d 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

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M677](/packages/phpspec-prophecy)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k118.8M753](/packages/brianium-paratest)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.0k](/packages/orchestra-testbench)[pdepend/pdepend

Official version of pdepend to be handled with Composer

954110.9M808](/packages/pdepend-pdepend)[phpstan/phpstan-symfony

Symfony Framework extensions and rules for PHPStan

78268.9M1.5k](/packages/phpstan-phpstan-symfony)

PHPackages © 2026

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