Skip to content

Commit 299f60c

Browse files
committed
Extended Readme
1 parent 8763f84 commit 299f60c

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

README.md

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,47 @@
33
This library is a PHP based implementation for evaluating and validating [JSON Schemas](https://json-schema.org/).
44
The library can be easily extended with your own keywords and drafts.
55

6-
The following drafts are natively supported:
7-
* [Draft 2020-12 Core](https://json-schema.org/draft/2020-12/json-schema-core.html)
8-
* [Draft 2020-12 Validation](https://json-schema.org/draft/2020-12/json-schema-validation.html)
9-
10-
### Draft 2020-12
11-
Passes all [official JSON schema tests](https://github.com/json-schema-org/JSON-Schema-Test-Suite) except the following optional tests:
12-
* [optional/refOfUnknownKeyword.json](https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/tests/draft2020-12/optional/refOfUnknownKeyword.json):
13-
This means that you cannot use the $ref keyword to reference to schemas that are located inside unknown keywords.
14-
* [optional/ecmascript-regex.json](https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/tests/draft2020-12/optional/ecmascript-regex.json):
15-
This means that the specifics of Ecmascript regular expressions are not respected. Instead, regular expressions are evaluated as PERL regular expressions.
16-
17-
### Requirements
18-
* PHP ^8.1
6+
## Requirements
7+
* PHP >= 8.1
8+
* ext-bcmath
9+
* ext-mbstring
10+
* ext-fileinfo
11+
12+
## Table of contents
13+
* [Installation](#installation)
14+
* [Supported drafts](#supported-drafts)
15+
* [Basic examples](#basic-examples)
16+
* [Basic usage](#basic-usage)
17+
* [Read individual error results](#read-individual-error-results)
18+
* [Formatting results](#formatting-results)
19+
* [Mutations](#mutations)
20+
* [Default values](#default-values)
21+
* [Content decoding](#content-decoding)
22+
* [Advanced examples](#advanced-examples)
23+
* [Assert content media type](#assert-content-media-type)
24+
* [Assert format](#assert-format)
25+
* [Short-circuiting](#short-circuiting)
26+
* [Big numbers](#big-numbers-interpret-numeric-strings-as-numbers)
27+
* [Custom keywords](#custom-keywords)
28+
1929
## Installation
2030
The library can be installed from a command line interface by using [composer](https://getcomposer.org/).
2131

32+
## Supported drafts
33+
34+
### Draft 2020-12 ([Core](https://json-schema.org/draft/2020-12/json-schema-core.html) and [Validation](https://json-schema.org/draft/2020-12/json-schema-validation.html))
35+
Passes all tests of [official JSON schema test suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite) except the following optional tests:
36+
* [optional/refOfUnknownKeyword.json](https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/tests/draft2020-12/optional/refOfUnknownKeyword.json):
37+
This means that you cannot use the <b>$ref</b> keyword to reference schemas that are located inside unknown keywords.
38+
* [optional/ecmascript-regex.json](https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/tests/draft2020-12/optional/ecmascript-regex.json):
39+
This means that the specifics of Ecmascript regular expressions are not respected. Instead, regular expressions are evaluated as PERL regular expressions.
40+
2241
```
2342
composer require ropi/json-schema-evaluator
2443
```
25-
26-
## Basic usage
44+
## Basic examples
45+
### Basic usage
2746
```php
28-
<?php
2947
$schema = json_decode('{
3048
"type": "string",
3149
"maxLength": 5
@@ -62,8 +80,14 @@ foreach ($results as $result) {
6280
}
6381
}
6482
```
65-
The results can also be formatted.
66-
In this example errors will be formatted to [Basic Output Structure](https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.12.4.2).
83+
Output of above example:
84+
```
85+
Error keyword location: '/maxLength'
86+
Error instance location: ''
87+
Error message: At most 5 characters are allowed, but there are 10.
88+
```
89+
### Formatting results
90+
In the following example, the results are formatted as [Basic Output Structure](https://json-schema.org/draft/2020-12/json-schema-core#name-basic).
6791
```php
6892
$formattedResults = (new \Ropi\JsonSchemaEvaluator\Output\BasicOutput($valid, $results))->format();
6993
echo json_encode($formattedResults, JSON_PRETTY_PRINT);
@@ -84,12 +108,10 @@ Output of above example:
84108
]
85109
}
86110
```
87-
88111
## Mutations
89112
### Default values
90-
If a default value is defined with the [default keyword](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.9.2), it can be automatically applied during evaluation.
113+
If a default value is defined with the [default keyword](https://json-schema.org/draft/2020-12/json-schema-validation#name-default), it can be automatically applied during evaluation.
91114
```php
92-
<?php
93115
$schema = json_decode('{
94116
"type": "object",
95117
"required": ["lastname"],
@@ -117,10 +139,9 @@ $evaluator->evaluate($instance, $staticEvaluationContext);
117139
echo $instance->firstname; // Prints "n/a"
118140
```
119141

120-
### Decoding of encoded content
121-
If encoded content is defined with the [contentEncoding keyword](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.8.3), it can be automatically decoded during evaluation.
142+
### Content decoding
143+
If encoded content is defined with the [contentEncoding keyword](https://json-schema.org/draft/2020-12/json-schema-validation#name-contentencoding), it can be automatically decoded during evaluation.
122144
```php
123-
<?php
124145
$schema = json_decode('{
125146
"contentMediaType": "application/json",
126147
"contentEncoding": "base64"
@@ -140,11 +161,10 @@ $evaluator->evaluate($instance, $staticEvaluationContext); // Returns true
140161

141162
echo $instance; // Prints '{"foo": "bar"}'
142163
```
143-
144-
## Assert content media type
164+
## Advanced examples
165+
### Assert content media type
145166
If content media type is defined with the [contentMediaType keyword](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.8.4), it can be respected during evaluation.
146167
```php
147-
<?php
148168
$schema = json_decode('{
149169
"contentMediaType": "application/json"
150170
}');
@@ -164,10 +184,9 @@ $instance2 = 'invalidJSON';
164184
$evaluator->evaluate($instance2, $staticEvaluationContext); // Returns false
165185
```
166186

167-
## Assert format
168-
If format is defined with the [format keyword](https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.12.1), it can be respected during evaluation.
187+
### Assert format
188+
If format is defined with the [format keyword](https://json-schema.org/draft/2020-12/json-schema-validation#name-format-annotation-vocabular), it can be respected during evaluation.
169189
```php
170-
<?php
171190
$schema = json_decode('{
172191
"format": "email"
173192
}');
@@ -187,7 +206,7 @@ $instance2 = 'invalidEmail';
187206
$evaluator->evaluate($instance2, $staticEvaluationContext, $runtimeEvaluationConfig); // Returns false
188207
```
189208

190-
## Short-circuiting
209+
### Short-circuiting
191210
By default, all keywords are evaluated, even if the first keyword validation fails.
192211
If short circuiting is activated, the evaluation stops at the first negative validation result.
193212
```php
@@ -198,9 +217,8 @@ $config = new \Ropi\JsonSchemaEvaluator\EvaluationConfig\StaticEvaluationConfig(
198217
);
199218
```
200219

201-
## Big numbers (interpret numeric strings as numbers)
220+
### Big numbers (interpret numeric strings as numbers)
202221
```php
203-
<?php
204222
$schema = json_decode('{
205223
"type": "integer"
206224
}');
@@ -217,9 +235,8 @@ $instance = json_decode('6565650699413464649797946464646464649797979', false, 51
217235
$evaluator->evaluate($instance, $staticEvaluationContext); // Returns true
218236
```
219237

220-
## Custom keywords
238+
### Custom keywords
221239
```php
222-
<?php
223240
$schema = json_decode('{
224241
"md5Hash": "098f6bcd4621d373cade4e832627b4f6"
225242
}');

0 commit comments

Comments
 (0)