Skip to content
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

handle negative integers #325

Merged
merged 1 commit into from
Oct 24, 2016
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
2 changes: 1 addition & 1 deletion src/JsonSchema/Constraints/ObjectConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected function toNumber($value)

protected function toInteger($value)
{
if(ctype_digit ($value)) {
if(is_numeric($value) && (int)$value == $value) {
return (int)$value; // cast to number
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Constraints/CoerciveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,23 @@ public function testValidCoerceCases($input, $schema, $errors = array())

$this->assertTrue(gettype($value->number) == "double");
$this->assertTrue(gettype($value->integer) == "integer");
$this->assertTrue(gettype($value->negativeInteger) == "integer");
$this->assertTrue(gettype($value->boolean) == "boolean");

$this->assertTrue($value->number === 1.5);
$this->assertTrue($value->integer === 1);
$this->assertTrue($value->negativeInteger === -2);
$this->assertTrue($value->boolean === true);

$this->assertTrue(gettype($value->multitype1) == "boolean");
$this->assertTrue(gettype($value->multitype2) == "double");
$this->assertTrue(gettype($value->multitype3) == "integer");

$this->assertTrue($value->number === 1.5);
$this->assertTrue($value->integer === 1);
$this->assertTrue($value->negativeInteger === -2);
$this->assertTrue($value->boolean === true);

$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
}

Expand Down Expand Up @@ -111,6 +122,7 @@ public function getValidCoerceTests()
"string":"string test",
"number":"1.5",
"integer":"1",
"negativeInteger":"-2",
"boolean":"true",
"object":{},
"array":[],
Expand All @@ -132,6 +144,7 @@ public function getValidCoerceTests()
"string":{"type":"string"},
"number":{"type":"number"},
"integer":{"type":"integer"},
"negativeInteger":{"type":"integer"},
"boolean":{"type":"boolean"},
"object":{"type":"object"},
"array":{"type":"array"},
Expand Down