Skip to content

Commit db18b1e

Browse files
author
Antoine Lelaisant
committed
doc: added documentation in the readme
1 parent 3b94697 commit db18b1e

File tree

4 files changed

+227
-6
lines changed

4 files changed

+227
-6
lines changed

README.md

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
# php-json-schema
2+
3+
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.
4+
5+
# Installation
6+
7+
Install the latest version with
8+
9+
```bash
10+
$ composer require knplabs/php-json-schema
11+
```
12+
13+
# Basic Usage
14+
15+
A JsonSchema must implements `KnpLabs\JsonSchema\JsonSchemaInterface` (which is basically an alias for `JsonSerializable`).
16+
17+
## Default JsonSchema
18+
19+
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.
20+
21+
## Scalars
22+
23+
### `JsonSchema::string()`
24+
25+
```php
26+
use KnpLabs\JsonSchema\JsonSchema;
27+
28+
$schema = JsonSchema::create(
29+
'firstName', // The name of the property
30+
'Hold the first name of the user', // The description of the property
31+
['John', 'Georges'], // Some examples of possible values
32+
JsonSchema::string() // The type of the property
33+
);
34+
```
35+
36+
### `JsonSchema::text()`
37+
38+
```php
39+
use KnpLabs\JsonSchema\JsonSchema;
40+
41+
$schema = JsonSchema::create(
42+
'content', // The name of the property
43+
'The content of the article', // The description of the property
44+
['Lorem ipsum...'], // Some examples of possible values
45+
JsonSchema::text() // The type of the property
46+
);
47+
```
48+
49+
### `JsonSchema::integer()`
50+
51+
```php
52+
use KnpLabs\JsonSchema\JsonSchema;
53+
54+
$schema = JsonSchema::create(
55+
'age', // The name of the property
56+
'Hold the age of the user', // The description of the property
57+
[25, 30], // Some examples of possible values
58+
JsonSchema::integer() // The type of the property
59+
);
60+
```
61+
62+
### `JsonSchema::positiveInteger()`
63+
64+
```php
65+
use KnpLabs\JsonSchema\JsonSchema;
66+
67+
$schema = JsonSchema::create(
68+
'age', // The name of the property
69+
'Hold the age of the user', // The description of the property
70+
[25, 30], // Some examples of possible values
71+
JsonSchema::positiveInteger() // The type of the property
72+
);
73+
```
74+
75+
### `JsonSchema::number()`
76+
77+
```php
78+
use KnpLabs\JsonSchema\JsonSchema;
79+
80+
$schema = JsonSchema::create(
81+
'price', // The name of the property
82+
'The price in dollars', // The description of the property
83+
[10.8, 30.0], // Some examples of possible values
84+
JsonSchema::number() // The type of the property
85+
);
86+
```
87+
88+
### `JsonSchema::boolean()`
89+
90+
```php
91+
use KnpLabs\JsonSchema\JsonSchema;
92+
93+
$schema = JsonSchema::create(
94+
'isAdult', // The name of the property
95+
'Hold if the user is an adult', // The description of the property
96+
[true, false], // Some examples of possible values
97+
JsonSchema::boolean() // The type of the property
98+
);
99+
```
100+
101+
### `JsonSchema::date()`
102+
103+
```php
104+
use KnpLabs\JsonSchema\JsonSchema;
105+
106+
$schema = JsonSchema::create(
107+
'createdAt', // The name of the property
108+
'The date of creation', // The description of the property
109+
['2015-01-01', '2015-01-02'], // Some examples of possible values
110+
JsonSchema::date() // The type of the property
111+
);
112+
```
113+
114+
## Enum
115+
116+
Enum is a special type of scalar which is a list of possible values.
117+
They can be created by extending the `KnpLabs\JsonSchema\EnumSchema`:
118+
119+
```php
120+
<?php
121+
122+
namespace Acme;
123+
124+
use KnpLabs\JsonSchema\EnumSchema;
125+
126+
class RoleEnum extends EnumSchema
127+
{
128+
const ROLE_ADMIN = 'ROLE_ADMIN';
129+
const ROLE_USER = 'ROLE_USER';
130+
131+
public function getTitle(): string
132+
{
133+
return 'Role enum';
134+
}
135+
136+
public function getDescription(): string
137+
{
138+
return 'Enum of the possible roles';
139+
}
140+
141+
public static function getEnum()
142+
{
143+
yield self::ROLE_ADMIN;
144+
yield self::ROLE_USER;
145+
}
146+
}
147+
```
148+
149+
## Objects
150+
151+
You can create objects schema by extending the `KnpLabs\JsonSchema\ObjectSchema` class:
152+
153+
```php
154+
<?php
155+
156+
namespace Acme;
157+
158+
use KnpLabs\JsonSchema\ObjectSchema;
159+
160+
class PersonSchema extends ObjectSchema
161+
{
162+
public function __construct()
163+
{
164+
$this->addProperty(
165+
'firstName',
166+
JsonSchema::create(
167+
'firstName',
168+
'Hold the first name of the user',
169+
['John', 'Georges'],
170+
JsonSchema::string()
171+
)
172+
);
173+
174+
$this->addProperty(
175+
'lastName',
176+
JsonSchema::create(
177+
'lastName',
178+
'Hold the last name of the user',
179+
['Doe', 'Smith'],
180+
JsonSchema::string()
181+
)
182+
);
183+
184+
$this->addProperty('role', new RoleEnum());
185+
}
186+
}
187+
```
188+
189+
## Collections
190+
191+
You can create collections schema by extending the `KnpLabs\JsonSchema\CollectionSchema` class:
192+
193+
```php
194+
<?php
195+
196+
namespace Acme;
197+
198+
use KnpLabs\JsonSchema\CollectionSchema;
199+
200+
class PersonCollectionSchema extends CollectionSchema
201+
{
202+
public function __construct()
203+
{
204+
parent::__construct(new PersonSchema());
205+
}
206+
207+
public function getDescription(): string
208+
{
209+
return 'The list of all the persons';
210+
}
211+
}
212+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "knplabs/json-schema",
2+
"name": "knplabs/php-json-schema",
33
"description": "PHP json-schema implementation",
44
"type": "library",
55
"license": "MIT",

src/JsonSchema.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
namespace KnpLabs\JsonSchema;
66

7-
use JsonSerializable;
8-
97
/**
108
* @template T of mixed
119
*/
12-
abstract class JsonSchema implements JsonSerializable
10+
abstract class JsonSchema implements JsonSchemaInterface
1311
{
1412
/**
1513
* @param JsonSchema<E> $schema
@@ -44,7 +42,7 @@ public static function create(
4442
/**
4543
* @var iterable<int, E>
4644
*/
47-
private readonly iterable $examples;
45+
private iterable $examples;
4846

4947
/**
5048
* @param iterable<int, E> $examples
@@ -77,7 +75,7 @@ public function getExamples(): iterable
7775
yield from $this->examples;
7876
}
7977

80-
protected function getSchema(): array
78+
public function getSchema(): array
8179
{
8280
return $this->schema;
8381
}

src/JsonSchemaInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KnpLabs\JsonSchema;
6+
7+
use JsonSerializable;
8+
9+
interface JsonSchemaInterface extends JsonSerializable
10+
{
11+
}

0 commit comments

Comments
 (0)