Skip to content

Commit dab7947

Browse files
committed
Add additional array tests with nested objects, nested combined schemas
Exception indentation Fix validator filtering on PropertyProxy Fix id to $id Fix counting of combined elements
1 parent d26a470 commit dab7947

40 files changed

+3802
-3280
lines changed

docs/source/complexTypes/array.rst

Lines changed: 283 additions & 283 deletions
Large diffs are not rendered by default.

docs/source/complexTypes/enum.rst

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
1-
Enum
2-
====
3-
4-
Enums can be used to define a set of constant values a property must accept.
5-
6-
.. code-block:: json
7-
8-
{
9-
"id": "example",
10-
"type": "object",
11-
"properties": {
12-
"example": {
13-
"type": "string",
14-
"enum": ["ABC", "DEF"]
15-
}
16-
}
17-
}
18-
19-
Generated interface:
20-
21-
.. code-block:: php
22-
23-
public function setExample(?string $example): self;
24-
public function getExample(): ?string;
25-
26-
Possible exceptions:
27-
28-
* Invalid type for example. Requires string, got __TYPE__
29-
* Invalid value for example declined by enum constraint
30-
31-
Untyped Enum
32-
------------
33-
34-
An enum can also be defined without a specific type.
35-
36-
.. code-block:: json
37-
38-
{
39-
"id": "example",
40-
"type": "object",
41-
"properties": {
42-
"example": {
43-
"enum": ["ABC", 10, true, null]
44-
}
45-
}
46-
}
47-
48-
Generated interface (no typehints are generated as it's an untyped enum):
49-
50-
.. code-block:: php
51-
52-
public function setExample($example): self;
53-
public function getExample();
54-
55-
Possible exceptions:
56-
57-
* Invalid value for example declined by enum constraint
1+
Enum
2+
====
3+
4+
Enums can be used to define a set of constant values a property must accept.
5+
6+
.. code-block:: json
7+
8+
{
9+
"$id": "example",
10+
"type": "object",
11+
"properties": {
12+
"example": {
13+
"type": "string",
14+
"enum": ["ABC", "DEF"]
15+
}
16+
}
17+
}
18+
19+
Generated interface:
20+
21+
.. code-block:: php
22+
23+
public function setExample(?string $example): self;
24+
public function getExample(): ?string;
25+
26+
Possible exceptions:
27+
28+
* Invalid type for example. Requires string, got __TYPE__
29+
* Invalid value for example declined by enum constraint
30+
31+
Untyped Enum
32+
------------
33+
34+
An enum can also be defined without a specific type.
35+
36+
.. code-block:: json
37+
38+
{
39+
"$id": "example",
40+
"type": "object",
41+
"properties": {
42+
"example": {
43+
"enum": ["ABC", 10, true, null]
44+
}
45+
}
46+
}
47+
48+
Generated interface (no typehints are generated as it's an untyped enum):
49+
50+
.. code-block:: php
51+
52+
public function setExample($example): self;
53+
public function getExample();
54+
55+
Possible exceptions:
56+
57+
* Invalid value for example declined by enum constraint
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
Combined exceptions
2+
===================
3+
4+
When building larger schema files with complex types like arrays or combined schemas exception messages may get larger as they include information about what violations were found.
5+
Our example schema provides an array of people. The people object is combined via `allOf`:
6+
7+
.. code-block:: json
8+
9+
{
10+
"definitions": {
11+
"name": {
12+
"type": "object",
13+
"properties": {
14+
"name": {
15+
"type": "string",
16+
"minLength": 2
17+
}
18+
},
19+
"required": [
20+
"name"
21+
]
22+
}
23+
},
24+
"$id": "example",
25+
"type": "object",
26+
"properties": {
27+
"people": {
28+
"type": "array",
29+
"items": {
30+
"allOf": [
31+
{
32+
"$ref": "#/definitions/name"
33+
},
34+
{
35+
"type": "object",
36+
"properties": {
37+
"age": {
38+
"type": "integer"
39+
}
40+
}
41+
}
42+
]
43+
}
44+
}
45+
}
46+
}
47+
48+
after generating the classes for the given schema we provide an array which contains only invalid items:
49+
50+
.. code-block:: php
51+
52+
$example = new Example([
53+
['name' => false, 'age' => 42],
54+
['name' => 'F', 'age' => 'yes'],
55+
5,
56+
[]
57+
]);
58+
59+
The exception which will be thrown (combined array-exception and combined-schema-exception with indentation to indicate the error structure):
60+
61+
.. code-block:: none
62+
63+
Invalid item in array property:
64+
- invalid item #0
65+
* Invalid value for item of array property declined by composition constraint.
66+
Requires to match 2 composition elements but matched 1 elements.
67+
- Composition element #1: Failed
68+
* Invalid type for name. Requires string, got boolean
69+
- Composition element #2: Valid
70+
- invalid item #1
71+
* Invalid value for item of array property declined by composition constraint.
72+
Requires to match 2 composition elements but matched 0 elements.
73+
- Composition element #1: Failed
74+
* Value for name must not be shorter than 2
75+
- Composition element #2: Failed
76+
* Invalid type for age. Requires int, got string
77+
- invalid item #2
78+
* Invalid value for item of array property declined by composition constraint.
79+
Requires to match 2 composition elements but matched 0 elements.
80+
- Composition element #1: Failed
81+
* Invalid type for item of array property. Requires object, got integer
82+
- Composition element #2: Failed
83+
* Invalid type for item of array property. Requires object, got integer
84+
- invalid item #3
85+
* Invalid value for item of array property declined by composition constraint.
86+
Requires to match 2 composition elements but matched 1 elements.
87+
- Composition element #1: Failed
88+
* Missing required value for name
89+
* Invalid type for name. Requires string, got NULL
90+
- Composition element #2: Valid

docs/source/generic/default.rst

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
Default values
2-
==============
3-
4-
Default values are set inside the model if a property which is not required isn't provided in the input data.
5-
6-
.. code-block:: json
7-
8-
{
9-
"id": "example",
10-
"type": "object",
11-
"properties": {
12-
"example": {
13-
"type": "string",
14-
"default": "Not provided"
15-
}
16-
}
17-
}
18-
19-
Behaviour with different inputs:
20-
21-
.. code-block:: php
22-
23-
// property example not provided --> fallback to the default value
24-
$example = new Example([]);
25-
$example->getExample(); // returns "Not provided"
26-
27-
// property example explicitly set to null (allowed as the property isn't required)
28-
$example = new Example(['example' => null]);
29-
$example->getExample(); // returns NULL
30-
31-
// property example set to a custom value
32-
$example = new Example(['example' => 'My Input']);
33-
$example->getExample(); // returns "My Input"
1+
Default values
2+
==============
3+
4+
Default values are set inside the model if a property which is not required isn't provided in the input data.
5+
6+
.. code-block:: json
7+
8+
{
9+
"$id": "example",
10+
"type": "object",
11+
"properties": {
12+
"example": {
13+
"type": "string",
14+
"default": "Not provided"
15+
}
16+
}
17+
}
18+
19+
Behaviour with different inputs:
20+
21+
.. code-block:: php
22+
23+
// property example not provided --> fallback to the default value
24+
$example = new Example([]);
25+
$example->getExample(); // returns "Not provided"
26+
27+
// property example explicitly set to null (allowed as the property isn't required)
28+
$example = new Example(['example' => null]);
29+
$example->getExample(); // returns NULL
30+
31+
// property example set to a custom value
32+
$example = new Example(['example' => 'My Input']);
33+
$example->getExample(); // returns "My Input"

0 commit comments

Comments
 (0)