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

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

frontlayer/json-schema
======================

JSON Schema Validator

020PHP

Since Aug 20Pushed 6y ago1 watchersCompare

[ Source](https://github.com/frontlayer/json-schema)[ Packagist](https://packagist.org/packages/frontlayer/json-schema)[ RSS](/packages/frontlayer-json-schema/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

JSON Schema Draft 7 for PHP 7.2+
================================

[](#json-schema-draft-7-for-php-72)

[![Build Status](https://camo.githubusercontent.com/7cba73d24b90bd2475345959dd9513d5854557fe892303d91a34f6fe6c6086be/68747470733a2f2f7472617669732d63692e6f72672f66726f6e746c617965722f6a736f6e2d736368656d612e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/frontlayer/json-schema)[![Latest Stable Version](https://camo.githubusercontent.com/c768267e0d3dca7f75388091bbc85323d5724de6991530b76cb0db7856e11c33/68747470733a2f2f706f7365722e707567782e6f72672f66726f6e746c617965722f6a736f6e2d736368656d612f762f737461626c652e706e67)](https://packagist.org/packages/frontlayer/json-schema)[![Total Downloads](https://camo.githubusercontent.com/4b379b8712a7b67d9203ef5107c19ff85e0ef83e9e9bcb2d986a6704eb238a8f/68747470733a2f2f706f7365722e707567782e6f72672f66726f6e746c617965722f6a736f6e2d736368656d612f646f776e6c6f6164732e706e67)](https://packagist.org/packages/frontlayer/json-schema)

Validate `JSON` Structures against a given `Schema`.

Supports **all** official [JSON Schema Draft 7](https://github.com/json-schema-org/JSON-Schema-Test-Suite/tree/master/tests/draft7) tests

*\* Without two for bignum because PHP cannot validate them*

Intro
-----

[](#intro)

- PHP strict code
    - stdObjects instead of associative arrays
    - Strict mode `declare(strict_types=1);`
    - Return types `: void, : bool, : string, ...`
    - Method arguments types `bool $isSomething, string $someValue`
- More than 2300 tests
- Tests for OpenAPI with [Official OpenAPI Schema (Draft 4)](https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/schemas/v3.0/schema.json) and [PetStore Specification](https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v3.0/petstore.yaml)
- Support data casts e.g. for data from query/post/url paths
- Register custom formats
- Apply defaults in complex structures like `if`/`then`/`else`, `oneOf`/`allOf`/`anyOf`
- Clean code
- Well documented

What remains to be done `@todo`
-------------------------------

[](#what-remains-to-be-done-todo)

- Complete: `refRemote` and few tests with `ref`
- Add versions for composer
- Grammatically correct comments and exceptions

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

[](#installation)

### Composer

[](#composer)

```
composer require frontlayer/json-schema
```

How to start
------------

[](#how-to-start)

### Simple Example

[](#simple-example)

```
$data = 10;
$jsonSchema = '{
    "type": "integer",
    "minimum": 10
}';

$schema = new \FrontLayer\JsonSchema\Schema(json_decode($jsonSchema));
$validator = new \FrontLayer\JsonSchema\Validator();

try {
    $validator->validate($data, $schema);
} catch (\Exception $e) {
    print 'FAIL: ' . $e->getMessage();
    die(1);
}

print 'SUCCESS';
```

### Cast

[](#cast)

```
$data = (object)[
    'stringTest' => 123, // Integer > String
    'jsonStringTest' => '{"key": "value"}', // JSON string > PHP Object
    'integerTest' => '456', // String > Integer
    'numberTest' => '10.10', // String > Float/Double
    'booleanTest' => 'TRUE' // String > Boolean
];

$jsonSchema = (object)[
    'type' => 'object',
    'properties' => (object)[
        'stringTest' => (object)[
            'type' => 'string'
        ],
        'jsonStringTest' => (object)[
            'type' => 'object'
        ],
        'integerTest' => (object)[
            'type' => 'integer'
        ],
        'numberTest' => (object)[
            'type' => 'number'
        ],
        'booleanTest' => (object)[
            'type' => 'boolean'
        ]
    ]
];

$schema = new \FrontLayer\JsonSchema\Schema($jsonSchema);
$validator = new \FrontLayer\JsonSchema\Validator(\FrontLayer\JsonSchema\Validator::MODE_CAST);
$newData = $validator->validate($data, $schema);
var_dump($newData);
```

### Register Custom Format

[](#register-custom-format)

```
use \FrontLayer\JsonSchema\Schema;
use \FrontLayer\JsonSchema\Validator;
use \FrontLayer\JsonSchema\ValidationException;
use \FrontLayer\JsonSchema\SchemaException;

// Prepare data & schema
$data = '507f191e810c19729de860ea';

$jsonSchema = (object)[
    'type' => 'string',
    'format' => 'objectId'
];

// Initialize
$schema = new Schema($jsonSchema);
$validator = new Validator(Validator::MODE_CAST);

// Register new format
$validator->registerFormat('objectId', function (string $input): bool {
    return (bool)preg_match('/^[a-f\d]{24}$/i', $input);
});

// Validate and catch the problems
try {
    $validator->validate($data, $schema);
} catch (ValidationException $e) {
    print 'Validation Problem: ' . $e->getMessage();
    die(1);
} catch (SchemaException $e) {
    print 'Schema Structure Problem: ' . $e->getMessage();
    die(1);
} catch (\Exception $e) {
    print 'General Problem: ' . $e->getMessage();
    die(1);
}

print 'SUCCESS';
```

### Build whole week directly from the defaults in the different cases

[](#build-whole-week-directly-from-the-defaults-in-the-different-cases)

```
$data = (object)[
    'week' => (object)[]
];

$jsonSchema = '
{
    "properties": {
        "week": {
            "properties": {
                "day1": {
                    "default": "Monday"
                }
            },
            "if": {
                "type": "object"
            },
            "then": {
                "default": {
                    "day2": "Thursday"
                }
            },
            "allOf": [
                {
                    "properties": {
                        "day2": {
                            "const": "Thursday"
                        }
                    },
                    "default": {
                        "day3": "Wednesday"
                    }
                },
                {
                    "properties": {
                        "day3": {
                            "const": "Wednesday"
                        }
                    },
                    "default": {
                        "day4": "Tuesday"
                    }
                }
            ],
            "anyOf": [
                {
                    "properties": {
                        "day4": {
                            "const": "Fail"
                        }
                    },
                    "default": {
                        "day5": "Fail"
                    }
                },
                {
                    "properties": {
                        "day4": {
                            "const": "Tuesday"
                        }
                    },
                    "default": {
                        "day5": "Friday",
                        "day6": "Saturday"
                    }
                }
            ],
            "oneOf": [
                {
                    "type": "boolean",
                    "default": {
                        "day3": "Fail"
                    }
                },
                {
                    "properties": {
                        "day6": {
                            "const": "Saturday"
                        }
                    },
                    "default": {
                        "day7": "Sunday"
                    }
                }
            ]
        }
    }
}
';

$schema = new \FrontLayer\JsonSchema\Schema(json_decode($jsonSchema));
$validator = new \FrontLayer\JsonSchema\Validator(\FrontLayer\JsonSchema\Validator::MODE_APPLY_DEFAULTS);

$newData = (array)$validator->validate($data, $schema)->week;
ksort($newData);
var_dump(implode('; ', $newData)); // Monday;Thursday;Wednesday;Tuesday;Friday;Saturday;Sunday
```

Validation modes
----------------

[](#validation-modes)

FlagDescription`Validator::MODE_CAST`Cast the data to the specific format`Validator::MODE_REMOVE_ADDITIONALS`Remove additional properties &amp; additional items if they are not set to TRUE/FALSE`Validator::MODE_APPLY_DEFAULTS`Apply default values defined in the schemaYou can combine multiple flags with the bitwise operator `^`

```
$validator->validate($data, $schema, Validator::MODE_CAST ^ Validator::MODE_REMOVE_ADDITIONALS ^ Validator::MODE_APPLY_DEFAULTS)
```

Test the project with all official &amp; custom tests
-----------------------------------------------------

[](#test-the-project-with-all-official--custom-tests)

```
composer run test
```

###  Health Score

19

—

LowBetter than 9% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1092357?v=4)[Alexander Gavazov](/maintainers/agavazov)[@agavazov](https://github.com/agavazov)

### Embed Badge

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

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

###  Alternatives

[chaoswey/taiwan-id-validator

台灣身分證、統一編號驗證

319.9k](/packages/chaoswey-taiwan-id-validator)

PHPackages © 2026

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