Skip to content

Commit 959323e

Browse files
committed
Validation with oneOf, allOf and anyOf only if property is set
Added check if property is defined before values are checked with ofProperty validators
1 parent 91ed23e commit 959323e

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/JsonSchema/Constraints/Undefined.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ protected function validateCommonProperties($value, $schema = null, $path = null
195195
*/
196196
protected function validateOfProperties($value, $schema, $path, $i = "")
197197
{
198+
// Verify type
199+
if ($value instanceof Undefined) {
200+
return;
201+
}
202+
198203
if (isset($schema->allOf)) {
199204
$isValid = true;
200205
foreach ($schema->allOf as $allOf) {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/*
3+
* This file is part of the JsonSchema package.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
namespace JsonSchema\Tests\Constraints;
9+
10+
use JsonSchema\Validator;
11+
12+
/**
13+
* Class OfPropertiesTest
14+
*/
15+
class OfPropertiesTest extends BaseTestCase
16+
{
17+
18+
public function getValidTests()
19+
{
20+
return array(
21+
array(
22+
'{"prop1": "abc"}',
23+
'{
24+
"type": "object",
25+
"properties": {
26+
"prop1": {"type": "string"},
27+
"prop2": {
28+
"oneOf": [
29+
{"type": "number"},
30+
{"type": "string"}
31+
]
32+
}
33+
},
34+
"required": ["prop1"]
35+
}'
36+
),
37+
array(
38+
'{"prop1": "abc", "prop2": 23}',
39+
'{
40+
"type": "object",
41+
"properties": {
42+
"prop1": {"type": "string"},
43+
"prop2": {
44+
"oneOf": [
45+
{"type": "number"},
46+
{"type": "string"}
47+
]
48+
}
49+
},
50+
"required": ["prop1"]
51+
}'
52+
),
53+
);
54+
}
55+
56+
public function getInvalidTests()
57+
{
58+
return array(
59+
array(
60+
'{"prop1": "abc", "prop2": []}',
61+
'{
62+
"type": "object",
63+
"properties": {
64+
"prop1": {"type": "string"},
65+
"prop2": {
66+
"oneOf": [
67+
{"type": "number"},
68+
{"type": "string"}
69+
]
70+
}
71+
},
72+
"required": ["prop1"]
73+
}',
74+
Validator::CHECK_MODE_NORMAL,
75+
array(
76+
array(
77+
"property" => "prop2",
78+
"message" => "array value found, but a number is required",
79+
),
80+
array(
81+
"property" => "prop2",
82+
"message" => "array value found, but a string is required",
83+
),
84+
array(
85+
"property" => "prop2",
86+
"message" => "failed to match exactly one schema",
87+
),
88+
),
89+
),
90+
);
91+
}
92+
}

0 commit comments

Comments
 (0)