Skip to content

Commit 32d86fe

Browse files
Merge pull request #72 from vadymtsots/file-upload-validation-fix
Fixed an issue with file validation
2 parents 13496f3 + e40634b commit 32d86fe

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/SimpleDTO.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Carbon\CarbonImmutable;
1010
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
1111
use Illuminate\Database\Eloquent\Model;
12+
use Illuminate\Http\UploadedFile;
1213
use Illuminate\Support\Collection;
1314
use Illuminate\Validation\ValidationException;
1415
use ReflectionClass;
@@ -430,7 +431,7 @@ private function isArrayable(mixed $value): bool
430431
$value instanceof Collection ||
431432
$value instanceof ValidatedDTO ||
432433
$value instanceof Model ||
433-
is_object($value);
434+
(is_object($value) && ! ($value instanceof UploadedFile));
434435
}
435436

436437
private function formatArrayableValue(mixed $value): array|int|string
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WendellAdriel\ValidatedDTO\Tests\Datasets;
6+
7+
use Illuminate\Http\UploadedFile;
8+
use WendellAdriel\ValidatedDTO\ValidatedDTO;
9+
10+
class ValidatedFileDTO extends ValidatedDTO
11+
{
12+
public UploadedFile $file;
13+
14+
protected function defaults(): array
15+
{
16+
return [];
17+
}
18+
19+
protected function casts(): array
20+
{
21+
return [];
22+
}
23+
24+
protected function rules(): array
25+
{
26+
return [
27+
'file' => 'required|file|mimes:jpg,jpeg,png',
28+
];
29+
}
30+
}

tests/Unit/ValidatedDTOTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Console\Command;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Http\Request;
11+
use Illuminate\Http\UploadedFile;
1112
use Illuminate\Validation\ValidationException;
1213
use WendellAdriel\ValidatedDTO\Exceptions\InvalidJsonException;
1314
use WendellAdriel\ValidatedDTO\Tests\Datasets\DummyBackedEnum;
@@ -25,6 +26,7 @@
2526
use WendellAdriel\ValidatedDTO\Tests\Datasets\UserNestedDTO;
2627
use WendellAdriel\ValidatedDTO\Tests\Datasets\ValidatedDTOInstance;
2728
use WendellAdriel\ValidatedDTO\Tests\Datasets\ValidatedEnumDTO;
29+
use WendellAdriel\ValidatedDTO\Tests\Datasets\ValidatedFileDTO;
2830
use WendellAdriel\ValidatedDTO\ValidatedDTO;
2931

3032
beforeEach(function () {
@@ -449,3 +451,17 @@ public function __invoke()
449451
->and($user->email)
450452
->toBe('[email protected]');
451453
});
454+
455+
it('validates that ValidatedDTO can be instantiated with file validation rules', function () {
456+
$uploadedFile = UploadedFile::fake()->image('avatar.jpg');
457+
$validatedDTO = ValidatedFileDTO::fromArray(['file' => $uploadedFile]);
458+
459+
expect($validatedDTO->validator->passes())
460+
->toBeTrue();
461+
});
462+
463+
it('validates that ValidateDTO cannot be instantiated with wrong mime type')
464+
->expect(function () {
465+
$uploadedFile = UploadedFile::fake()->create('document.pdf');
466+
ValidatedFileDTO::fromArray(['file' => $uploadedFile]);
467+
})->throws(ValidationException::class);

0 commit comments

Comments
 (0)