Skip to content

Commit 08c3cf4

Browse files
committed
feat: Add support for Draft 6 in NumberConstraint
1 parent b580100 commit 08c3cf4

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/JsonSchema/Constraints/NumberConstraint.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class NumberConstraint extends Constraint
2626
public function check(&$element, $schema = null, JsonPointer $path = null, $i = null)
2727
{
2828
// Verify minimum
29-
if (isset($schema->exclusiveMinimum)) {
29+
if (isset($schema->exclusiveMinimum) && filter_var($schema->exclusiveMinimum, FILTER_VALIDATE_BOOLEAN)) {
30+
// Draft 4 schema
3031
if (isset($schema->minimum)) {
3132
if ($schema->exclusiveMinimum && $element <= $schema->minimum) {
3233
$this->addError(ConstraintError::EXCLUSIVE_MINIMUM(), $path, array('minimum' => $schema->minimum));
@@ -36,12 +37,18 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
3637
} else {
3738
$this->addError(ConstraintError::MISSING_MINIMUM(), $path);
3839
}
40+
} elseif (isset($schema->exclusiveMinimum) && filter_var($schema->exclusiveMinimum, FILTER_VALIDATE_INT)) {
41+
// Draft 6 schema
42+
if ($element <= $schema->exclusiveMinimum) {
43+
$this->addError(ConstraintError::EXCLUSIVE_MINIMUM(), $path, array('exclusiveMinimum' => $schema->exclusiveMinimum));
44+
}
3945
} elseif (isset($schema->minimum) && $element < $schema->minimum) {
4046
$this->addError(ConstraintError::MINIMUM(), $path, array('minimum' => $schema->minimum));
4147
}
4248

4349
// Verify maximum
44-
if (isset($schema->exclusiveMaximum)) {
50+
if (isset($schema->exclusiveMaximum) && filter_var($schema->exclusiveMaximum, FILTER_VALIDATE_INT)) {
51+
// Draft 4 schema
4552
if (isset($schema->maximum)) {
4653
if ($schema->exclusiveMaximum && $element >= $schema->maximum) {
4754
$this->addError(ConstraintError::EXCLUSIVE_MAXIMUM(), $path, array('maximum' => $schema->maximum));
@@ -51,6 +58,11 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
5158
} else {
5259
$this->addError(ConstraintError::MISSING_MAXIMUM(), $path);
5360
}
61+
} elseif (isset($schema->exclusiveMaximum) && filter_var($schema->exclusiveMaximum, FILTER_VALIDATE_INT)) {
62+
// Draft 6 schema
63+
if ($element >= $schema->exclusiveMaximum) {
64+
$this->addError(ConstraintError::EXCLUSIVE_MAXIMUM(), $path, array('exclusiveMaximum' => $schema->exclusiveMaximum));
65+
}
5466
} elseif (isset($schema->maximum) && $element > $schema->maximum) {
5567
$this->addError(ConstraintError::MAXIMUM(), $path, array('maximum' => $schema->maximum));
5668
}

tests/Constraints/MinimumMaximumTest.php

+28-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,25 @@ public function getInvalidTests()
2828
}'
2929
),
3030
array(
31-
'{"value": 3}',
3231
'{
33-
"type": "object",
34-
"properties": {
35-
"value": {"type": "integer", "minimum": 3, "exclusiveMinimum": true}
36-
}
32+
"value":16
33+
}',
34+
'{
35+
"type":"object",
36+
"properties":{
37+
"value":{"type":"integer","maximum":8}
38+
}
39+
}'
40+
),
41+
array(
42+
'{
43+
"value":2
44+
}',
45+
'{
46+
"type":"object",
47+
"properties":{
48+
"value":{"type":"integer","exclusiveMinimum":2}
49+
}
3750
}'
3851
),
3952
array(
@@ -43,10 +56,19 @@ public function getInvalidTests()
4356
'{
4457
"type":"object",
4558
"properties":{
46-
"value":{"type":"integer","maximum":8}
59+
"value":{"type":"integer","exclusiveMaximum":16}
4760
}
4861
}'
4962
),
63+
array(
64+
'{"value": 3}',
65+
'{
66+
"type": "object",
67+
"properties": {
68+
"value": {"type": "integer", "minimum": 3, "exclusiveMinimum": true}
69+
}
70+
}'
71+
),
5072
array(
5173
'{"value": 8}',
5274
'{

0 commit comments

Comments
 (0)