Skip to content

Commit a8af693

Browse files
committed
Add boolean coercion cases from ajv matrix
1 parent d04e1d4 commit a8af693

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/JsonSchema/Constraints/TypeConstraint.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,17 @@ protected function validateType(&$value, $type, $coerce = false)
240240
*/
241241
protected function toBoolean($value)
242242
{
243-
if ($value === 'true') {
243+
if ($value === 1 || $value === 'true') {
244244
return true;
245245
}
246-
247-
if ($value === 'false') {
246+
if (is_null($value) || $value === 0 || $value === 'false') {
248247
return false;
249248
}
249+
if ($this->getTypeCheck()->isArray($value) && count($value) === 1) {
250+
reset($value);
251+
252+
return $this->toBoolean(current($value));
253+
}
250254

251255
return $value;
252256
}
@@ -275,6 +279,13 @@ protected function toNumber($value)
275279
return $value;
276280
}
277281

282+
/**
283+
* Converts a value to an integer
284+
*
285+
* @param mixed $value
286+
*
287+
* @return mixed
288+
*/
278289
protected function toInteger($value)
279290
{
280291
$numberValue = $this->toNumber($value);

tests/Constraints/CoerciveTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function testValidCoerceCases($input, $schema, $errors = array())
6161
$this->assertTrue(gettype($value->integer) == 'integer');
6262
$this->assertTrue(gettype($value->negativeInteger) == 'integer');
6363
$this->assertTrue(gettype($value->boolean) == 'boolean');
64+
$this->assertTrue(gettype($value->numberBoolean) == 'boolean');
65+
$this->assertTrue(gettype($value->nullBoolean) == 'boolean');
66+
$this->assertTrue(gettype($value->arrayBoolean) == 'boolean');
6467

6568
$this->assertTrue(gettype($value->multitype1) == 'boolean');
6669
$this->assertTrue(gettype($value->multitype2) == 'double');
@@ -74,6 +77,9 @@ public function testValidCoerceCases($input, $schema, $errors = array())
7477
$this->assertTrue($value->integer === 1);
7578
$this->assertTrue($value->negativeInteger === -2);
7679
$this->assertTrue($value->boolean === true);
80+
$this->assertTrue($value->numberBoolean === true);
81+
$this->assertTrue($value->nullBoolean === false);
82+
$this->assertTrue($value->arrayBoolean === true);
7783

7884
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
7985
}
@@ -131,6 +137,9 @@ public function getValidCoerceTests()
131137
"integer":"1",
132138
"negativeInteger":"-2",
133139
"boolean":"true",
140+
"numberBoolean":1,
141+
"nullBoolean":null,
142+
"arrayBoolean":["true"],
134143
"object":{},
135144
"array":[],
136145
"nullArray":null,
@@ -162,6 +171,9 @@ public function getValidCoerceTests()
162171
"integer":{"type":"integer"},
163172
"negativeInteger":{"type":"integer"},
164173
"boolean":{"type":"boolean"},
174+
"numberBoolean":{"type":"boolean"},
175+
"nullBoolean":{"type":"boolean"},
176+
"arrayBoolean":{"type":"boolean"},
165177
"object":{"type":"object"},
166178
"array":{"type":"array"},
167179
"nullArray":{"type":"array"},

0 commit comments

Comments
 (0)