PHPackages                             hunomina/json-data-validator - 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. hunomina/json-data-validator

Abandoned → [hunomina/data-validator](/?search=hunomina%2Fdata-validator)Library[Validation &amp; Sanitization](/categories/validation)

hunomina/json-data-validator
============================

Library for json schemas validation

3.1.0(6y ago)1579[1 PRs](https://github.com/hunomina/json-data-validator/pulls)proprietaryPHPPHP 7.4CI failing

Since Apr 29Pushed 3y ago1 watchersCompare

[ Source](https://github.com/hunomina/json-data-validator)[ Packagist](https://packagist.org/packages/hunomina/json-data-validator)[ RSS](/packages/hunomina-json-data-validator/feed)WikiDiscussions master Synced yesterday

READMEChangelog (4)Dependencies (1)Versions (29)Used By (0)

Json Data Validator
===================

[](#json-data-validator)

[![Build Status](https://camo.githubusercontent.com/66c7af52b4f044c2633c2f76158c8158c1e0154484af1ee36aca372ef0fa447d/68747470733a2f2f7472617669732d63692e636f6d2f68756e6f6d696e612f646174612d76616c696461746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/hunomina/data-validator)[![codecov](https://camo.githubusercontent.com/59f4a7c67f2868b39bf82464de6e21095aa85c3ae88af6b1a26b51582d278542/68747470733a2f2f636f6465636f762e696f2f67682f68756e6f6d696e612f646174612d76616c696461746f722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/hunomina/data-validator)

**Description :** Library for data validation based on data schemas

This project is licensed under the terms of the MIT license.

Interfaces and classes
----------------------

[](#interfaces-and-classes)

### [DataType](https://github.com/hunomina/json-data-validator/blob/master/src/Data/DataType.php)

[](#datatype)

Allows to encapsulate the data into an object and format it in order to be validated using a [DataSchema](https://github.com/hunomina/json-data-validator/blob/master/src/Schema/DataSchema.php).

### [JsonData](https://github.com/hunomina/json-data-validator/blob/master/src/Data/JsonData.php)

[](#jsondata)

[JsonData](https://github.com/hunomina/json-data-validator/blob/master/src/Data/JsonData.php) implements [DataType](https://github.com/hunomina/json-data-validator/blob/master/src/Data/DataType.php).

`JsonData::format()` uses `json_decode()` to format json string into php array.

---

### [DataRule](https://github.com/hunomina/json-data-validator/blob/master/src/Rule/Rule.php)

[](#datarule)

Allows to validate DataType based on rule specifications (is the data optional ?, is the data allowed to be...).

### [JsonRule](https://github.com/hunomina/json-data-validator/blob/master/src/Rule/JsonRule.php)

[](#jsonrule)

[JsonRule](https://github.com/hunomina/json-data-validator/blob/master/src/Rule/JsonRule.php) implement [DataRule](https://github.com/hunomina/json-data-validator/blob/master/src/Rule/Rule.php).

[JsonRule](https://github.com/hunomina/json-data-validator/blob/master/src/Rule/JsonRule.php) can validate :

Type/CheckNullLengthPatternMin/MaxEnumDate formatEmptyString✅✅✅✅✅✅Character✅✅✅Number✅✅✅Integer✅✅✅Float✅✅✅BooleanTyped List✅✅✅✅---

### [DataSchema](https://github.com/hunomina/json-data-validator/blob/master/src/Schema/DataSchema.php)

[](#dataschema)

[DataSchema](https://github.com/hunomina/json-data-validator/blob/master/src/Schema/DataSchema.php) is the library main class. It allows to validate [DataType](https://github.com/hunomina/json-data-validator/blob/master/src/Data/DataType.php) based on sub-schemas and [DataRule](https://github.com/hunomina/json-data-validator/blob/master/src/Rule/Rule.php).

`DataSchema::validate()` method allows this validation. If `DataSchema::validate()` does not validate the [DataType](https://github.com/hunomina/json-data-validator/blob/master/src/Data/DataType.php) it throws an InvalidDataException.

### [JsonSchema](https://github.com/hunomina/json-data-validator/blob/master/src/Schema/JsonSchema.php)

[](#jsonschema)

[JsonSchema](https://github.com/hunomina/json-data-validator/blob/master/src/Schema/JsonSchema.php) implements [DataSchema](https://github.com/hunomina/json-data-validator/blob/master/src/Schema/DataSchema.php) and validates [JsonData](https://github.com/hunomina/json-data-validator/blob/master/src/Data/JsonData.php) using [JsonRule](https://github.com/hunomina/json-data-validator/blob/master/src/Rule/JsonRule.php).

How it works
------------

[](#how-it-works)

See [tests](https://github.com/hunomina/json-data-validator/tree/master/tests) for examples

A [JsonSchema](https://github.com/hunomina/json-data-validator/blob/master/src/Schema/JsonSchema.php) has a type : `object` or `list`.

Objects are composed of rules and "child" schemas if needed.

This is a schema definition :

```
use hunomina\Validator\Json\Schema\Json\JsonSchema;

$schema = new JsonSchema([
    'success' => ['type' => 'bool'],
    'error' => ['type' => 'string', 'null' => true],
    'user' => ['type' => 'object', 'null' => true, 'optional' => true, 'schema' => [
        'name' => ['type' => 'string'],
        'age' => ['type' => 'int']
    ]]
]);
```

Schemas are just php arrays passe to `JsonSchema::setSchema()` method.

This schema is composed of 3 elements :

- a rule `success` which :

    - is a boolean
    - can not be null
    - is not optional
- a rule `error` which :

    - is a string
    - can be null
    - is not optional
- a "child" schema `user` which :

    - is an object and therefor is represented by a schema which contains 2 elements : a `name` (string) and an `age` (integer)
    - can be null
    - is optional

When a data unit is being validated using this schema by calling the `JsonSchema::validate()` method, the schema will check recursively if the data respects the rules and the "child" schemas.

If the data has :

- a boolean element `success`
- a null or string element `error`
- an optionally, null or object element `user` which must have :
    - a string element `name`
    - an integer element `age`

This data is valid :

```
use hunomina\Validator\Json\Data\Json\JsonData;

$data = new JsonData([
    'success' => true,
    'error' => null,
    'user' => [
        'name' => 'test',
        'age' => 10
    ]
]);
```

This one is not :

```
use hunomina\Validator\Json\Data\Json\JsonData;

$data = new JsonData([
    'success' => true,
    'error' => null,
    'user' => 'test'
]);
```

As said earlier, rules can be used to validate length or data pattern.

This schema uses the pattern validation on the `name` element and the length validation on the `geolocation` element :

```
use hunomina\Validator\Json\Schema\Json\JsonSchema;

$schema = new JsonSchema([
    'name' => ['type' => 'string', 'pattern' => '/^[a-z]+$/'],
    'geolocation' => ['type' => 'integer-list', 'length' => 2]
]);
```

When calling the `JsonSchema::validate()` method, the schema will recursively check all the rule set and "child" schemas. If one rule or one "child" schema is invalid, `JsonSchema::validate()` returns `false`.

The "first level" schema is an `object` typed schema. It could be changed but is not meant to.

Finally, if a "child" schema is typed as an `object`, the schema will validate it as described above. If it's typed as a `list`, the schema will simply check each element of the data as an `object` type using the given "child" schema.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity70

Established project with proven stability

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

Recently: every ~2 days

Total

27

Last Release

2324d ago

Major Versions

1.10.0 → 2.0.02019-12-21

2.2.1 → 3.0.02019-12-29

### Community

Maintainers

![](https://www.gravatar.com/avatar/37975d851f2946e4fab563ba4558e2b81908d933a4a550da65d1ac8a15a207e0?d=identicon)[hunomina972](/maintainers/hunomina972)

---

Top Contributors

[![hunomina](https://avatars.githubusercontent.com/u/16224681?v=4)](https://github.com/hunomina "hunomina (201 commits)")

---

Tags

composercomposer-packagecomposer-projectdata-validationphpphp74

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hunomina-json-data-validator/health.svg)

```
[![Health](https://phpackages.com/badges/hunomina-json-data-validator/health.svg)](https://phpackages.com/packages/hunomina-json-data-validator)
```

###  Alternatives

[webmozart/assert

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

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[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)

PHPackages © 2026

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