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

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

knplabs/php-json-schema
=======================

PHP json-schema implementation

v0.1.0(3y ago)725.6k↓14.2%12MITPHPPHP &gt;=8.0

Since Sep 27Pushed 2y ago3 watchersCompare

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

READMEChangelog (9)DependenciesVersions (11)Used By (2)

php-json-schema
===============

[](#php-json-schema)

A PHP implementation of [JSON Schema](http://json-schema.org/). This library allows you to create JSON Schema objects and validate JSON data against them.

Installation
============

[](#installation)

Install the latest version with

```
composer require knplabs/php-json-schema
```

Basic Usage
===========

[](#basic-usage)

A JsonSchema must implements `KnpLabs\JsonSchema\JsonSchemaInterface` (which also extends `JsonSerializable`).

Default JsonSchema
------------------

[](#default-jsonschema)

There is already a default implementation of `JsonSchemaInterface` called `KnpLabs\JsonSchema\JsonSchema` which is an abstract class. This class provides some static methods to create some common JSON Schema scalars or objects.

Scalars
-------

[](#scalars)

### `JsonSchema::string()`

[](#jsonschemastring)

```
use KnpLabs\JsonSchema\JsonSchema;

$schema = JsonSchema::create(
    'firstName',                       // The name of the property
    'Hold the first name of the user', // The description of the property
    ['John', 'Georges'],               // Some examples of possible values
    JsonSchema::string()               // The type of the property
);
```

### `JsonSchema::text()`

[](#jsonschematext)

```
use KnpLabs\JsonSchema\JsonSchema;

$schema = JsonSchema::create(
    'content',                    // The name of the property
    'The content of the article', // The description of the property
    ['Lorem ipsum...'],           // Some examples of possible values
    JsonSchema::text()            // The type of the property
);
```

### `JsonSchema::integer()`

[](#jsonschemainteger)

```
use KnpLabs\JsonSchema\JsonSchema;

$schema = JsonSchema::create(
    'age',                            // The name of the property
    'Hold the age of the user',       // The description of the property
    [25, 30],                         // Some examples of possible values
    JsonSchema::integer()             // The type of the property
);
```

### `JsonSchema::positiveInteger()`

[](#jsonschemapositiveinteger)

```
use KnpLabs\JsonSchema\JsonSchema;

$schema = JsonSchema::create(
    'age',                            // The name of the property
    'Hold the age of the user',       // The description of the property
    [25, 30],                         // Some examples of possible values
    JsonSchema::positiveInteger()     // The type of the property
);
```

### `JsonSchema::number()`

[](#jsonschemanumber)

```
use KnpLabs\JsonSchema\JsonSchema;

$schema = JsonSchema::create(
    'price',                // The name of the property
    'The price in dollars', // The description of the property
    [10.8, 30.0],           // Some examples of possible values
    JsonSchema::number()    // The type of the property
);
```

### `JsonSchema::boolean()`

[](#jsonschemaboolean)

```
use KnpLabs\JsonSchema\JsonSchema;

$schema = JsonSchema::create(
    'isAdult',                      // The name of the property
    'Hold if the user is an adult', // The description of the property
    [true, false],                  // Some examples of possible values
    JsonSchema::boolean()           // The type of the property
);
```

### `JsonSchema::date()`

[](#jsonschemadate)

```
use KnpLabs\JsonSchema\JsonSchema;

$schema = JsonSchema::create(
    'createdAt',                    // The name of the property
    'The date of creation',         // The description of the property
    ['2015-01-01', '2015-01-02'],   // Some examples of possible values
    JsonSchema::date()              // The type of the property
);
```

Enum
----

[](#enum)

Enum is a special type of scalar which is a list of possible values. They can be created by extending the `KnpLabs\JsonSchema\EnumSchema`:

```
