lundi 8 juillet 2019

How can validate a uploaded file by filename?

I have a file that extends from the Request class to customize my validation rule, in which I have validated a file type field with a maximum size and a specific length for the file name. To make it simpler to try with

php artisan tinker

You can reproduce the error:

Validator::make(['file' => Illuminate\Http\UploadedFile::fake()
         ->create('page.pdf')],['file' => 'regex:/^.*$/'])->fails();

With this code generate a file dynamically with faker to be uploaded and gave a specific name, apply a validation and try to check if it fails or not.

The regular expression used indicates that the name can be any character with an open dimension has no limit. This should return false since it should not fail. However, it fails.

To know if I'm okay with respect to regular expression we can use:

preg_match('/^.*$/', 'page.pdf');

In my case it returns 1 indicating that it found the match because the documentation indicates that 1 is that it found the match, 0 that it does not and false that an error occurred.

So what could I be doing wrong when using regex to validate an UploadedFile with Laravel in my unit tests?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire