Skip to content

Commit

Permalink
feat: Add support for Draft 6 in NumberConstraint
Browse files Browse the repository at this point in the history
  • Loading branch information
llupa committed Jan 11, 2024
1 parent b580100 commit 08c3cf4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/JsonSchema/Constraints/NumberConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class NumberConstraint extends Constraint
public function check(&$element, $schema = null, JsonPointer $path = null, $i = null)
{
// Verify minimum
if (isset($schema->exclusiveMinimum)) {
if (isset($schema->exclusiveMinimum) && filter_var($schema->exclusiveMinimum, FILTER_VALIDATE_BOOLEAN)) {
// Draft 4 schema
if (isset($schema->minimum)) {
if ($schema->exclusiveMinimum && $element <= $schema->minimum) {
$this->addError(ConstraintError::EXCLUSIVE_MINIMUM(), $path, array('minimum' => $schema->minimum));
Expand All @@ -36,12 +37,18 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
} else {
$this->addError(ConstraintError::MISSING_MINIMUM(), $path);
}
} elseif (isset($schema->exclusiveMinimum) && filter_var($schema->exclusiveMinimum, FILTER_VALIDATE_INT)) {
// Draft 6 schema
if ($element <= $schema->exclusiveMinimum) {
$this->addError(ConstraintError::EXCLUSIVE_MINIMUM(), $path, array('exclusiveMinimum' => $schema->exclusiveMinimum));
}
} elseif (isset($schema->minimum) && $element < $schema->minimum) {
$this->addError(ConstraintError::MINIMUM(), $path, array('minimum' => $schema->minimum));
}

// Verify maximum
if (isset($schema->exclusiveMaximum)) {
if (isset($schema->exclusiveMaximum) && filter_var($schema->exclusiveMaximum, FILTER_VALIDATE_INT)) {
// Draft 4 schema
if (isset($schema->maximum)) {
if ($schema->exclusiveMaximum && $element >= $schema->maximum) {
$this->addError(ConstraintError::EXCLUSIVE_MAXIMUM(), $path, array('maximum' => $schema->maximum));
Expand All @@ -51,6 +58,11 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
} else {
$this->addError(ConstraintError::MISSING_MAXIMUM(), $path);
}
} elseif (isset($schema->exclusiveMaximum) && filter_var($schema->exclusiveMaximum, FILTER_VALIDATE_INT)) {
// Draft 6 schema
if ($element >= $schema->exclusiveMaximum) {
$this->addError(ConstraintError::EXCLUSIVE_MAXIMUM(), $path, array('exclusiveMaximum' => $schema->exclusiveMaximum));
}
} elseif (isset($schema->maximum) && $element > $schema->maximum) {
$this->addError(ConstraintError::MAXIMUM(), $path, array('maximum' => $schema->maximum));
}
Expand Down
34 changes: 28 additions & 6 deletions tests/Constraints/MinimumMaximumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,25 @@ public function getInvalidTests()
}'
),
array(
'{"value": 3}',
'{
"type": "object",
"properties": {
"value": {"type": "integer", "minimum": 3, "exclusiveMinimum": true}
}
"value":16
}',
'{
"type":"object",
"properties":{
"value":{"type":"integer","maximum":8}
}
}'
),
array(
'{
"value":2
}',
'{
"type":"object",
"properties":{
"value":{"type":"integer","exclusiveMinimum":2}
}
}'
),
array(
Expand All @@ -43,10 +56,19 @@ public function getInvalidTests()
'{
"type":"object",
"properties":{
"value":{"type":"integer","maximum":8}
"value":{"type":"integer","exclusiveMaximum":16}
}
}'
),
array(
'{"value": 3}',
'{
"type": "object",
"properties": {
"value": {"type": "integer", "minimum": 3, "exclusiveMinimum": true}
}
}'
),
array(
'{"value": 8}',
'{
Expand Down

0 comments on commit 08c3cf4

Please sign in to comment.