user() !== null; } protected function prepareForValidation(): void { $this->merge([ 'base_url' => $this->normalizedBaseUrl(), 'provider' => Str::lower(trim((string) $this->input('provider'))), ]); } /** * Get the validation rules that apply to the request. * * @return array|string> */ public function rules(): array { /** @var UserGitConnection $gitConnection */ $gitConnection = $this->route('gitConnection'); return [ 'api_token' => ['required', 'string', 'max:2048'], 'base_url' => [ 'required', 'url:http,https', 'max:255', Rule::unique('user_git_connections') ->ignore($gitConnection->id) ->where(fn ($query) => $query ->where('user_id', $this->user()->id) ->where('provider', $this->input('provider'))), ], 'provider' => ['required', Rule::in(array_map( static fn (GitProviderKey $providerKey): string => $providerKey->value, GitProviderKey::cases(), ))], ]; } private function normalizedBaseUrl(): string { $baseUrl = Str::of((string) $this->input('base_url')) ->trim() ->rtrim('/') ->value(); return preg_replace('#/api/v1$#i', '', $baseUrl) ?? $baseUrl; } }