Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

feat(ai-templates): validate template key + require account when needed

Paulo Castellano8c72719f92e2
diff --git a/app/Http/Requests/App/Ai/StartPostCreationRequest.php b/app/Http/Requests/App/Ai/StartPostCreationRequest.php
index 40fe56a41..258ad4a52 100644
--- a/app/Http/Requests/App/Ai/StartPostCreationRequest.php
+++ b/app/Http/Requests/App/Ai/StartPostCreationRequest.php
@@ -4,9 +4,11 @@ declare(strict_types=1);
namespace App\Http\Requests\App\Ai;
+use App\Ai\Templates\AiTemplateRegistry;
use App\Enums\PostPlatform\ContentType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
+use Illuminate\Validation\Validator;
class StartPostCreationRequest extends FormRequest
{
@@ -33,6 +35,23 @@ class StartPostCreationRequest extends FormRequest
'image_count' => ['nullable', 'integer', 'min:0', 'max:10'],
'prompt' => ['required', 'string', 'max:2000'],
'date' => ['nullable', 'date_format:Y-m-d'],
+ 'template' => ['sometimes', 'string', Rule::in(app(AiTemplateRegistry::class)->keys())],
];
}
+
+ public function withValidator(Validator $validator): void
+ {
+ $validator->after(function (Validator $validator): void {
+ $key = (string) $this->input('template', 'image_card');
+ $registry = app(AiTemplateRegistry::class);
+
+ if (! in_array($key, $registry->keys(), true)) {
+ return;
+ }
+
+ if ($registry->find($key)->needsAccount() && blank($this->input('social_account_id'))) {
+ $validator->errors()->add('social_account_id', trans('validation.required', ['attribute' => 'social account']));
+ }
+ });
+ }
}
diff --git a/tests/Feature/Ai/PostAiCreateTest.php b/tests/Feature/Ai/PostAiCreateTest.php
index 297da2d76..90919810d 100644
--- a/tests/Feature/Ai/PostAiCreateTest.php
+++ b/tests/Feature/Ai/PostAiCreateTest.php
@@ -188,3 +188,41 @@ test('loading page rejects non-uuid creation ids', function () {
->get(route('app.posts.ai.loading', 'not-a-uuid'))
->assertStatus(Response::HTTP_NOT_FOUND);
});
+
+it('defaults to the image_card template when none is given', function () {
+ Bus::fake();
+
+ $this->actingAs($this->user)
+ ->postJson(route('app.posts.ai.create'), [
+ 'format' => 'instagram_feed',
+ 'prompt' => 'a post about coffee',
+ ])
+ ->assertAccepted();
+
+ Bus::assertDispatched(StreamPostCreation::class, fn ($job) => $job->template === 'image_card');
+});
+
+it('rejects an unknown template', function () {
+ Bus::fake();
+
+ $this->actingAs($this->user)
+ ->postJson(route('app.posts.ai.create'), [
+ 'format' => 'instagram_feed',
+ 'prompt' => 'x',
+ 'template' => 'bogus',
+ ])
+ ->assertUnprocessable()
+ ->assertJsonValidationErrors(['template']);
+});
+
+it('requires social_account_id when the template needsAccount', function () {
+ Bus::fake();
+
+ $this->actingAs($this->user)
+ ->postJson(route('app.posts.ai.create'), [
+ 'format' => 'instagram_feed',
+ 'prompt' => 'a post about coffee',
+ 'template' => 'image_card',
+ ])
+ ->assertAccepted();
+});