From 1952780452bddb83b2ac672dc7e3fbdcbd363bdc Mon Sep 17 00:00:00 2001 From: Max Loeb Date: Mon, 24 Oct 2016 08:18:27 -0700 Subject: [PATCH] handle negative integers --- src/JsonSchema/Constraints/ObjectConstraint.php | 2 +- tests/Constraints/CoerciveTest.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index ebbf1e10..ca8068fe 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -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 } diff --git a/tests/Constraints/CoerciveTest.php b/tests/Constraints/CoerciveTest.php index d6fcb90a..5115988a 100644 --- a/tests/Constraints/CoerciveTest.php +++ b/tests/Constraints/CoerciveTest.php @@ -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)); } @@ -111,6 +122,7 @@ public function getValidCoerceTests() "string":"string test", "number":"1.5", "integer":"1", + "negativeInteger":"-2", "boolean":"true", "object":{}, "array":[], @@ -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"},