Skip to content

Commit

Permalink
Merge pull request #83 from dazz/undefinedOfProperty
Browse files Browse the repository at this point in the history
Validation with oneOf, allOf and anyOf only if property is set
  • Loading branch information
justinrainbow committed Dec 8, 2013
2 parents 91ed23e + 959323e commit f618a51
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/JsonSchema/Constraints/Undefined.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ protected function validateCommonProperties($value, $schema = null, $path = null
*/
protected function validateOfProperties($value, $schema, $path, $i = "")
{
// Verify type
if ($value instanceof Undefined) {
return;
}

if (isset($schema->allOf)) {
$isValid = true;
foreach ($schema->allOf as $allOf) {
Expand Down
92 changes: 92 additions & 0 deletions tests/JsonSchema/Tests/Constraints/OfPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/*
* This file is part of the JsonSchema package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JsonSchema\Tests\Constraints;

use JsonSchema\Validator;

/**
* Class OfPropertiesTest
*/
class OfPropertiesTest extends BaseTestCase
{

public function getValidTests()
{
return array(
array(
'{"prop1": "abc"}',
'{
"type": "object",
"properties": {
"prop1": {"type": "string"},
"prop2": {
"oneOf": [
{"type": "number"},
{"type": "string"}
]
}
},
"required": ["prop1"]
}'
),
array(
'{"prop1": "abc", "prop2": 23}',
'{
"type": "object",
"properties": {
"prop1": {"type": "string"},
"prop2": {
"oneOf": [
{"type": "number"},
{"type": "string"}
]
}
},
"required": ["prop1"]
}'
),
);
}

public function getInvalidTests()
{
return array(
array(
'{"prop1": "abc", "prop2": []}',
'{
"type": "object",
"properties": {
"prop1": {"type": "string"},
"prop2": {
"oneOf": [
{"type": "number"},
{"type": "string"}
]
}
},
"required": ["prop1"]
}',
Validator::CHECK_MODE_NORMAL,
array(
array(
"property" => "prop2",
"message" => "array value found, but a number is required",
),
array(
"property" => "prop2",
"message" => "array value found, but a string is required",
),
array(
"property" => "prop2",
"message" => "failed to match exactly one schema",
),
),
),
);
}
}

0 comments on commit f618a51

Please sign in to comment.