Skip to content

Commit 8dbd73e

Browse files
committed
Update docs
1 parent cbdf1d9 commit 8dbd73e

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

README.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
High definition PHP structures with JSON-schema based validation.
1010

1111
Supported schemas:
12+
1213
* [JSON Schema Draft 7](http://json-schema.org/specification-links.html#draft-7)
1314
* [JSON Schema Draft 6](http://json-schema.org/specification-links.html#draft-6)
1415
* [JSON Schema Draft 4](http://json-schema.org/specification-links.html#draft-4)
@@ -26,7 +27,8 @@ Structure definition can be done either with `json-schema` or with
2627

2728
### Validating JSON data against given schema
2829

29-
Define your json-schema
30+
Define your json-schema
31+
3032
```php
3133
$schemaJson = <<<'JSON'
3234
{
@@ -69,12 +71,14 @@ JSON;
6971
```
7072

7173
Load it
74+
7275
```php
7376
use Swaggest\JsonSchema\Schema;
7477
$schema = Schema::import(json_decode($schemaJson));
7578
```
7679

7780
Validate data
81+
7882
```php
7983
$schema->in(json_decode(<<<'JSON'
8084
{
@@ -94,11 +98,13 @@ JSON
9498
```
9599

96100
You can also call `Schema::import` on string `uri` to schema json data.
101+
97102
```php
98103
$schema = Schema::import('http://localhost:1234/my_schema.json');
99104
```
100105

101106
Or with boolean argument.
107+
102108
```php
103109
$schema = Schema::import(true); // permissive schema, always validates
104110
$schema = Schema::import(false); // restrictive schema, always invalidates
@@ -125,7 +131,7 @@ For ambiguous schemas defined with `oneOf`/`anyOf` message is indented multi-lin
125131
Processing path is a combination of schema and data pointers. You can use `InvalidValue->getSchemaPointer()`
126132
and `InvalidValue->getDataPointer()` to extract schema/data pointer.
127133

128-
You can receive `Schema` instance that failed validation with `InvalidValue->getFailedSubSchema`.
134+
You can receive `Schema` instance that failed validation with `InvalidValue->getFailedSubSchema`.
129135

130136
You can build error tree using `InvalidValue->inspect()`.
131137

@@ -251,15 +257,15 @@ class Order implements ClassStructureContract
251257

252258
$ownerSchema->setFromRef('#/definitions/order');
253259

254-
// Define default mapping if any
260+
// Define default mapping if any.
255261
$ownerSchema->addPropertyMapping('date_time', Order::names()->dateTime);
256262

257263
// Use mapped name references after the default mapping was configured.
258264
$names = self::names($ownerSchema->properties);
259265
$ownerSchema->required = array(
260-
$names->id,
261-
$names->dateTime,
262-
$names->price
266+
$names->id,
267+
$names->dateTime, // "date_time"
268+
$names->price
263269
);
264270

265271
// Define additional mapping
@@ -270,22 +276,24 @@ class Order implements ClassStructureContract
270276
}
271277
```
272278

273-
Validation of dynamic properties is performed on set,
274-
this can help to find source of invalid data at cost of
275-
some performance drop
279+
Validation of dynamic properties is performed on set, this can help to find source of invalid data at cost of some
280+
performance drop
281+
276282
```php
277283
$user = new User();
278284
$user->quantity = -1; // Exception: Value more than 0 expected, -1 received
279285
```
280286

281287
Validation of native properties is performed only on import/export
288+
282289
```php
283290
$user = new User();
284291
$user->quantity = 10;
285292
User::export($user); // Exception: Required property missing: id
286293
```
287294

288295
Error messages provide a path to invalid data
296+
289297
```php
290298
$user = new User();
291299
$user->id = 1;
@@ -328,8 +336,8 @@ $this->assertSame('John', $imported->info->firstName);
328336
$this->assertSame('Doe', $imported->info->lastName);
329337
```
330338

331-
You can also use `\Swaggest\JsonSchema\Structure\Composition` to dynamically create schema compositions.
332-
This can be helpful to deal with results of database query on joined data.
339+
You can also use `\Swaggest\JsonSchema\Structure\Composition` to dynamically create schema compositions. This can be
340+
helpful to deal with results of database query on joined data.
333341

334342
```php
335343
$schema = new Composition(UserInfo::schema(), Order::schema());
@@ -359,8 +367,7 @@ $this->assertSame(2.66, $order->price);
359367

360368
#### Keys mapping
361369

362-
If property names of PHP objects should be different from raw data you
363-
can call `->addPropertyMapping` on owner schema.
370+
If property names of PHP objects should be different from raw data you can call `->addPropertyMapping` on owner schema.
364371

365372
```php
366373
// Define default mapping if any
@@ -373,6 +380,7 @@ $ownerSchema->addPropertyMapping('PrIcE', Order::names()->price, self::FANCY_MAP
373380
```
374381

375382
It will affect data mapping:
383+
376384
```php
377385
$order = new Order();
378386
$order->id = 1;
@@ -393,6 +401,7 @@ $this->assertSame('2015-10-28T07:28:00Z', $imported->dateTime);
393401
```
394402

395403
You can have multiple mapping namespaces, controlling with `mapping` property of `Context`
404+
396405
```php
397406
$options = new Context();
398407
$options->mapping = Order::FANCY_MAPPING;
@@ -418,23 +427,25 @@ You can create your own pre-processor implementing `Swaggest\JsonSchema\DataPreP
418427
`Meta` is a way to complement `Schema` with your own data. You can keep and retrieve it.
419428

420429
You can store it.
430+
421431
```php
422432
$dbMeta = new DbTable();
423433
$dbMeta->tableName = 'orders';
424434
$ownerSchema->addMeta($dbMeta);
425435
```
426436

427437
And get back.
438+
428439
```php
429440
// Retrieving meta
430441
$dbTable = DbTable::get(Order::schema());
431442
$this->assertSame('orders', $dbTable->tableName);
432443
```
433444

434-
435445
#### Mapping without validation
436446

437-
If you want to tolerate invalid data or improve mapping performance you can specify `skipValidation` flag in processing `Context`
447+
If you want to tolerate invalid data or improve mapping performance you can specify `skipValidation` flag in
448+
processing `Context`
438449

439450
```php
440451
$schema = Schema::object();
@@ -448,7 +459,6 @@ $res = $schema->in(json_decode('{"one":4}'), $options);
448459
$this->assertSame(4, $res->one);
449460
```
450461

451-
452462
#### Overriding mapping classes
453463

454464
If you want to map data to a different class you can register mapping at top level of your importer structure.
@@ -481,12 +491,15 @@ $this->assertInstanceOf(CustomSchema::className(), $schema->definitions['User'])
481491
## Code quality and test coverage
482492

483493
Some code quality best practices are deliberately violated here
484-
(see [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/swaggest/php-json-schema/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/swaggest/php-json-schema/?branch=master)
494+
(
495+
see [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/swaggest/php-json-schema/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/swaggest/php-json-schema/?branch=master)
485496
) to allow best performance at maintenance cost.
486497

487498
Those violations are secured by comprehensive test coverage:
488-
* draft-04, draft-06, draft-07 of [JSON-Schema-Test-Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite)
489-
* test cases (excluding `$data` and few tests) of [epoberezkin/ajv](https://github.com/epoberezkin/ajv/tree/master/spec) (a mature js implementation)
499+
500+
* draft-04, draft-06, draft-07 of [JSON-Schema-Test-Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite)
501+
* test cases (excluding `$data` and few tests)
502+
of [epoberezkin/ajv](https://github.com/epoberezkin/ajv/tree/master/spec) (a mature js implementation)
490503

491504
## Contributing
492505

tests/src/Helper/Order.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ public static function setUpProperties($properties, Schema $ownerSchema)
3939

4040
$ownerSchema->setFromRef('#/definitions/order');
4141

42-
// Define default mapping if any
43-
$ownerSchema->addPropertyMapping('date_time', Order::names()->dateTime);
42+
// Define default mapping if any.
43+
$properties->addPropertyMapping('date_time', Order::names()->dateTime);
4444

4545
// Use mapped name references after the default mapping was configured.
46-
$names = self::names($ownerSchema->properties);
46+
$names = self::names($properties);
4747
$ownerSchema->required = array(
4848
$names->id,
4949
$names->dateTime,
5050
$names->price
5151
);
5252

5353
// Define additional mapping
54-
$ownerSchema->addPropertyMapping('DaTe_TiMe', Order::names()->dateTime, self::FANCY_MAPPING);
55-
$ownerSchema->addPropertyMapping('Id', Order::names()->id, self::FANCY_MAPPING);
56-
$ownerSchema->addPropertyMapping('PrIcE', Order::names()->price, self::FANCY_MAPPING);
54+
$properties->addPropertyMapping('DaTe_TiMe', Order::names()->dateTime, self::FANCY_MAPPING);
55+
$properties->addPropertyMapping('Id', Order::names()->id, self::FANCY_MAPPING);
56+
$properties->addPropertyMapping('PrIcE', Order::names()->price, self::FANCY_MAPPING);
5757
}
5858
}

0 commit comments

Comments
 (0)