Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue with file validation #72

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading