9
9
High definition PHP structures with JSON-schema based validation.
10
10
11
11
Supported schemas:
12
+
12
13
* [ JSON Schema Draft 7] ( http://json-schema.org/specification-links.html#draft-7 )
13
14
* [ JSON Schema Draft 6] ( http://json-schema.org/specification-links.html#draft-6 )
14
15
* [ 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
26
27
27
28
### Validating JSON data against given schema
28
29
29
- Define your json-schema
30
+ Define your json-schema
31
+
30
32
``` php
31
33
$schemaJson = <<<'JSON'
32
34
{
@@ -69,12 +71,14 @@ JSON;
69
71
```
70
72
71
73
Load it
74
+
72
75
``` php
73
76
use Swaggest\JsonSchema\Schema;
74
77
$schema = Schema::import(json_decode($schemaJson));
75
78
```
76
79
77
80
Validate data
81
+
78
82
``` php
79
83
$schema->in(json_decode(<<<'JSON'
80
84
{
94
98
```
95
99
96
100
You can also call ` Schema::import ` on string ` uri ` to schema json data.
101
+
97
102
``` php
98
103
$schema = Schema::import('http://localhost:1234/my_schema.json');
99
104
```
100
105
101
106
Or with boolean argument.
107
+
102
108
``` php
103
109
$schema = Schema::import(true); // permissive schema, always validates
104
110
$schema = Schema::import(false); // restrictive schema, always invalidates
@@ -125,7 +131,7 @@ For ambiguous schemas defined with `oneOf`/`anyOf` message is indented multi-lin
125
131
Processing path is a combination of schema and data pointers. You can use ` InvalidValue->getSchemaPointer() `
126
132
and ` InvalidValue->getDataPointer() ` to extract schema/data pointer.
127
133
128
- You can receive ` Schema ` instance that failed validation with ` InvalidValue->getFailedSubSchema ` .
134
+ You can receive ` Schema ` instance that failed validation with ` InvalidValue->getFailedSubSchema ` .
129
135
130
136
You can build error tree using ` InvalidValue->inspect() ` .
131
137
@@ -251,15 +257,15 @@ class Order implements ClassStructureContract
251
257
252
258
$ownerSchema->setFromRef('#/definitions/order');
253
259
254
- // Define default mapping if any
260
+ // Define default mapping if any.
255
261
$ownerSchema->addPropertyMapping('date_time', Order::names()->dateTime);
256
262
257
263
// Use mapped name references after the default mapping was configured.
258
264
$names = self::names($ownerSchema->properties);
259
265
$ownerSchema->required = array(
260
- $names->id,
261
- $names->dateTime,
262
- $names->price
266
+ $names->id,
267
+ $names->dateTime, // "date_time"
268
+ $names->price
263
269
);
264
270
265
271
// Define additional mapping
@@ -270,22 +276,24 @@ class Order implements ClassStructureContract
270
276
}
271
277
```
272
278
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
+
276
282
``` php
277
283
$user = new User();
278
284
$user->quantity = -1; // Exception: Value more than 0 expected, -1 received
279
285
```
280
286
281
287
Validation of native properties is performed only on import/export
288
+
282
289
``` php
283
290
$user = new User();
284
291
$user->quantity = 10;
285
292
User::export($user); // Exception: Required property missing: id
286
293
```
287
294
288
295
Error messages provide a path to invalid data
296
+
289
297
``` php
290
298
$user = new User();
291
299
$user->id = 1;
@@ -328,8 +336,8 @@ $this->assertSame('John', $imported->info->firstName);
328
336
$this->assertSame('Doe', $imported->info->lastName);
329
337
```
330
338
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.
333
341
334
342
``` php
335
343
$schema = new Composition(UserInfo::schema(), Order::schema());
@@ -359,8 +367,7 @@ $this->assertSame(2.66, $order->price);
359
367
360
368
#### Keys mapping
361
369
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.
364
371
365
372
``` php
366
373
// Define default mapping if any
@@ -373,6 +380,7 @@ $ownerSchema->addPropertyMapping('PrIcE', Order::names()->price, self::FANCY_MAP
373
380
```
374
381
375
382
It will affect data mapping:
383
+
376
384
``` php
377
385
$order = new Order();
378
386
$order->id = 1;
@@ -393,6 +401,7 @@ $this->assertSame('2015-10-28T07:28:00Z', $imported->dateTime);
393
401
```
394
402
395
403
You can have multiple mapping namespaces, controlling with ` mapping ` property of ` Context `
404
+
396
405
``` php
397
406
$options = new Context();
398
407
$options->mapping = Order::FANCY_MAPPING;
@@ -418,23 +427,25 @@ You can create your own pre-processor implementing `Swaggest\JsonSchema\DataPreP
418
427
` Meta ` is a way to complement ` Schema ` with your own data. You can keep and retrieve it.
419
428
420
429
You can store it.
430
+
421
431
``` php
422
432
$dbMeta = new DbTable();
423
433
$dbMeta->tableName = 'orders';
424
434
$ownerSchema->addMeta($dbMeta);
425
435
```
426
436
427
437
And get back.
438
+
428
439
``` php
429
440
// Retrieving meta
430
441
$dbTable = DbTable::get(Order::schema());
431
442
$this->assertSame('orders', $dbTable->tableName);
432
443
```
433
444
434
-
435
445
#### Mapping without validation
436
446
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 `
438
449
439
450
``` php
440
451
$schema = Schema::object();
@@ -448,7 +459,6 @@ $res = $schema->in(json_decode('{"one":4}'), $options);
448
459
$this->assertSame(4, $res->one);
449
460
```
450
461
451
-
452
462
#### Overriding mapping classes
453
463
454
464
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'])
481
491
## Code quality and test coverage
482
492
483
493
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 )
485
496
) to allow best performance at maintenance cost.
486
497
487
498
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)
490
503
491
504
## Contributing
492
505
0 commit comments