Skip to content

Commit 254c331

Browse files
Modify readme.md add new validation methods
1 parent 1f6dd20 commit 254c331

File tree

6 files changed

+128
-54
lines changed

6 files changed

+128
-54
lines changed

README.md

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,65 @@ return [
1515
];
1616
```
1717

18-
## Artisan Command List
18+
## Build rules
1919

20-
<!-- List Of Command -->
2120
<div>
2221
<ol>
2322
<li><a href="#create-rule">Build rule</a></li>
2423
</ol>
2524
</div>
2625

27-
```
28-
<?php
29-
30-
namespace App\Http\Requests;
31-
32-
use Illuminate\Foundation\Http\FormRequest;
33-
use Shergela\LaravelValidationRule\Rule;
34-
35-
class StorePostRequest extends FormRequest
36-
{
37-
/**
38-
* @return bool
39-
*/
40-
public function authorize(): bool
41-
{
42-
return true;
43-
}
44-
45-
/**
46-
* @return array<string, Rule|array<string>>
47-
*/
48-
public function rules(): array
49-
{
50-
return [
51-
'name' => [Rule::required()->string()->min(3)],
52-
];
53-
}
54-
}
55-
56-
57-
```
26+
<div id="create-rule">
27+
28+
| **<span style="color: red;">Methods</span>** | **<span style="color: red;">Laravel Rule</span>** |
29+
|:-------------------------------------------------------------:|:---------------------------------------------------------:|
30+
| <span style="color: #CA473F">Rule::required()</span> | <span style="color: #CA473A">required</span> |
31+
| <span style="color: #CA473F">Rule::nullable()</span> | <span style="color: #CA473A">nullable</span> |
32+
| <span style="color: #CA473F">Rule::boolean()</span> | <span style="color: #CA473A">boolean</span> |
33+
| <span style="color: #CA473F">Rule::rules()</span> | <span style="color: #CA473A">custom rules</span> |
34+
| <span style="color: #CA473F">->email()</span> | <span style="color: #CA473A">email</span> |
35+
| <span style="color: #CA473F">->uniqueEmail()</span> | <span style="color: #CA473A">unique:users,email</span> |
36+
| **<span style="color: red">Integer Rules</span>** | **<span style="color: red">Integer Rules</span>** |
37+
| <span style="color: #CA473F">->min()</span> | <span style="color: #CA473A">min</span> |
38+
| <span style="color: #CA473F">->max()</span> | <span style="color: #CA473A">max</span> |
39+
| <span style="color: #CA473F">->minDigits()</span> | <span style="color: #CA473A">min_digits</span> |
40+
| <span style="color: #CA473F">->maxDigits()</span> | <span style="color: #CA473A">max_digits</span> |
41+
| <span style="color: #CA473F">->integer()</span> | <span style="color: #CA473A">integer</span> |
42+
| <span style="color: #CA473F">->numeric()</span> | <span style="color: #CA473A">numeric</span> |
43+
| <span style="color: #CA473F">->digits()</span> | <span style="color: #CA473A">digits:1</span> |
44+
| <span style="color: #CA473F">->digitsBetween()</span> | <span style="color: #CA473A">digits:1,4</span> |
45+
| <span style="color: #CA473F">->decimal()</span> | <span style="color: #CA473A">decimal</span> |
46+
| **<span style="color: red">String Rules</span>** | **<span style="color: red">String Rules</span>** |
47+
| <span style="color: #CA473F">->alpha()</span> | <span style="color: #CA473A">alpha</span> |
48+
| <span style="color: #CA473F">->alphaDash()</span> | <span style="color: #CA473A">alpha_dash</span> |
49+
| <span style="color: #CA473F">->alphaNum()</span> | <span style="color: #CA473A">alpha_num</span> |
50+
| <span style="color: #CA473F">->string()</span> | <span style="color: #CA473A">string</span> |
51+
| <span style="color: #CA473A">->uppercase()</span> | <span style="color: #CA473A">uppercase</span> |
52+
| <span style="color: #CA473A">->lowercase()</span> | <span style="color: #CA473A">lowercase</span> |
53+
| <span style="color: #CA473A">->regex()</span> | <span style="color: #CA473A">regex:pattern</span> |
54+
| <span style="color: #CA473A">->hexColor()</span> | <span style="color: #CA473A">hex_color</span> |
55+
| <span style="color: #CA473A">->hexColor()</span> | <span style="color: #CA473A">hex_color</span> |
56+
| <span style="color: #CA473A">->json()</span> | <span style="color: #CA473A">json</span> |
57+
| <span style="color: #CA473A">->url()</span> | <span style="color: #CA473A">url or url:http,https</span> |
58+
| <span style="color: #CA473A">->uuid()</span> | <span style="color: #CA473A">uuid</span> |
59+
| <span style="color: #CA473A">->ulid()</span> | <span style="color: #CA473A">ulid</span> |
60+
| **<span style="color: red">Date Rules</span>** | **<span style="color: red">Date Rules</span>** |
61+
| <span style="color: #CA473A">->timezone()</span> | <span style="color: #CA473A">timezone</span> |
62+
| <span style="color: #CA473F">->date()</span> | <span style="color: #CA473A">date</span> |
63+
| <span style="color: #CA473F">->dateFormat()</span> | <span style="color: #CA473A">date_format</span> |
64+
| <span style="color: #CA473F">->dateEquals()</span> | <span style="color: #CA473A">date_equals</span> |
65+
| <span style="color: #CA473F">->dateBefore()</span> | <span style="color: #CA473A">before</span> |
66+
| <span style="color: #CA473F">->dateBeforeOrEqual()</span> | <span style="color: #CA473A">before_or_equal</span> |
67+
| <span style="color: #CA473F">->dateAfter()</span> | <span style="color: #CA473A">after</span> |
68+
| <span style="color: #CA473F">->dateAfterOrEqualToday()</span> | <span style="color: #CA473A">after_or_equal:today</span> |
69+
| <span style="color: #CA473F">->dateAfterOrEquals()</span> | <span style="color: #CA473A">after_or_equal</span> |
70+
| **<span style="color: red">IP Mac Address Rule</span>** | **<span style="color: red">IP Mac Address Rule</span>** |
71+
| <span style="color: #CA473A">->ip()</span> | <span style="color: #CA473A">ip</span> |
72+
| <span style="color: #CA473A">->ipv4()</span> | <span style="color: #CA473A">ipv4</span> |
73+
| <span style="color: #CA473A">->ipv6()</span> | <span style="color: #CA473A">ipv6</span> |
74+
| <span style="color: #CA473A">->macAddress()</span> | <span style="color: #CA473A">mac_address</span> |
5875

76+
</div>
5977

6078

6179
# License

composer.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
"sort-packages": true
3333
},
3434
"extra": {
35-
"branch-alias": {
36-
"dev-main": "1.x"
37-
},
3835
"laravel": {
3936
"providers": [
4037
"Shergela\\Validations\\ShergelaValidationsServiceProvider"

src/Enums/ValidationRuleEnum.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ enum ValidationRuleEnum: string
3030
* The field under validation must contain a valid color value in hexadecimal format.
3131
*/
3232
public const HEX_COLOR = 'hex_color';
33+
34+
/**
35+
* The field under validation must be a valid RFC 4122 (version 1, 3, 4, or 5) universally unique identifier (UUID).
36+
*/
37+
public const UUID = 'uuid';
38+
39+
/**
40+
* The field under validation must be a valid Universally Unique Lexicographically Sortable Identifier (ULID).
41+
*/
42+
public const ULID = 'ulid';
3343
}

src/Enums/ValidationStringEnum.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ enum ValidationStringEnum: string
3838
* The field under validation must be a valid JSON string.
3939
*/
4040
public const JSON = 'json';
41+
42+
/**
43+
* The field under validation must be a valid URL.
44+
*/
45+
public const URL = 'url';
4146
}

src/Validation/BuildValidationRule.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ class BuildValidationRule
166166
*/
167167
protected bool $hexColor = false;
168168

169+
protected ?string $url = null;
170+
171+
/**
172+
* @var bool
173+
* The field under validation must be a valid Universally Unique Lexicographically Sortable Identifier (ULID).
174+
*/
175+
protected bool $ulid = false;
176+
177+
/**
178+
* @var bool
179+
* The field under validation must be a valid RFC 4122 (version 1, 3, 4, or 5) universally unique identifier (UUID).
180+
*/
181+
protected bool $uuid = false;
182+
169183
/**
170184
* @var bool
171185
*/
@@ -221,6 +235,8 @@ protected function buildValidationRules(): array
221235
...($this->email === true ? [Rule::EMAIL] : []),
222236
...($this->uniqueEmail !== null ? [$this->uniqueEmail] : []),
223237
...($this->hexColor === true ? [Rule::HEX_COLOR] : []),
238+
...($this->uuid === true ? [Rule::UUID] : []),
239+
...($this->ulid === true ? [Rule::ULID] : []),
224240

225241
/**
226242
* IP | MAC address validations
@@ -257,6 +273,7 @@ protected function buildValidationRules(): array
257273
...($this->upperCase === true ? [StringRule::UPPERCASE] : []),
258274
...($this->lowerCase === true ? [StringRule::LOWERCASE] : []),
259275
...($this->json === true ? [StringRule::JSON] : []),
276+
...($this->url !== null ? [$this->url] : []),
260277

261278
/**
262279
* --------------------------------------------------------------------------------

src/Validation/Rule.php

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Shergela\Validations\Validation;
44

55
use Closure;
6+
use Dotenv\Util\Str;
67
use Illuminate\Contracts\Validation\DataAwareRule;
78
use Illuminate\Contracts\Validation\ValidationRule;
89
use Illuminate\Contracts\Validation\ValidatorAwareRule;
@@ -43,7 +44,7 @@ class Rule extends BuildValidationRule implements ValidationRule, ValidatorAware
4344
* Additional validation rules that should be merged into the default rules during validation.
4445
* @var array<string>
4546
*/
46-
protected array $customRules = [];
47+
protected static array $customRules = [];
4748

4849
/**
4950
* @param Validator $validator
@@ -241,6 +242,43 @@ public function string(): static
241242
return $this;
242243
}
243244

245+
/**
246+
* @param string|null $protocol
247+
* @return $this
248+
*/
249+
public function url(string $protocol = null): static
250+
{
251+
if ($protocol !== null) {
252+
$this->url = StringRule::URL . ':' . $protocol;
253+
254+
return $this;
255+
}
256+
257+
$this->url = StringRule::URL;
258+
259+
return $this;
260+
}
261+
262+
/**
263+
* @return $this
264+
*/
265+
public function uuid(): static
266+
{
267+
$this->uuid = true;
268+
269+
return $this;
270+
}
271+
272+
/**
273+
* @return $this
274+
*/
275+
public function ulid(): static
276+
{
277+
$this->ulid = true;
278+
279+
return $this;
280+
}
281+
244282
/**
245283
* @return $this
246284
*/
@@ -384,17 +422,6 @@ public function decimal(int $first, int $second = null): static
384422
return $this;
385423
}
386424

387-
/**
388-
* @param string $timezone
389-
* @return $this
390-
*/
391-
public function timezone(string $timezone): static
392-
{
393-
$this->timezone = $timezone;
394-
395-
return $this;
396-
}
397-
398425
/**
399426
* @return $this
400427
*/
@@ -512,25 +539,25 @@ public function maxDigits(int $maxDigits): static
512539
/**
513540
* Specify additional validation rules that should be merged with the default rules during validation.
514541
* @param array<string>|string $rules
515-
* @return $this
542+
* @return Rule
516543
*/
517-
public function rules(array|string $rules): static
544+
public static function rules(array|string $rules): Rule
518545
{
519-
$this->customRules = Arr::wrap($rules);
546+
static::$customRules = Arr::wrap($rules);
520547

521-
return $this;
548+
return new self();
522549
}
523550

524551
/**
525552
* @return array<string>
526553
*/
527554
private function getValidationRules(): array
528555
{
529-
if (empty($this->customRules)) {
556+
if (empty(static::$customRules)) {
530557
return $this->buildValidationRules();
531558
}
532559

533-
return array_merge($this->customRules, $this->buildValidationRules());
560+
return array_merge(static::$customRules, $this->buildValidationRules());
534561
}
535562

536563
/**

0 commit comments

Comments
 (0)