diff --git a/src/JsonSchema/Constraints/TypeConstraint.php b/src/JsonSchema/Constraints/TypeConstraint.php index 0ef32843..012dccbd 100644 --- a/src/JsonSchema/Constraints/TypeConstraint.php +++ b/src/JsonSchema/Constraints/TypeConstraint.php @@ -208,6 +208,10 @@ protected function validateType(&$value, $type) } if ('null' === $type) { + if ($coerce) { + $value = $this->toNull($value); + } + return is_null($value); } @@ -234,6 +238,22 @@ protected function toBoolean($value) return $value; } + /** + * Converts a value to null. For example, "" becomes null. + * + * @param $value The value to convert to null + * + * @return null|mixed + */ + protected function toNull($value) + { + if ($value === '') { + return null; + } + + return $value; + } + /** * Converts a numeric string to a number. For example, "4" becomes 4. * diff --git a/tests/Constraints/BasicTypesTest.php b/tests/Constraints/BasicTypesTest.php index 7daa43ad..a9994850 100644 --- a/tests/Constraints/BasicTypesTest.php +++ b/tests/Constraints/BasicTypesTest.php @@ -86,6 +86,17 @@ public function getInvalidTests() "additionalProperties":false }' ), + array( + '{ + "null":"" + }', + '{ + "type":"object", + "properties": { + "null":{"type":"null"} + } + }' + ), array( '{ "null":1 diff --git a/tests/Constraints/CoerciveTest.php b/tests/Constraints/CoerciveTest.php index cb5c5518..6b97cb9f 100644 --- a/tests/Constraints/CoerciveTest.php +++ b/tests/Constraints/CoerciveTest.php @@ -50,7 +50,9 @@ public function testValidCoerceCases($input, $schema, $errors = array()) $this->assertTrue(gettype($value->number) == 'string'); $this->assertTrue(gettype($value->integer) == 'string'); + $this->assertTrue(gettype($value->negativeInteger) == 'string'); $this->assertTrue(gettype($value->boolean) == 'string'); + $this->assertTrue(gettype($value->null) == 'string'); $validator->validate($value, $schema, $checkMode); @@ -58,21 +60,18 @@ public function testValidCoerceCases($input, $schema, $errors = array()) $this->assertTrue(gettype($value->integer) == 'integer'); $this->assertTrue(gettype($value->negativeInteger) == 'integer'); $this->assertTrue(gettype($value->boolean) == 'boolean'); + $this->assertTrue(gettype($value->null) == 'NULL'); $this->assertTrue($value->number === 1.5); $this->assertTrue($value->integer === 1); $this->assertTrue($value->negativeInteger === -2); $this->assertTrue($value->boolean === true); + $this->assertTrue($value->null === null); $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)); } @@ -128,7 +127,7 @@ public function getValidCoerceTests() "boolean":"true", "object":{}, "array":[], - "null":null, + "null":"", "any": "string", "allOf": "1", "multitype1": "false",