Skip to content

Commit 44c6787

Browse files
authored
Merge pull request #591 from erayd/backport-529
## Backported PRs * #559 ArraysTest for array items with enum validation * #567 Don't run checks which assume a defined instance against undefined * #575 Tests on PHP 7.3 * #587 Fixed PHPDoc of Validator::validate() method * #583 Fix travis PHP 5.4 and 5.5 config ## Additional PRs (5.x.x only) These PRs are only applicable to the 5.x.x branch, and have been merged individually. * #589 Update validate-json to use spl_autoload_register ## Skipped PRs * #464 marc-mabe/php-enum versions (dependency not present in 5.x.x)
2 parents e8b7614 + 5bc9862 commit 44c6787

File tree

7 files changed

+134
-5
lines changed

7 files changed

+134
-5
lines changed

.php_cs.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ $config
2121
'phpdoc_order' => true,
2222
'phpdoc_summary' => false,
2323
'pre_increment' => false,
24+
'increment_style' => false,
2425
'simplified_null_return' => false,
2526
'trailing_comma_in_multiline_array' => false,
27+
'yoda_style' => false,
28+
'phpdoc_types_order' => array('null_adjustment' => 'none', 'sort_algorithm' => 'none'),
2629
))
2730
->setFinder($finder)
2831
;

.travis.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ matrix:
1212
- php: 5.3
1313
dist: precise
1414
- php: 5.4
15+
dist: trusty
1516
- php: 5.5
17+
dist: trusty
1618
- php: 5.6
1719
- php: 7.0
18-
env: WITH_COVERAGE=true WITH_PHPCSFIXER=true
20+
env: WITH_COVERAGE=true
21+
- php: 7.0
22+
env: WITH_PHPCSFIXER=true
1923
- php: 7.1
2024
- php: 7.2
25+
- php: 7.3
2126
- php: 'nightly'
2227
- php: hhvm
2328
dist: trusty

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"php": ">=5.3.3"
3131
},
3232
"require-dev": {
33-
"friendsofphp/php-cs-fixer": "~2.2.20",
33+
"friendsofphp/php-cs-fixer": "~2.2.20||~2.15.1",
3434
"json-schema/JSON-Schema-Test-Suite": "1.2.0",
3535
"phpunit/phpunit": "^4.8.35"
3636
},

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
150150
'required'
151151
);
152152
}
153+
} else {
154+
// If the value is both undefined and not required, skip remaining checks
155+
// in this method which assume an actual, defined instance when validating.
156+
if ($value instanceof self) {
157+
return;
158+
}
153159
}
154160
}
155161

src/JsonSchema/Validator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ class Validator extends BaseConstraint
3434
* Both the php object and the schema are supposed to be a result of a json_decode call.
3535
* The validation works as defined by the schema proposal in http://json-schema.org.
3636
*
37-
* Note that the first argument is passwd by reference, so you must pass in a variable.
38-
*
39-
* {@inheritdoc}
37+
* Note that the first argument is passed by reference, so you must pass in a variable.
4038
*/
4139
public function validate(&$value, $schema = null, $checkMode = null)
4240
{

tests/Constraints/ArraysTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,51 @@ public function getInvalidTests()
7171
}
7272
}
7373
}'
74+
),
75+
array( // Test array items.enum where type string fail validation if value(s) is/are not in items.enum
76+
'{"data": ["a", "b"]}',
77+
'{
78+
"type": "object",
79+
"properties": {
80+
"data": {
81+
"type": "array",
82+
"items": {
83+
"type": "string",
84+
"enum": ["b", "c"]
85+
}
86+
}
87+
}
88+
}'
89+
),
90+
array( // Test array items.enum where type integer fail validation if value(s) is/are not in items.enum
91+
'{"data": [1, 2]}',
92+
'{
93+
"type": "object",
94+
"properties": {
95+
"data": {
96+
"type": "array",
97+
"items": {
98+
"type": "integer",
99+
"enum": [2, 3]
100+
}
101+
}
102+
}
103+
}'
104+
),
105+
array( // Test array items.enum where type number fail validation if value(s) is/are not in items.enum
106+
'{"data": [1.25, 2.25]}',
107+
'{
108+
"type": "object",
109+
"properties": {
110+
"data": {
111+
"type": "array",
112+
"items": {
113+
"type": "number",
114+
"enum": [1.25, 2]
115+
}
116+
}
117+
}
118+
}'
74119
)
75120
);
76121
}
@@ -168,6 +213,51 @@ public function getValidTests()
168213
}
169214
}
170215
}'
216+
),
217+
array( // Test array items.enum where type string passes validation if value(s) is/are in items.enum
218+
'{"data": ["c", "c", "b"]}',
219+
'{
220+
"type": "object",
221+
"properties": {
222+
"data": {
223+
"type": "array",
224+
"items": {
225+
"type": "string",
226+
"enum": ["b", "c"]
227+
}
228+
}
229+
}
230+
}'
231+
),
232+
array( // Test array items.enum where type integer passes validation if value(s) is/are in items.enum
233+
'{"data": [1, 1, 2]}',
234+
'{
235+
"type": "object",
236+
"properties": {
237+
"data": {
238+
"type": "array",
239+
"items": {
240+
"type": "integer",
241+
"enum": [1, 2]
242+
}
243+
}
244+
}
245+
}'
246+
),
247+
array( // Test array items.enum where type number passes validation if value(s) is/are in items.enum
248+
'{"data": [1.25, 1.25, 2.25]}',
249+
'{
250+
"type": "object",
251+
"properties": {
252+
"data": {
253+
"type": "array",
254+
"items": {
255+
"type": "number",
256+
"enum": [1.25, 2.25]
257+
}
258+
}
259+
}
260+
}'
171261
)
172262
);
173263
}

tests/Constraints/NotTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ public function getInvalidTests()
3131
}
3232
}
3333
}'
34+
),
35+
array( // check that a missing, required property is correctly validated
36+
'{"y": "foo"}',
37+
'{
38+
"type": "object",
39+
"required": ["x"],
40+
"properties": {
41+
"x": {
42+
"not": {
43+
"type": "null"
44+
}
45+
}
46+
}
47+
}'
3448
)
3549
);
3650
}
@@ -69,6 +83,19 @@ public function getValidTests()
6983
}
7084
}
7185
}'
86+
),
87+
array( // check that a missing, non-required property isn't validated
88+
'{"y": "foo"}',
89+
'{
90+
"type": "object",
91+
"properties": {
92+
"x": {
93+
"not": {
94+
"type": "null"
95+
}
96+
}
97+
}
98+
}'
7299
)
73100
);
74101
}

0 commit comments

Comments
 (0)