Skip to content

Commit

Permalink
Merge pull request #72 from vadymtsots/file-upload-validation-fix
Browse files Browse the repository at this point in the history
Fixed an issue with file validation
  • Loading branch information
WendellAdriel committed Feb 5, 2024
2 parents 13496f3 + e40634b commit 32d86fe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/SimpleDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Illuminate\Validation\ValidationException;
use ReflectionClass;
Expand Down Expand Up @@ -430,7 +431,7 @@ private function isArrayable(mixed $value): bool
$value instanceof Collection ||
$value instanceof ValidatedDTO ||
$value instanceof Model ||
is_object($value);
(is_object($value) && ! ($value instanceof UploadedFile));
}

private function formatArrayableValue(mixed $value): array|int|string
Expand Down
30 changes: 30 additions & 0 deletions tests/Datasets/ValidatedFileDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace WendellAdriel\ValidatedDTO\Tests\Datasets;

use Illuminate\Http\UploadedFile;
use WendellAdriel\ValidatedDTO\ValidatedDTO;

class ValidatedFileDTO extends ValidatedDTO
{
public UploadedFile $file;

protected function defaults(): array
{
return [];
}

protected function casts(): array
{
return [];
}

protected function rules(): array
{
return [
'file' => 'required|file|mimes:jpg,jpeg,png',
];
}
}
16 changes: 16 additions & 0 deletions tests/Unit/ValidatedDTOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\ValidationException;
use WendellAdriel\ValidatedDTO\Exceptions\InvalidJsonException;
use WendellAdriel\ValidatedDTO\Tests\Datasets\DummyBackedEnum;
Expand All @@ -25,6 +26,7 @@
use WendellAdriel\ValidatedDTO\Tests\Datasets\UserNestedDTO;
use WendellAdriel\ValidatedDTO\Tests\Datasets\ValidatedDTOInstance;
use WendellAdriel\ValidatedDTO\Tests\Datasets\ValidatedEnumDTO;
use WendellAdriel\ValidatedDTO\Tests\Datasets\ValidatedFileDTO;
use WendellAdriel\ValidatedDTO\ValidatedDTO;

beforeEach(function () {
Expand Down Expand Up @@ -449,3 +451,17 @@ public function __invoke()
->and($user->email)
->toBe('john.doe@example.com');
});

it('validates that ValidatedDTO can be instantiated with file validation rules', function () {
$uploadedFile = UploadedFile::fake()->image('avatar.jpg');
$validatedDTO = ValidatedFileDTO::fromArray(['file' => $uploadedFile]);

expect($validatedDTO->validator->passes())
->toBeTrue();
});

it('validates that ValidateDTO cannot be instantiated with wrong mime type')
->expect(function () {
$uploadedFile = UploadedFile::fake()->create('document.pdf');
ValidatedFileDTO::fromArray(['file' => $uploadedFile]);
})->throws(ValidationException::class);

0 comments on commit 32d86fe

Please sign in to comment.