PHPackages                             rethink/json-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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. rethink/json-validator

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

rethink/json-validator
======================

A json validator

v0.1.0(8y ago)5339.6k↓30%4MITPHPPHP &gt;=7.0.0

Since Jul 17Pushed 6y ago5 watchersCompare

[ Source](https://github.com/rethinkphp/json-validator)[ Packagist](https://packagist.org/packages/rethink/json-validator)[ RSS](/packages/rethink-json-validator/feed)WikiDiscussions master Synced 1mo ago

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

JSON Validator
==============

[](#json-validator)

A JSON Validator that designed to be elegant and easy to use.

Motivation
----------

[](#motivation)

JSON Validation is a common task in automated API testing, JSON-Schema is complex and not easy to use, so i created this library to simplify the JSON validation process and made JSON validation more elegant and fun.

Features
--------

[](#features)

- JSON Schema validation, useful for automated API testing
- Custom type support, it is possible to define your custom types and reuse it everywhere
- Nullable type support
- More is coming...

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

[](#installation)

You can install the latest version of JSON validator with the following command:

```
composer require  rethink/json-validator:dev-master
```

Documentation
-------------

[](#documentation)

### Types

[](#types)

By default, JSON Validator shipped with seven kinds of built-in types:

- integer
- double
- boolean
- string
- number
- array
- object

Besides the built-in types, it is possible to define your custom type via `defineType()` method.

The following code snippets shows how we can define custom types through array or callable.

#### 1. Define a composite type

[](#1-define-a-composite-type)

```
$validator->defineType('User', [
    'name' => 'string',
    'gender' => 'string',
    'age' => '?integer',
    'rating' => '?integer|boolean',
]);
```

This example defines a custom type named `User`, which have four properties. name and gender require be a string, age requires be an integer but allows to be nullable, and rating required to integer or boolean and allows to be null.

#### 2. Define a list type

[](#2-define-a-list-type)

```
$validator->defineType('UserCollection', ['User']);
```

This defines `UserCollection` to be an array of `User`. In order to define a list type, the definition of the type much contains only one element.

#### 3. Define a type in callable

[](#3-define-a-type-in-callable)

```
$validator->defineType('timestamp', function ($value) {
    if ((!is_string($value) && !is_numeric($value)) || strtotime($value) === false) {
        return false;
    }

    $date = date_parse($value);

    return checkdate($date['month'], $date['day'], $date['year']);
});
```

It is also possible to define a type using a callable, which is useful to perform some validation on the data. Such as the example above defined a timestamp type, that requires the data to be a valid datetime.

### Validate a Type

[](#validate-a-type)

We can validate a type by the following two steps:

#### 1. Create a Validator instance

[](#1-create-a-validator-instance)

```
use rethink\jsv\Validator;

$validator = new Validator();
// $validator->defineType(...)  Add your custom type if necessary
```

#### 2. Preform the validation

[](#2-preform-the-validation)

```
$matched = $validator->matches($data, 'User');
if ($matched) {
    // Validation passed
} else {
    $errors = $validator->getErrors();
}
```

This example will check whether the given `$data` matches the type `User`, if validation fails, we can get the error messages through `getErrors()` method.

### Strict Mode

[](#strict-mode)

In some situations, we may want an object matches our type strictly, we can utilizing `strict mode` to achieve this, the following is the example:

```
$data = [
    'name' => 'Bob',
    'gender' => 'Male',
    'age' => 19,
    'phone' => null, // This property is unnecessary
];
$matched = $validator->matches($data, 'User', true); // strict mode is turned on
var_dump($matched); // false is returned
```

Related Projects
----------------

[](#related-projects)

- [Blink Framework](https://github.com/bixuehujin/blink) - A high performance web framework and application server in PHP. Edit

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 85.7% 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

3227d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5291f9967c4bb1cfda7f50ecde566eb5b554352fa6f7eb7920104d58704400f5?d=identicon)[bixuehujin](/maintainers/bixuehujin)

---

Top Contributors

[![bixuehujin](https://avatars.githubusercontent.com/u/1145280?v=4)](https://github.com/bixuehujin "bixuehujin (18 commits)")[![walangkaji](https://avatars.githubusercontent.com/u/12222084?v=4)](https://github.com/walangkaji "walangkaji (2 commits)")[![ravage84](https://avatars.githubusercontent.com/u/625761?v=4)](https://github.com/ravage84 "ravage84 (1 commits)")

---

Tags

jsonjson-schemajson-validationphp-libraryjson

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[justinrainbow/json-schema

A library to validate a json schema.

3.6k316.9M612](/packages/justinrainbow-json-schema)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k135.8M851](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30422.2M45](/packages/colinodell-json5)[clue/ndjson-react

Streaming newline-delimited JSON (NDJSON) parser and encoder for ReactPHP.

15467.7M16](/packages/clue-ndjson-react)

PHPackages © 2026

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