PHPackages                             geraintluff/jsv4 - 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. geraintluff/jsv4

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

geraintluff/jsv4
================

A (coercive) JSON Schema v4 Validator for PHP

v1.0.1(11y ago)115455.2k↓19%32[12 issues](https://github.com/geraintluff/jsv4-php/issues)[5 PRs](https://github.com/geraintluff/jsv4-php/pulls)4MITPHP

Since Jun 12Pushed 9y ago12 watchersCompare

[ Source](https://github.com/geraintluff/jsv4-php)[ Packagist](https://packagist.org/packages/geraintluff/jsv4)[ RSS](/packages/geraintluff-jsv4/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (4)

jsv4-php
========

[](#jsv4-php)

A (coercive) JSON Schema v4 Validator for PHP
---------------------------------------------

[](#a-coercive-json-schema-v4-validator-for-php)

`jsv4-php` is a data validator, using version 4 JSON Schemas.

Just include `jsv4.php` from your code, and use the static methods on the `Jsv4` class it defines.

Usage:

### `Jsv4::validate($data, $schema)`

[](#jsv4validatedata-schema)

This usage returns an object of the following shape.

```
{
    "valid": true/false,
    "errors": [
        {...}
    ]
}
```

The values in the `errors` array are similar to those for [tv4](https://github.com/geraintluff/tv4) (a similar project):

```
{
    "code": 0,
    "message": "Invalid type: string",
    "dataPath": "/intKey",
    "schemaKey": "/properties/intKey/type"
}
```

The `code` property corresponds to a constant corresponding to the nature of the validation error, e.g. `JSV4_INVALID_TYPE`. The names of these constants (and their values) match up exactly with the [constants from tv4](https://github.com/geraintluff/tv4/blob/master/source/api.js).

### `Jsv4::isValid($data, $schema)`

[](#jsv4isvaliddata-schema)

If you just want to know the validation status, and don't care what the errors actually are, then this is a more concise way of getting it.

It returns a boolean indicating whether the data correctly followed the schema.

### `Jsv4::coerce($data, $schema)`

[](#jsv4coercedata-schema)

Sometimes, the data is not quite the correct shape - but it could be *made* the correct shape by simple modifications.

If you call `Jsv4::coerce($data, $schema)`, then it will attempt to change the data.

If it is successful, then a modified version of the data can be found in `$result->value`.

It's not psychic - in fact, it's quite limited. What it currently does is:

### Type-coercion for scalar types

[](#type-coercion-for-scalar-types)

Perhaps you are using data from `$_GET`, so everything's a string, but the schema says certain values should be integers or booleans.

`Jsv4::coerce()` will attempt to convert strings to numbers/booleans *only where the schema says*, leaving other numerically-value strings as strings.

### Missing properties

[](#missing-properties)

Perhaps the API needs a complete object (described using `"required"` in the schema), but only a partial one was supplied.

`Jsv4::coerce()` will attempt to insert appropriate values for the missing properties, using a default (if it is defined in a nearby `"properties"` entry) or by creating a value if it knows the type.

The `SchemaStore` class
-----------------------

[](#the-schemastore-class)

This class represents a collection of schemas. You include it from `schema-store.php`, and use it like this:

```
$store = new SchemaStore();
$store->add($url, $schema);
$retrieved = $store->get($url);
```

It can handle:

- Fragments in URLs, using both JSON Pointer fragments and identification using `"id"`
- Converts URIs in `"id"` and `"$ref"` to absolute (where possible)
- Resolves `"$ref"`s, splicing the resulting value into the schema
- Converts associative PHP arrays to objects - you can express your schema natively, but what you retrieve from the store is always an object.
- Adds sub-schemas according to their `"id"` - by default, this only happens if `"id"` is a sub-path of the current schema URL.

If the schemas being added are "trusted", then an extra argument can be supplied: `$store->add($url, $schema, TRUE)`. In that case, the value of `"id"` is *always* believed for all sub-schemas.

A list of "missing" schemas (unresolved `"$ref"`s) can be retrieved used `$store->missing()`.

This class does not depend on `jsv4.php` at all - it just deals with raw schema objects. As such, it could (hopefully) be used with other validators with minimal fuss.

Tests
-----

[](#tests)

The tests can be run using `test.php` (from the command-line or via the web).

License
-------

[](#license)

This code is released under a do-anything-you-like "public domain" license (see `LICENSE.txt`).

It is also released under an MIT-style license (see `LICENSE-MIT.txt`) because there is sometimes benefit in having a recognised open-source license.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity52

Moderate usage in the ecosystem

Community27

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 62.9% 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 ~116 days

Total

2

Last Release

4241d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/267e3fd115ead27b2b9b41b42837ede0c090af278c151861f445a2fa921e005b?d=identicon)[geraintluff](/maintainers/geraintluff)

---

Top Contributors

[![geraintluff](https://avatars.githubusercontent.com/u/1957191?v=4)](https://github.com/geraintluff "geraintluff (22 commits)")[![bazo](https://avatars.githubusercontent.com/u/181588?v=4)](https://github.com/bazo "bazo (5 commits)")[![jdesrosiers](https://avatars.githubusercontent.com/u/716571?v=4)](https://github.com/jdesrosiers "jdesrosiers (3 commits)")[![selslack](https://avatars.githubusercontent.com/u/4094244?v=4)](https://github.com/selslack "selslack (3 commits)")[![shwesley](https://avatars.githubusercontent.com/u/20866705?v=4)](https://github.com/shwesley "shwesley (2 commits)")

---

Tags

jsonschemavalidatorjson-schemav4Geraint Luffgeraintluff

### Embed Badge

![Health badge](/badges/geraintluff-jsv4/health.svg)

```
[![Health](https://phpackages.com/badges/geraintluff-jsv4/health.svg)](https://phpackages.com/packages/geraintluff-jsv4)
```

###  Alternatives

[opis/json-schema

Json Schema Validator for PHP

64736.9M186](/packages/opis-json-schema)[seld/jsonlint

JSON Linter

1.3k217.8M205](/packages/seld-jsonlint)[ergebnis/json-schema-validator

Provides a JSON schema validator, building on top of justinrainbow/json-schema.

3626.9M7](/packages/ergebnis-json-schema-validator)[johnstevenson/json-works

Create, edit, query and validate json

272.5M6](/packages/johnstevenson-json-works)[romaricdrigon/metayaml

Using \[Yaml|Xml|json\] schemas files to validate \[Yaml|Xml|json\]

103306.5k8](/packages/romaricdrigon-metayaml)[paulzi/yii2-json-behavior

Yii2 json attribute behavior

76528.6k3](/packages/paulzi-yii2-json-behavior)

PHPackages © 2026

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