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

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

aklump/json-schema-validation
=============================

JSON Schema validation using PHP.

0.0.12(7mo ago)086↓50%2BSD-3-ClausePHPPHP &gt;=7.4

Since Feb 18Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/aklump/json-schema-validation)[ Packagist](https://packagist.org/packages/aklump/json-schema-validation)[ Docs](https://github.com/aklump/json-schema-validation)[ RSS](/packages/aklump-json-schema-validation/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (13)Used By (2)

JSON Schema Validation
======================

[](#json-schema-validation)

[![Validation](images/validation.jpg)](images/validation.jpg)

[![](https://camo.githubusercontent.com/1b2b046345901be7ddad85420f88bdab509bc37dfe7f79566a42370938204b61/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f7068702f616b6c756d702f646f6d2d74657374696e672d73656c6563746f7273)](https://camo.githubusercontent.com/1b2b046345901be7ddad85420f88bdab509bc37dfe7f79566a42370938204b61/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f7068702f616b6c756d702f646f6d2d74657374696e672d73656c6563746f7273) [![](https://camo.githubusercontent.com/884ba60b7b1c2c9d0201bef8278db217c255f907f54cc3cf68fe8e7609d414e0/68747470733a2f2f62616467656e2e6e65742f6769746875622f6c6963656e73652f616b6c756d702f6a736f6e2d736368656d612d76616c69646174696f6e)](https://camo.githubusercontent.com/884ba60b7b1c2c9d0201bef8278db217c255f907f54cc3cf68fe8e7609d414e0/68747470733a2f2f62616467656e2e6e65742f6769746875622f6c6963656e73652f616b6c756d702f6a736f6e2d736368656d612d76616c69646174696f6e)

Provides a suite of classes to help with JSON Schema validation.

Validate Data Using JSON Schema
-------------------------------

[](#validate-data-using-json-schema)

```
$path_to_schema = 'json_schema/foo.schema.json';
$schema_json = file_get_contents($path_to_schema);

// Create the validator for this schema.
$validate = new \AKlump\JsonSchema\ValidateWithSchema($schema_json, dirname($path_to_schema));

// Validate some data against the schema.
$errors = $validate(["lorem", "ipsum"]);

$is_valid = empty($errors);

// Display any errors.
if (!$is_valid) {
  foreach ($errors as $erro) {
    print $erro . PHP_EOL;
  }
}
```

Preparing Data for Validation
-----------------------------

[](#preparing-data-for-validation)

In most cases you should prepare data using `\AKlump\JsonSchema\JsonDecodeLossless`. This has the result of casting to mixed objects and arrays. This is a quirk of using PHP with JSON Schema that doesn't think in terms of associative arrays, but objects--like Javascript.

```
$data = ['foo' => [1, 2, 3]];
$data_to_validate = (new \AKlump\JsonSchema\JsonDecodeLossless())(json_encode($data));
// $data_to_validate->foo[0] === 1
```

Extract Default Values from JSON Schema
---------------------------------------

[](#extract-default-values-from-json-schema)

```
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "greeting": {
      "type": "string",
      "default": "Hello, World!"
    }
  }
}
```

```
$schema_json = file_get_contents('greeting.schema.json');
$defaults = (new \AKlump\JsonSchema\GetPropertyDefaults())($schema_json);
// 'Hello, World!' === $defaults['greeting'];
```

### Using Default Values

[](#using-default-values)

In these examples, `$dataset` is meant to represent user-provided values, which may need to be filled in with default values. **Be sure to heed the argument order for `array_replace_recursive()`.**

```
/** @var array $dataset An empty dataset example */
$dataset = [];
$dataset = array_replace_recursive($defaults, $dataset);
// 'greeting' gets the default.
$dataset['greeting'] === 'Hello, World!';

/** @var array $dataset Greeting is already set. */
$dataset = ['greeting' => 'Yo, dude!']
$dataset = array_replace_recursive($defaults, $dataset);
// 'greeting keeps the non-default value.
$dataset['greeting'] === 'Yo, dude!';

/** @var array $dataset Greeting is an empty string, will not be replaced. */
$dataset = ['greeting' => '']
$dataset = array_replace_recursive($defaults, $dataset);
// 'greeting' is still an empty string.
$dataset['greeting'] === '';
```

Relative Path $ref
------------------

[](#relative-path-ref)

```
.
└── json_schema
    ├── _definitions.schema.json
    └── foo.schema.json

```

*\_definitions.schema.json*

```
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$defs": {
    "title": {
      "type": "string"
    }
  }
}
```

*foo.schema.json*

```
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "title": {
      "$ref": "./_definitions.schema.json#/$defs/title"
    }
  }
}
```

Relative path references as shown above in *foo.schema.json* for the *title* property, are resolved based on the second argument to `ValidateWithSchema`:

```
$path_to_schema = 'json_schema/foo.schema.json';
ValidateWithSchema(file_get_contents($path_to_schema), dirname($path_to_schema);
```

As you might expect these are equivalent:

```
"$ref": "_definitions.schema.json#/$defs/title"
"$ref": "./_definitions.schema.json#/$defs/title"

```

Install with Composer
---------------------

[](#install-with-composer)

1. Because this is an unpublished package, you must define it's repository in your project's *composer.json* file. Add the following to *composer.json* in the `repositories` array:

    ```
    {
     "type": "github",
     "url": "https://github.com/aklump/json-schema-validation"
    }
    ```
2. Require this package:

    ```
    composer require aklump/json-schema-validation:^0.0

    ```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance62

Regular maintenance activity

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity38

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

Every ~53 days

Recently: every ~123 days

Total

12

Last Release

234d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/79476f3b82f9c766433c7eb401cbcfc636cd5d5ccf2b2e6b50ea81d1de6c2730?d=identicon)[aklump](/maintainers/aklump)

---

Top Contributors

[![aklump](https://avatars.githubusercontent.com/u/425737?v=4)](https://github.com/aklump "aklump (60 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/aklump-json-schema-validation/health.svg)

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

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)[xemlock/htmlpurifier-html5

HTML5 support for HTML Purifier

1052.9M11](/packages/xemlock-htmlpurifier-html5)

PHPackages © 2026

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