@@ -40,24 +40,35 @@ $schema = SchemaFactory::object('user')
4040 SchemaFactory::object('settings')
4141 ->additionalProperties(false)
4242 ->properties([
43- SchemaFactory::string('theme')->enum(['light', 'dark']),
44- SchemaFactory::boolean('notifications')
43+ SchemaFactory::string('theme')
44+ ->enum(['light', 'dark']),
4545 ]),
4646 );
4747```
4848
49- Or you can use the objects directly
49+ You can also use the objects directly instead of the factory methods.
5050``` php
51- $schema = new ObjectSchema('user')
51+ $schema = ( new ObjectSchema('user') )
5252 ->description('User schema')
5353 ->properties(
54- new StringSchema('name')
54+ ( new StringSchema('name') )
5555 ->minLength(2)
5656 ->maxLength(100)
5757 ->required(),
58- new StringSchema('email')
58+ ( new StringSchema('email') )
5959 ->format(SchemaFormat::Email)
6060 ->required(),
61+ (new IntegerSchema('age'))
62+ ->minimum(18)
63+ ->maximum(150),
64+ (new BooleanSchema('active'))
65+ ->default(true),
66+ (new ObjectSchema('settings'))
67+ ->additionalProperties(false)
68+ ->properties(
69+ (new StringSchema('theme'))
70+ ->enum(['light', 'dark']),
71+ ),
6172 );
6273```
6374
@@ -68,23 +79,24 @@ $schema->toArray();
6879// Convert to JSON string
6980$schema->toJson();
7081
82+ $data = [
83+ 'name' => 'John Doe',
84+ 'email' => 'john@example.com',
85+ 'age' => 16,
86+ 'active' => true,
87+ 'settings' => [
88+ 'theme' => 'dark',
89+ ],
90+ ];
91+
7192// Validate data against the schema
7293try {
73- $schema->validate([
74- 'name' => 'John Doe',
75- 'email' => 'john@example.com',
76- 'age' => 16,
77- 'active' => true,
78- 'settings' => [
79- 'theme' => 'dark',
80- 'notifications' => true,
81- ],
82- ]);
94+ $schema->validate($data);
8395} catch (SchemaException $e) {
8496 echo $e->getMessage(); // "The data must match the 'email' format"
8597}
8698
87- // Validate data against the schema
99+ // Or just get a boolean
88100$schema->isValid($data);
89101```
90102
@@ -374,6 +386,42 @@ $schema->isValid([
374386
375387---
376388
389+ ### Union Schema
390+
391+ ``` php
392+ use Cortex\JsonSchema\SchemaFactory;
393+ use Cortex\JsonSchema\Enums\SchemaType;
394+
395+ $schema = SchemaFactory::union([SchemaType::String, SchemaType::Integer], 'id')
396+ ->description('ID can be either a string or an integer')
397+ ->enum(['abc123', 'def456', 1, 2, 3])
398+ ->nullable();
399+ ```
400+
401+ ``` php
402+ $schema->isValid('abc123'); // true
403+ $schema->isValid(1); // true
404+ $schema->isValid(null); // true (because it's nullable)
405+ $schema->isValid(true); // false (not a string or integer)
406+ $schema->isValid('invalid'); // false (not in enum)
407+ ```
408+
409+ <details >
410+ <summary >View JSON Schema</summary >
411+
412+ ``` json
413+ {
414+ "$schema" : " http://json-schema.org/draft-07/schema#" ,
415+ "type" : [" string" , " integer" , " null" ],
416+ "title" : " id" ,
417+ "description" : " ID can be either a string or an integer" ,
418+ "enum" : [" abc123" , " def456" , 1 , 2 , 3 ]
419+ }
420+ ```
421+ </details >
422+
423+ ---
424+
377425## Validation
378426
379427The library throws a ` SchemaException ` when validation fails:
0 commit comments