chore: better validation rules

This commit is contained in:
Phan An 2024-01-10 21:04:37 +01:00
parent f22f1e0cba
commit de44bc781b
11 changed files with 40 additions and 20 deletions

View file

@ -14,8 +14,7 @@ class AddSongsToPlaylistRequest extends Request
public function rules(): array
{
return [
'songs' => 'required|array',
'songs.*' => [Rule::exists(Song::class, 'id')],
'songs' => ['required', 'array', Rule::exists(Song::class, 'id')],
];
}
}

View file

@ -2,6 +2,9 @@
namespace App\Http\Requests\API;
use App\Models\Song;
use Illuminate\Validation\Rule;
/**
* @property array<string> $songs
*/
@ -11,7 +14,7 @@ class BatchInteractionRequest extends Request
public function rules(): array
{
return [
'songs' => 'required|array|exists:songs,id',
'songs' => ['required', 'array', Rule::exists(Song::class, 'id')],
];
}
}

View file

@ -3,6 +3,8 @@
namespace App\Http\Requests\API;
use App\Facades\License;
use App\Models\Song;
use Illuminate\Validation\Rule;
/**
* @property-read array<string> $songs
@ -13,7 +15,7 @@ class ChangeSongsVisibilityRequest extends Request
public function rules(): array
{
return [
'songs' => 'required|exists:songs,id',
'songs' => ['required', 'array', Rule::exists(Song::class, 'id')],
];
}

View file

@ -2,6 +2,9 @@
namespace App\Http\Requests\API;
use App\Models\Song;
use Illuminate\Validation\Rule;
/** @property-read array<string> $songs */
class DeleteSongsRequest extends Request
{
@ -9,7 +12,7 @@ class DeleteSongsRequest extends Request
public function rules(): array
{
return [
'songs' => 'required|array|exists:songs,id',
'songs' => ['required', 'array', Rule::exists(Song::class, 'id')],
];
}
}

View file

@ -3,7 +3,7 @@
namespace App\Http\Requests\API;
use App\Models\Playlist;
use App\Rules\AllPlaylistsBelongToUser;
use App\Rules\AllPlaylistsBelongTo;
use Illuminate\Validation\Rule;
/**
@ -15,8 +15,12 @@ class PlaylistFolderPlaylistDestroyRequest extends Request
public function rules(): array
{
return [
'playlists' => ['required', 'array', new AllPlaylistsBelongToUser($this->user())],
'playlists.*' => [Rule::exists(Playlist::class, 'id')],
'playlists' => [
'required',
'array',
new AllPlaylistsBelongTo($this->user()),
Rule::exists(Playlist::class, 'id'),
],
];
}
}

View file

@ -3,7 +3,7 @@
namespace App\Http\Requests\API;
use App\Models\Playlist;
use App\Rules\AllPlaylistsBelongToUser;
use App\Rules\AllPlaylistsBelongTo;
use Illuminate\Validation\Rule;
/**
@ -15,8 +15,12 @@ class PlaylistFolderPlaylistStoreRequest extends Request
public function rules(): array
{
return [
'playlists' => ['required', 'array', new AllPlaylistsBelongToUser($this->user())],
'playlists.*' => [Rule::exists(Playlist::class, 'id')],
'playlists' => [
'required',
'array',
new AllPlaylistsBelongTo($this->user()),
Rule::exists(Playlist::class, 'id'),
],
];
}
}

View file

@ -2,6 +2,9 @@
namespace App\Http\Requests\API;
use App\Models\Song;
use Illuminate\Validation\Rule;
/**
* @property-read array<string> $songs
*/
@ -11,7 +14,7 @@ class RemoveSongsFromPlaylistRequest extends Request
public function rules(): array
{
return [
'songs' => 'required|array|exists:songs,id',
'songs' => ['required', 'array', Rule::exists(Song::class, 'id')],
];
}
}

View file

@ -2,9 +2,12 @@
namespace App\Http\Requests\API;
use App\Models\Song;
use Illuminate\Validation\Rule;
/**
* @property array<string> $songs
* @property array<mixed> $data
* @property-read array<string> $songs
* @property-read array<mixed> $data
*/
class SongUpdateRequest extends Request
{
@ -13,7 +16,7 @@ class SongUpdateRequest extends Request
{
return [
'data' => 'required|array',
'songs' => 'required|array|exists:songs,id',
'songs' => ['required', 'array', Rule::exists(Song::class, 'id')],
];
}
}

View file

@ -14,8 +14,7 @@ class UpdateQueueStateRequest extends Request
public function rules(): array
{
return [
'songs' => 'array',
'songs.*' => [Rule::exists(Song::class, 'id')],
'songs' => ['array', Rule::exists(Song::class, 'id')],
];
}
}

View file

@ -6,7 +6,7 @@ use App\Models\User;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Arr;
class AllPlaylistsBelongToUser implements Rule
final class AllPlaylistsBelongTo implements Rule
{
public function __construct(private User $user)
{

View file

@ -45,7 +45,7 @@ class InteractionTest extends TestCase
]);
}
public function testToggle(): void
public function testToggleLike(): void
{
$this->expectsEvents(SongLikeToggled::class);
@ -72,7 +72,7 @@ class InteractionTest extends TestCase
]);
}
public function testToggleBatch(): void
public function testToggleLikeBatch(): void
{
$this->expectsEvents(SongsBatchLiked::class);