Skip to content

Commit 9e7e38d

Browse files
add new methods and support writing a custom message
1 parent 800d5be commit 9e7e38d

13 files changed

+566
-127
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to `shergela/validation-rule` will be documented in this file.
44

5+
## 1.0.4 | 26 August - 2024
6+
7+
- add new `length()` method.
8+
- add a custom message support in the methods.
9+
510
## 1.0.3 | 23 August - 2024
611

712
## Modified Rule::in() and Rule::notIn() rules.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ return [
2121
<ol>
2222
<li><a href="#create-rule">Build rule</a></li>
2323
<li><a href="#writing-messages">Build Messages</a></li>
24+
<li><a href="#writing-custom-message">Writing a custom Messages into method</a></li>
2425
</ol>
2526
</div>
2627

@@ -93,6 +94,7 @@ return [
9394
| ->timezoneAustralia() | new TimezoneRegionValidation() |
9495
| ->timezoneIndian() | new TimezoneRegionValidation() |
9596
| ->timezonePacific() | new TimezoneRegionValidation() |
97+
| ->length() | size |
9698
</div>
9799

98100

@@ -135,6 +137,37 @@ public function authorize(): bool
135137

136138
</div>
137139

140+
## Writing a custom message in the methods
141+
<div id="writing-custom-message">
142+
143+
```php
144+
145+
namespace App\Http\Requests;
146+
147+
use Illuminate\Foundation\Http\FormRequest;
148+
use Shergela\Validations\Validation\Rule;
149+
150+
class TestRequest extends FormRequest
151+
{
152+
/**
153+
* @return bool
154+
*/
155+
public function authorize(): bool
156+
{
157+
return true;
158+
}
159+
160+
public function rules(): array
161+
{
162+
return [
163+
'name' => Rule::required(message: 'Please enter your name')
164+
->min(min: 3, message: 'Please enter at least 3 characters'),
165+
];
166+
}
167+
}
168+
```
169+
</div>
170+
138171

139172
# License
140173

src/DataManipulation/InNotInRuleManipulation.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ class InNotInRuleManipulation
1111
/**
1212
* @param Arrayable<int, string>|array<string>|string $values
1313
*/
14-
public function __construct(protected readonly Arrayable|array|string $values)
15-
{
14+
public function __construct(
15+
protected readonly Arrayable|array|string $values,
16+
protected readonly bool $in = false,
17+
protected readonly bool $notIn = false,
18+
) {
1619
}
1720

1821
/**
@@ -21,6 +24,7 @@ public function __construct(protected readonly Arrayable|array|string $values)
2124
private function manipulation(): string
2225
{
2326
$values = $this->values;
27+
$rule = $this->in === true ? 'in:' : 'not_in:';
2428

2529
$values = array_map(function ($value) {
2630
$value = match (true) {
@@ -35,7 +39,7 @@ private function manipulation(): string
3539
return '"' . str_replace('"', '""', $v) . '"';
3640
}, (array) $values);
3741

38-
return implode(',', $values);
42+
return $rule . implode(',', $values);
3943
}
4044

4145
/**

src/Enums/ValidationDateEnum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ enum ValidationDateEnum: string
1717
public const DATE_EQUALS = 'date_equals:';
1818

1919
public const DATE_FORMAT = 'date_format:';
20-
public const TIMEZONE = 'timezone:';
20+
public const TIMEZONE = 'timezone';
2121
public const TIMEZONE_ALL = 'timezone:all';
2222
}

src/Rules/LowercaseFirstLetter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
class LowercaseFirstLetter implements ValidationRule
1010
{
11+
public function __construct(protected readonly ?string $message = null)
12+
{
13+
}
14+
1115
/**
1216
* @param string $attribute
1317
* @param mixed $value
@@ -24,7 +28,11 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
2428
}
2529

2630
if (Str::lcfirst($toString) !== $value) {
27-
$fail('The first character of :attribute must be lowercase letter.');
31+
$fail(
32+
$this->message !== null
33+
? $this->message
34+
: 'The first character of :attribute must be lowercase letter.'
35+
);
2836
}
2937
}
3038
}

src/Rules/SeparateIntegersByComma.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
class SeparateIntegersByComma implements ValidationRule
1010
{
11+
public function __construct(protected readonly ?string $message = null)
12+
{
13+
}
14+
1115
/**
1216
* @param string $attribute
1317
* @param mixed $value
@@ -20,7 +24,11 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
2024
$toString = $value;
2125

2226
if (! preg_match(pattern: Regex::SEPARATE_INTEGERS_BY_COMMA, subject: $toString)) {
23-
$fail("Please separate (:attribute) integer values by comma. Entered value: {$toString}");
27+
$fail(
28+
$this->message == null
29+
? "Please separate (:attribute) integer values by comma. Entered value: {$toString}"
30+
: $this->message
31+
);
2432
}
2533
}
2634
}

src/Rules/SeparateStringsByComma.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
class SeparateStringsByComma implements ValidationRule
1010
{
11+
public function __construct(protected readonly ?string $message = null)
12+
{
13+
}
14+
1115
/**
1216
* @param string $attribute
1317
* @param mixed $value
@@ -20,7 +24,11 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
2024
$toString = $value;
2125

2226
if (! preg_match(pattern: Regex::SEPARATE_STRINGS_BY_COMMA, subject: $toString)) {
23-
$fail("Please separate (:attribute) values by comma. Entered value: {$toString}");
27+
$fail(
28+
$this->message == null
29+
? "Please separate (:attribute) values by comma. Entered value: {$toString}"
30+
: $this->message
31+
);
2432
}
2533
}
2634
}

src/Rules/SeparateStringsByUnderscore.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
class SeparateStringsByUnderscore implements ValidationRule
1010
{
11+
public function __construct(protected readonly ?string $message = null)
12+
{
13+
}
14+
1115
/**
1216
* @param string $attribute
1317
* @param mixed $value
@@ -20,7 +24,11 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
2024
$toString = $value;
2125

2226
if (! preg_match(pattern: Regex::SEPARATE_STRINGS_BY_UNDERSCORE, subject: $toString)) {
23-
$fail("Please separate (:attribute) values by underscore. Entered value: {$toString}");
27+
$fail(
28+
$this->message == null
29+
? "Please separate (:attribute) values by underscore. Entered value: {$toString}"
30+
: $this->message
31+
);
2432
}
2533
}
2634
}

src/Rules/TimezoneRegionValidation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function __construct(
1818
protected readonly array $cities,
1919
protected readonly int $timezoneGroupNumber,
2020
protected readonly string $timezoneGroup,
21+
protected readonly string|null $customMessage,
2122
) {
2223
}
2324

@@ -40,7 +41,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
4041
* Validate provided cities
4142
*/
4243
if ($this->validateProvidedValues() === false) {
43-
$fail($this->message);
44+
$fail($this->customMessage == null ? $this->message : $this->customMessage);
4445
return;
4546
};
4647

src/Rules/TimezoneValidation.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use Closure;
66
use Illuminate\Contracts\Validation\ValidationRule;
7+
use Illuminate\Support\Str;
78

89
class TimezoneValidation implements ValidationRule
910
{
1011
/**
1112
* @param array<string> $timezones
1213
*/
13-
public function __construct(protected readonly array $timezones)
14+
public function __construct(protected readonly array $timezones, protected readonly ?string $message = null)
1415
{
1516
}
1617

@@ -33,10 +34,14 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
3334

3435
if (! in_array($toString, $timezones)) {
3536
$implode = implode(', ', $this->timezones);
36-
$fail("
37-
The [:attribute] value does not match the given timezones list: [{$implode}].
38-
Please provide correct timezone.
39-
");
37+
if ($this->message !== null) {
38+
$fail($this->message);
39+
} else {
40+
$fail(sprintf("
41+
The :attribute value (%s) does not match the given timezones list: [%s].
42+
Please provide correct timezone.
43+
", $toString, $implode));
44+
}
4045
}
4146
}
4247
}

0 commit comments

Comments
 (0)