Skip to content

Commit 74dedef

Browse files
authored
Merge pull request #13 from arifszn/feat/rule-jwt
feat: rule `jwt`
2 parents 59b3cfb + 0e9e6e5 commit 74dedef

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ php artisan vendor:publish --provider="Arifszn\AdvancedValidation\ServiceProvide
3030
You can specify the error message on the fly when declaring the rules. Simple pass the error message parameter.
3131

3232
```php
33-
use Arifszn\AdvancedValidation\Rules\Base64Image;
33+
use Arifszn\AdvancedValidation\Rules\Username;
3434

3535
public function rules()
3636
{
3737
return [
38-
'avatar' => [new Base64Image('Your custom error message')],
38+
'foo' => [new Username('Your custom error message')],
3939
];
4040
}
4141
```
@@ -54,6 +54,7 @@ public function rules()
5454
- [`Float Number`](#floatnumber)
5555
- [`Hash`](#hash)
5656
- [`Image URL`](#imageurl)
57+
- [`JWT`](#jwt)
5758
- [`Name`](#name)
5859
- [`Phone`](#phone)
5960
- [`Username`](#username)
@@ -292,6 +293,25 @@ public function rules()
292293
}
293294
```
294295

296+
### `JWT`
297+
298+
The field under validation must have a valid format of JWT ([JSON Web Token](https://en.wikipedia.org/wiki/JSON_Web_Token)).
299+
300+
```
301+
public Arifszn\AdvancedValidation\Rules\Jwt::__construct(string $errorMessage = null)
302+
```
303+
304+
```php
305+
use Arifszn\AdvancedValidation\Rules\Jwt;
306+
307+
public function rules()
308+
{
309+
return [
310+
'foo' => [new Jwt()],
311+
];
312+
}
313+
```
314+
295315
### `Name`
296316

297317
The field under validation must be a valid name.

resources/lang/en/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'float_number' => 'The :attribute must be a float number.',
1414
'hash' => 'The :attribute must be a hash of :algorithm algorithm.',
1515
'image_url' => 'The :attribute must be a valid image URL.',
16+
'jwt' => 'The :attribute must have a valid format of JWT.',
1617
'name' => 'The :attribute must be a valid name.',
1718
'phone' => 'The :attribute must be a valid phone number.',
1819
'username' => 'The :attribute must be a valid username.',

src/Rules/Jwt.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Arifszn\AdvancedValidation\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
/**
8+
* The field under validation must have a valid format of JWT.
9+
*
10+
* @package Arifszn\AdvancedValidation\Rules
11+
*/
12+
class Jwt implements Rule
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $errorMessage;
18+
19+
/**
20+
* Create a new rule instance.
21+
*
22+
* @param string|null $errorMessage Custom error message.
23+
* @return void
24+
*/
25+
public function __construct(string $errorMessage = null)
26+
{
27+
$this->errorMessage = $errorMessage;
28+
}
29+
30+
/**
31+
* Determine if the validation rule passes.
32+
*
33+
* @param string $attribute
34+
* @param mixed $value
35+
* @return bool
36+
*/
37+
public function passes($attribute, $value)
38+
{
39+
return preg_match('/^[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+$/', $value);
40+
}
41+
42+
/**
43+
* Get the validation error message.
44+
*
45+
* @return string
46+
*/
47+
public function message()
48+
{
49+
return $this->errorMessage ? $this->errorMessage : trans('advancedValidation::validation.jwt');
50+
}
51+
}

tests/Rules/JwtTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Arifszn\AdvancedValidation\Tests\Rules;
4+
5+
use Arifszn\AdvancedValidation\Rules\Jwt;
6+
use Arifszn\AdvancedValidation\Tests\TestCase;
7+
8+
class JwtTest extends TestCase
9+
{
10+
/**
11+
* @dataProvider provider
12+
*/
13+
public function testValidation($result, $value)
14+
{
15+
$this->assertEquals($result, (new Jwt())->passes('foo', $value));
16+
}
17+
18+
public function provider()
19+
{
20+
return [
21+
[true, 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI'],
22+
[true, 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb3JlbSI6Imlwc3VtIn0.ymiJSsMJXR6tMSr8G9usjQ15_8hKPDv_CArLhxw28MI'],
23+
[true, 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb2xvciI6InNpdCIsImFtZXQiOlsibG9yZW0iLCJpcHN1bSJdfQ.rRpe04zbWbbJjwM43VnHzAboDzszJtGrNsUxaqQ-GQ8'],
24+
[true, 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqb2huIjp7ImFnZSI6MjUsImhlaWdodCI6MTg1fSwiamFrZSI6eyJhZ2UiOjMwLCJoZWlnaHQiOjI3MH19.YRLPARDmhGMC3BBk_OhtwwK21PIkVCqQe8ncIRPKo-E'],
25+
26+
[false, 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'],
27+
[false, '$Zs.ewu.su84'],
28+
[false, 'ks64$S/9.dy$§kz.3sd73b'],
29+
[false, ''],
30+
[false, ' '],
31+
];
32+
}
33+
}

0 commit comments

Comments
 (0)