Skip to content

Optional enums #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/JsonSchema/Constraints/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ class Enum extends Constraint
*/
public function check($element, $schema = null, $path = null, $i = null)
{
foreach ($schema->enum as $possibleValue) {
if ($possibleValue == $element) {
$found = true;
break;
}
// Only validate enum if the attribute exists
if ($element instanceof Undefined && (!isset($schema->required) || !$schema->required)) {
return;
}

if (!isset($found)) {
if (!in_array($element, $schema->enum)) {
$this->addError($path, "does not have a value in the enumeration " . implode(', ', $schema->enum));
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/JsonSchema/Tests/Constraints/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ public function getInvalidTests()
},
"additionalProperties":false
}'
),
array(
'{}',
'{
"type":"object",
"properties":{
"value":{
"type":"string",
"enum":["Abacate","Manga","Pitanga"],
"required":true
}
},
"additionalProperties":false
}'
)
);
}
Expand All @@ -43,6 +57,30 @@ public function getValidTests()
},
"additionalProperties":false
}'
),
array(
'{}',
'{
"type":"object",
"properties":{
"value":{"type":"string","enum":["Abacate","Manga","Pitanga"]}
},
"additionalProperties":false
}'
),
array(
'{}',
'{
"type":"object",
"properties":{
"value":{
"type":"string",
"enum":["Abacate","Manga","Pitanga"],
"required":false
}
},
"additionalProperties":false
}'
)
);
}
Expand Down