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

Coerce empty string to null #381

Closed
wants to merge 3 commits into from
Closed
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
20 changes: 20 additions & 0 deletions src/JsonSchema/Constraints/TypeConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ protected function validateType(&$value, $type)
}

if ('null' === $type) {
if ($coerce) {
$value = $this->toNull($value);
}

return is_null($value);
}

Expand All @@ -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.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Constraints/BasicTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ public function getInvalidTests()
"additionalProperties":false
}'
),
array(
'{
"null":""
}',
'{
"type":"object",
"properties": {
"null":{"type":"null"}
}
}'
),
array(
'{
"null":1
Expand Down
11 changes: 5 additions & 6 deletions tests/Constraints/CoerciveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,28 @@ 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);

$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(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));
}

Expand Down Expand Up @@ -128,7 +127,7 @@ public function getValidCoerceTests()
"boolean":"true",
"object":{},
"array":[],
"null":null,
"null":"",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice that you have lots of explicit tests in testValidCoerceCases, in addition to simply requiring the schema to validate, but that null coercion hasn't been added as one of them. Is there a reason for that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. Can you provide an example?

I believe I originally created this file by duplicating BasicTypesTest.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh, I think I get what you mean. One sec...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a post-validate test for null.

I also removed what looked like a duplicated block of explicit tests in the same section of code. Not sure if there was any purpose for it...

"any": "string",
"allOf": "1",
"multitype1": "false",
Expand Down