Skip to content

Commit 4efc08d

Browse files
committed
Add documentation for composed schemas
1 parent 40c6f3d commit 4efc08d

File tree

17 files changed

+304
-20
lines changed

17 files changed

+304
-20
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ env:
99
php:
1010
- 7.2
1111
- 7.3
12-
- 7.4snapshot
12+
- 7.4
1313

1414
install:
1515
# Install coveralls.phar

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"ext-mbstring": "*"
2020
},
2121
"require-dev": {
22-
"phpunit/phpunit": "^8.4"
22+
"phpunit/phpunit": "^9.0"
2323
},
2424
"autoload": {
2525
"psr-4": {

docs/source/combinedSchemas/allOf.rst

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
All Of
2+
======
3+
4+
The `allOf` keyword can be used to combine multiple subschemas. The provided value must be valid against each of the subschemas.
5+
6+
.. code-block:: json
7+
8+
{
9+
"$id": "example",
10+
"type": "object",
11+
"properties": {
12+
"example": {
13+
"allOf": [
14+
{
15+
"type": "number",
16+
"multipleOf": 5
17+
},
18+
{
19+
"type": "number",
20+
"multipleOf": 3
21+
}
22+
]
23+
}
24+
}
25+
}
26+
27+
Valid values are eg. 15, 30, 45. Invalid values are eg. 1, 2, 3, 4, 5 or any non numeric values.
28+
29+
Generated interface:
30+
31+
.. code-block:: php
32+
33+
public function setExample(float $example): self;
34+
public function getExample(): float;
35+
36+
37+
Possible exception (if a string is provided):
38+
39+
.. code-block:: none
40+
41+
Invalid value for example declined by composition constraint.
42+
Requires to match 2 composition elements but matched 0 elements.
43+
- Composition element #1: Failed
44+
* Invalid type for example. Requires float, got string
45+
- Composition element #2: Failed
46+
* Invalid type for example. Requires float, got string
47+
48+
Possible exception (if eg. 5 is provided, which matches only one subschema):
49+
50+
.. code-block:: none
51+
52+
Invalid value for example declined by composition constraint.
53+
Requires to match one composition element but matched 2 elements.
54+
- Composition element #1: Valid
55+
- Composition element #2: Failed
56+
* Value for example must be a multiple of 3
57+
58+
.. hint::
59+
60+
When combining multiple nested objects with an `allOf` composition a `merged property <mergedProperty.html>`__ will be generated

docs/source/combinedSchemas/anyOf.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Any Of
2+
======
3+
4+
The `anyOf` keyword can be used to combine multiple subschemas. The provided value must be valid against at least one of the subschemas.
5+
6+
.. code-block:: json
7+
8+
{
9+
"$id": "example",
10+
"type": "object",
11+
"properties": {
12+
"example": {
13+
"anyOf": [
14+
{
15+
"type": "number",
16+
"multipleOf": 5
17+
},
18+
{
19+
"type": "number",
20+
"multipleOf": 3
21+
}
22+
]
23+
}
24+
}
25+
}
26+
27+
Valid values are eg. 3, 5, 6, 9, 10, 12, 15. Invalid values are eg. 1, 2, 4, 7, 8, 11 or any non numeric values.
28+
29+
Generated interface:
30+
31+
.. code-block:: php
32+
33+
public function setExample(float $example): self;
34+
public function getExample(): float;
35+
36+
37+
Possible exception (if a string is provided):
38+
39+
.. code-block:: none
40+
41+
Invalid value for example declined by composition constraint.
42+
Requires to match at least one composition element.
43+
- Composition element #1: Failed
44+
* Invalid type for example. Requires float, got string
45+
- Composition element #2: Failed
46+
* Invalid type for example. Requires float, got string
47+
48+
.. hint::
49+
50+
When combining multiple nested objects with an `anyOf` composition a `merged property <mergedProperty.html>`__ will be generated

docs/source/combinedSchemas/if.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
If
2+
==
3+
4+
Currently not fully supported
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Merged Property
2+
===============
3+
4+
If multiple subschemas are combined with `oneOf`, `anyOf` or `allOf` and the subschemas contain multiple nested objects all properties of the nested objects will be merged together in a single object.
5+
For example we combine two objects with `allOf`:
6+
7+
.. code-block:: json
8+
9+
{
10+
"$id": "company",
11+
"type": "object",
12+
"properties": {
13+
"ceo": {
14+
"$id": "CEO",
15+
"allOf": [
16+
{
17+
"type": "object",
18+
"properties": {
19+
"name": {
20+
"type": "string"
21+
}
22+
}
23+
},
24+
{
25+
"type": "object",
26+
"properties": {
27+
"age": {
28+
"type": "integer"
29+
}
30+
}
31+
}
32+
]
33+
}
34+
}
35+
}
36+
37+
This schema will generate four classes. The main class will be `Company`, two classes to validate the subschemas combined with the `allOf` independent and one merged class containing all properties of the CEO (name and age in this example).
38+
As the subschemas don't contain IDs they will be named with uniqIds (compare the `naming of classes <../complexTypes/object.html#naming>`__):
39+
40+
* Company.php
41+
* Company_Ceo5e4a82e39edc3.php
42+
* Company_Ceo5e4a82e39fe37.php
43+
* Company_Merged_CEO.php
44+
45+
If the allOf doesn't contain an $id field the merged class will also contain an uniqId. So if you want to use the class with a reproducible class name you must set the $id field.
46+
The classes Company_Ceo5e4a82e39edc3 and Company_Ceo5e4a82e39fe37 are only used for internal validation and can't be accessed via the generated interface of Company.
47+
48+
Generated interface:
49+
50+
.. code-block:: php
51+
52+
# class Company
53+
public function setCeo(?Company_Merged_CEO $example): self;
54+
public function getCeo(): ?Company_Merged_CEO;
55+
56+
# class Company_Merged_CEO
57+
public function getName(): ?string
58+
public function setName(?string $name): self
59+
public function getAge(): ?int
60+
public function setAge(?int $name): self

docs/source/combinedSchemas/not.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Not
2+
===
3+
4+
Used to validate a provided schema or property doesn't match the given schema. In our example object the value for the property example may contain anything except a boolean value to be valid.
5+
6+
.. code-block:: json
7+
8+
{
9+
"$id": "example",
10+
"type": "object",
11+
"properties": {
12+
"example": {
13+
"not": {
14+
"type": "boolean"
15+
}
16+
}
17+
}
18+
}
19+
20+
Generated interface:
21+
22+
.. code-block:: php
23+
24+
public function setExample($example): self;
25+
public function getExample();
26+
27+
28+
Possible exceptions:
29+
30+
.. code-block:: none
31+
32+
Invalid value for property declined by composition constraint.
33+
Requires to match none composition element but matched 1 elements.
34+
- Composition element #1: Valid

docs/source/combinedSchemas/oneOf.rst

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
One Of
2+
======
3+
4+
The `oneOf` keyword can be used to combine multiple subschemas. The provided value must be valid against exactly one of the subschemas.
5+
6+
.. code-block:: json
7+
8+
{
9+
"$id": "example",
10+
"type": "object",
11+
"properties": {
12+
"example": {
13+
"oneOf": [
14+
{
15+
"type": "number",
16+
"multipleOf": 5
17+
},
18+
{
19+
"type": "number",
20+
"multipleOf": 3
21+
}
22+
]
23+
}
24+
}
25+
}
26+
27+
Valid values are eg. 3, 5, 6, 9, 10, 12. Invalid values are eg. 1, 2, 4, 7, 8, 11 or any non numeric values.
28+
29+
Generated interface:
30+
31+
.. code-block:: php
32+
33+
public function setExample(float $example): self;
34+
public function getExample(): float;
35+
36+
37+
Possible exception (if a string is provided):
38+
39+
.. code-block:: none
40+
41+
Invalid value for example declined by composition constraint.
42+
Requires to match one composition element but matched 0 elements.
43+
- Composition element #1: Failed
44+
* Invalid type for example. Requires float, got string
45+
- Composition element #2: Failed
46+
* Invalid type for example. Requires float, got string
47+
48+
49+
Possible exception (if eg. 15 is provided, which matches both subschemas):
50+
51+
.. code-block:: none
52+
53+
Invalid value for example declined by composition constraint.
54+
Requires to match one composition element but matched 2 elements.
55+
- Composition element #1: Valid
56+
- Composition element #2: Valid
57+
58+
.. hint::
59+
60+
When combining multiple nested objects with an `oneOf` composition a `merged property <mergedProperty.html>`__ will be generated

docs/source/generic/references.rst

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.. |br| raw:: html
2-
3-
<br>
4-
51
References
62
==========
73

@@ -10,7 +6,7 @@ References can be used to re-use parts/objects of JSON-Schema definitions.
106
Supported reference types
117
-------------------------
128

13-
* internal (in a single file) reference by id (example: `"$ref": "IdOfMyObject"`)
9+
* internal (in a single file) reference by id (example: `"$ref": "#IdOfMyObject"`)
1410
* internal (in a single file) reference by path (example: `"$ref": "#/definitions/myObject"`)
1511
* relative reference based on the location on the file system to a complete file (example: `"$ref": "./../modules/myObject.json"`)
1612
* relative reference based on the location on the file system to an object by id (example: `"$ref": "./../modules/myObject.json#IdOfMyObject"`)
@@ -29,7 +25,7 @@ An example for properties referring to a definition inside the same schema:
2925
{
3026
"definitions": {
3127
"person": {
32-
"$id": "person",
28+
"$id": "#person",
3329
"type": "object",
3430
"properties": {
3531
"name": {
@@ -42,7 +38,7 @@ An example for properties referring to a definition inside the same schema:
4238
"type": "object",
4339
"properties": {
4440
"leader": {
45-
"$ref": "#/definitions/person"
41+
"$ref": "#person"
4642
}
4743
"members": {
4844
"type": "array",

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Generates PHP model classes from JSON-Schema files including validation and prov
1111
.. include:: toc-generic.rst
1212
.. include:: toc-types.rst
1313
.. include:: toc-complexTypes.rst
14+
.. include:: toc-combinedSchemas.rst

0 commit comments

Comments
 (0)