vlucas/valitron

Empty string is considered as boolean true

Closed this issue · 1 comments

heres my input
{
"action" : "get",
"data" : "something"
}
(json converted to array)

code
$v = new Valitron\Validator($this->request_data);
$v->rules([
'required' => ['action'],
'alpha' => ['action'],
'optional' => ['data'],
'boolean' => ['data'],
]);

output : Data must be a boolean

but when i set lable to boolean key, validate() returns true

    $v = new Valitron\Validator($this->request_data);
    $v->rules([
        'required'  => ['action'],
        'alpha' => ['action'],
        'optional'  =>  ['data'],
        'boolean'   =>  ['data' => "some"],
    ]);

output : Yay! We're all good!

is i am wrong somewhere ? or is it an issue ?

There seems to be a mistake in the second piece of code:

/* ... */
        'boolean'   =>  ['data' => "some"],
/* ... */

The array key will be ignored, and now Valitron expects a boolean value for a field named "some". That key probably doesn't exists in the request, so Valitron will ignore it.