refactor(posts): centralize inline media validation in PostMediaRules
The media.* rules were duplicated across the web update request and both API requests (and diverged: web requires hosted id+path and tracks source; the API accepts a bare external url it downloads). Pull them into one App\Support\PostMediaRules::rules(hosted:) — same pattern as PostPlatformMetaRules — parameterized by contract, so there's a single place to add a media key and the validated()-strips-unlisted-keys footgun can't drift between entry points. Behavior is unchanged (each ruleset is reproduced exactly). Web store keeps its loose 'media' => array (no item rules) and is left out on purpose — adding strict rules there would change the web create contract.
This commit is contained in:
parent
8d7dcdf6eb
commit
1242cc7384
5 changed files with 91 additions and 30 deletions
|
|
@ -9,6 +9,7 @@
|
|||
use App\Models\SocialAccount;
|
||||
use App\Rules\ContentFitsPlatformLimits;
|
||||
use App\Rules\ContentTypeMatchesPlatform;
|
||||
use App\Support\PostMediaRules;
|
||||
use App\Support\PostPlatformMetaRules;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Collection;
|
||||
|
|
@ -35,15 +36,7 @@ public function rules(): array
|
|||
[new ContentFitsPlatformLimits($this->resolveSelectedPlatforms($workspaceId))]
|
||||
),
|
||||
],
|
||||
'media' => ['sometimes', 'array'],
|
||||
'media.*.id' => ['sometimes', 'nullable', 'string'],
|
||||
'media.*.path' => ['sometimes', 'nullable', 'string', 'max:500'],
|
||||
'media.*.url' => ['required', 'string', 'max:2048', 'url:http,https'],
|
||||
'media.*.type' => ['sometimes', 'nullable', 'string', 'max:32'],
|
||||
'media.*.mime_type' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||
'media.*.original_filename' => ['sometimes', 'nullable', 'string', 'max:500'],
|
||||
'media.*.size' => ['sometimes', 'nullable', 'integer'],
|
||||
'media.*.meta' => ['sometimes', 'nullable', 'array'],
|
||||
...PostMediaRules::rules(hosted: false),
|
||||
'platforms' => ['required', 'array', 'min:1'],
|
||||
'platforms.*.social_account_id' => [
|
||||
'required',
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
use App\Rules\ContentFitsPlatformLimits;
|
||||
use App\Rules\ContentTypeCompatibleWithMedia;
|
||||
use App\Rules\ContentTypeMatchesPostPlatform;
|
||||
use App\Support\PostMediaRules;
|
||||
use App\Support\PostPlatformMetaRules;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Collection;
|
||||
|
|
@ -44,15 +45,7 @@ public function rules(): array
|
|||
[new ContentFitsPlatformLimits($this->resolveSelectedPlatforms())]
|
||||
),
|
||||
],
|
||||
'media' => ['sometimes', 'array'],
|
||||
'media.*.id' => ['sometimes', 'nullable', 'string'],
|
||||
'media.*.path' => ['sometimes', 'nullable', 'string', 'max:500'],
|
||||
'media.*.url' => ['required', 'string', 'max:2048', 'url:http,https'],
|
||||
'media.*.type' => ['sometimes', 'nullable', 'string', 'max:32'],
|
||||
'media.*.mime_type' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||
'media.*.original_filename' => ['sometimes', 'nullable', 'string', 'max:500'],
|
||||
'media.*.size' => ['sometimes', 'nullable', 'integer'],
|
||||
'media.*.meta' => ['sometimes', 'nullable', 'array'],
|
||||
...PostMediaRules::rules(hosted: false),
|
||||
'platforms' => ['sometimes', 'array'],
|
||||
'platforms.*.id' => ['required', 'uuid', Rule::exists('post_platforms', 'id')->where('post_id', $this->route('post') instanceof Post ? $this->route('post')->id : $this->route('post'))],
|
||||
'platforms.*.content_type' => [
|
||||
|
|
|
|||
|
|
@ -4,12 +4,12 @@
|
|||
|
||||
namespace App\Http\Requests\App\Post;
|
||||
|
||||
use App\Enums\Media\Source;
|
||||
use App\Enums\Post\Status;
|
||||
use App\Enums\PostPlatform\ContentType;
|
||||
use App\Enums\SocialAccount\Platform;
|
||||
use App\Rules\ContentFitsPlatformLimits;
|
||||
use App\Rules\ContentTypeCompatibleWithMedia;
|
||||
use App\Support\PostMediaRules;
|
||||
use App\Support\PostPlatformMetaRules;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Collection;
|
||||
|
|
@ -42,17 +42,7 @@ public function rules(): array
|
|||
[new ContentFitsPlatformLimits($this->resolveSelectedPlatforms())]
|
||||
),
|
||||
],
|
||||
'media' => ['sometimes', 'array'],
|
||||
'media.*.id' => ['required', 'string'],
|
||||
'media.*.path' => ['required', 'string', 'max:500'],
|
||||
'media.*.url' => ['required', 'string', 'max:2048'],
|
||||
'media.*.type' => ['sometimes', 'nullable', 'string', 'max:32'],
|
||||
'media.*.mime_type' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||
'media.*.original_filename' => ['sometimes', 'nullable', 'string', 'max:500'],
|
||||
'media.*.size' => ['sometimes', 'nullable', 'integer'],
|
||||
'media.*.meta' => ['sometimes', 'nullable', 'array'],
|
||||
'media.*.source' => ['sometimes', 'nullable', 'string', Rule::in(array_column(Source::cases(), 'value'))],
|
||||
'media.*.source_meta' => ['sometimes', 'nullable', 'array'],
|
||||
...PostMediaRules::rules(hosted: true),
|
||||
'scheduled_at' => [
|
||||
'sometimes',
|
||||
'nullable',
|
||||
|
|
|
|||
47
app/Support/PostMediaRules.php
Normal file
47
app/Support/PostMediaRules.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Enums\Media\Source;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
/**
|
||||
* Single source of truth for inline post `media` validation, shared by the post
|
||||
* create/update flows. The web sends already-hosted media (id + path) and tracks
|
||||
* its source; the public REST API may send a bare external `url` we download and
|
||||
* host, so the id/path/url rules differ by contract.
|
||||
*/
|
||||
class PostMediaRules
|
||||
{
|
||||
/**
|
||||
* @param bool $hosted true (web): items must already be hosted (id + path
|
||||
* required) and may carry source metadata; false (API):
|
||||
* a bare external `url` is accepted and downloaded.
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function rules(bool $hosted): array
|
||||
{
|
||||
$rules = [
|
||||
'media' => ['sometimes', 'array'],
|
||||
'media.*.id' => $hosted ? ['required', 'string'] : ['sometimes', 'nullable', 'string'],
|
||||
'media.*.path' => $hosted ? ['required', 'string', 'max:500'] : ['sometimes', 'nullable', 'string', 'max:500'],
|
||||
'media.*.url' => $hosted
|
||||
? ['required', 'string', 'max:2048']
|
||||
: ['required', 'string', 'max:2048', 'url:http,https'],
|
||||
'media.*.type' => ['sometimes', 'nullable', 'string', 'max:32'],
|
||||
'media.*.mime_type' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||
'media.*.original_filename' => ['sometimes', 'nullable', 'string', 'max:500'],
|
||||
'media.*.size' => ['sometimes', 'nullable', 'integer'],
|
||||
'media.*.meta' => ['sometimes', 'nullable', 'array'],
|
||||
];
|
||||
|
||||
if ($hosted) {
|
||||
$rules['media.*.source'] = ['sometimes', 'nullable', 'string', Rule::in(array_column(Source::cases(), 'value'))];
|
||||
$rules['media.*.source_meta'] = ['sometimes', 'nullable', 'array'];
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
38
tests/Unit/Support/PostMediaRulesTest.php
Normal file
38
tests/Unit/Support/PostMediaRulesTest.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Support\PostMediaRules;
|
||||
|
||||
test('hosted media rules require id and path and allow source', function () {
|
||||
$rules = PostMediaRules::rules(hosted: true);
|
||||
|
||||
expect($rules['media.*.id'])->toContain('required')
|
||||
->and($rules['media.*.path'])->toContain('required')
|
||||
->and($rules)->toHaveKey('media.*.source')
|
||||
->and($rules)->toHaveKey('media.*.source_meta');
|
||||
});
|
||||
|
||||
test('api media rules accept a bare external url and omit source', function () {
|
||||
$rules = PostMediaRules::rules(hosted: false);
|
||||
|
||||
expect($rules['media.*.id'])->toContain('nullable')
|
||||
->and($rules['media.*.url'])->toContain('url:http,https')
|
||||
->and($rules)->not->toHaveKey('media.*.source')
|
||||
->and($rules)->not->toHaveKey('media.*.source_meta');
|
||||
});
|
||||
|
||||
test('both variants keep the shared item keys so validated() preserves them', function () {
|
||||
foreach ([true, false] as $hosted) {
|
||||
expect(PostMediaRules::rules(hosted: $hosted))->toHaveKeys([
|
||||
'media.*.id',
|
||||
'media.*.path',
|
||||
'media.*.url',
|
||||
'media.*.type',
|
||||
'media.*.mime_type',
|
||||
'media.*.original_filename',
|
||||
'media.*.size',
|
||||
'media.*.meta',
|
||||
]);
|
||||
}
|
||||
});
|
||||
Loading…
Reference in a new issue