emsifa / evo

Evolve your Laravel code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Strange behaviour with string property validation

emsifa opened this issue · comments

For example we have DTO like this:

class SomethingDto extends Dto
{
    public string $message;
}

When we send data below:

{
    "message": ""
}

It results an error "Message must be a string".

Expectation

It should not error because an empty string is a string.

So, this happens because by default in app/Http/Kernel.php Laravel enable \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class middleware which converts an empty string to null. If we want our DTO accepts empty strings, we have to notice user to disable the middleware.