Skip to content

Commit 6e39bb0

Browse files
authored
[ADD] new URI Rule to validate URI and use it to RedirectRule (#1544)
1 parent 499d68d commit 6e39bb0

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/Http/Rules/RedirectRule.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Validation\Factory;
66
use Illuminate\Contracts\Validation\Rule;
7+
use Laravel\Passport\Http\Rules\UriRule;
78

89
class RedirectRule implements Rule
910
{
@@ -31,7 +32,7 @@ public function __construct(Factory $validator)
3132
public function passes($attribute, $value)
3233
{
3334
foreach (explode(',', $value) as $redirect) {
34-
$validator = $this->validator->make(['redirect' => $redirect], ['redirect' => 'url']);
35+
$validator = $this->validator->make(['redirect' => $redirect], ['redirect' => new UriRule]);
3536

3637
if ($validator->fails()) {
3738
return false;
@@ -46,6 +47,6 @@ public function passes($attribute, $value)
4647
*/
4748
public function message()
4849
{
49-
return 'One or more redirects have an invalid url format.';
50+
return 'One or more redirects have an invalid URI format.';
5051
}
5152
}

src/Http/Rules/UriRule.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Laravel\Passport\Http\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
class UriRule implements Rule
8+
{
9+
/**
10+
* {@inheritdoc}
11+
*/
12+
public function passes($attribute, $value): bool
13+
{
14+
if (filter_var($value, FILTER_VALIDATE_URL)) {
15+
return true;
16+
}
17+
18+
return false;
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function message(): string
25+
{
26+
return 'The :attribute must be valid URI.';
27+
}
28+
}

0 commit comments

Comments
 (0)