Skip to content

Commit 6a55dc1

Browse files
committed
Add rule Jwt
1 parent 59b3cfb commit 6a55dc1

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

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+
}

0 commit comments

Comments
 (0)