Skip to content
This repository was archived by the owner on Sep 13, 2024. It is now read-only.

Commit d1eca31

Browse files
authored
Merge pull request #3 from VladimirBerdnik/master
Add ability to use custom declared validation rules
2 parents f4f9d74 + a64d29e commit d1eca31

File tree

7 files changed

+90
-2
lines changed

7 files changed

+90
-2
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes History
22

3+
1.0.5
4+
-----
5+
Add ability to use custom declared validation rules
6+
37
1.0.4
48
-----
59
Add 'sometimes' rule modifier

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ $rules = [
2828

2929
![Intelisence](docs/intellisence.png)
3030

31+
**Custom validation rules**
32+
33+
You can also use [custom declared validations](https://laravel.com/docs/5.4/validation#custom-validation-rules):
34+
35+
```php
36+
$rules = [
37+
... => Rule::custom('foo')
38+
]
39+
```
40+
41+
3142
## Installation
3243

3344
Install the ```saritasa/laravel-fluent-validation``` package:

src/DatabaseRuleSet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct(array $rules = [])
1616
{
1717
if (!Config::get('validation.allow_db', false)) {
1818
throw new ConfigurationException("Validation against database is disabled. "
19-
."To to use validation rules, like 'exists', 'unique', etc. set configuration parameter "
19+
."To use validation rules, like 'exists', 'unique', etc. set configuration parameter "
2020
."'validation.allow_db' = true");
2121
}
2222

@@ -46,7 +46,7 @@ public function exists(string $table, string $column = 'NULL', \Closure $callbac
4646
*
4747
* @param string $table
4848
* @param string $column
49-
* @param \Closure|null $callback callback, that will receive \Illuminate\Validation\Rules\Exists $rule
49+
* @param \Closure|null $callback callback, that will receive \Illuminate\Validation\Rules\Unique $rule
5050
* @return GenericRuleSet
5151
*/
5252
public function unique(string $table, string $column = 'NULL', \Closure $callback = null): GenericRuleSet

src/Rule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @method static GenericRuleSet different(string $anotherFiled) The field under validation must have a different value than field.
2222
* @method static GenericRuleSet same(string $anotherFiled) The given field must match the field under validation.
2323
* @method static GenericRuleSet size(int $value) The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes.
24+
* @method static GenericRuleSet between($minimalValue, $maximalValue) The field under validation must have a size between the given min and max. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.
2425
* @method static GenericRuleSet min($minValue) The field under validation must have a minimum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.
2526
* @method static GenericRuleSet max($maxValue) The field under validation must be less than or equal to a maximum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.
2627
* @method static GenericRuleSet inArray(string $anotherField) The field under validation must exist in $anotherField's values.
@@ -31,6 +32,7 @@
3132
* @method static GenericRuleSet requiredWithoutAll(string ...$otherFields) The field under validation must be present and not empty only when all of the other specified fields are not present.
3233
* @method static GenericRuleSet requiredIf(string $anotherField, $value) The field under validation must be present and not empty if the $anotherField field is equal to any value.
3334
* @method static GenericRuleSet requiredUnless(string $anotherField, $value) The field under validation must be present and not empty unless the $anotherField field is equal to any value.
35+
* @method static GenericRuleSet custom(string $customRule) The field under validation must pass custom validation rule.
3436
3537
* @method static StringRuleSet activeUrl() The field under validation must have a valid A or AAAA record according to the dns_get_record PHP function.
3638
* @method static StringRuleSet alpha() The field under validation must be entirely alphabetic characters.

src/RuleSet.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class RuleSet implements IRule
5050
'between',
5151
'size',
5252
'sometimes',
53+
'custom',
5354
];
5455

5556
/** @var array */

src/SimpleRules.php

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

33
namespace Saritasa\Laravel\Validation;
44

5+
use Illuminate\Support\Facades\Config;
6+
use Saritasa\Exceptions\ConfigurationException;
7+
58
/**
69
* Just breakdown of RuleSet, containing too many methods for easy reading
710
*/
@@ -126,4 +129,23 @@ public function sometimes()
126129
{
127130
return $this->appendIfNotExists("sometimes");
128131
}
132+
133+
/**
134+
* The field under validation must pass custom validation rule.
135+
* @param string $customRule
136+
* @return GenericRuleSet
137+
* @throws ConfigurationException
138+
* @see https://laravel.com/docs/5.4/validation#custom-validation-rules
139+
* @see \Illuminate\Validation\Factory::extend
140+
*/
141+
public function custom(string $customRule): GenericRuleSet
142+
{
143+
if (!Config::get('validation.allow_custom', false)) {
144+
throw new ConfigurationException("Custom validation rules are disabled. "
145+
. "To use custom validation rules, registered via Validator::extend, set configuration parameter "
146+
. "'validation.allow_custom' = true");
147+
}
148+
149+
return $this->appendIfNotExists($customRule);
150+
}
129151
}

tests/CustomRulesTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation\Tests;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use PHPUnit\Framework\TestCase;
7+
use Saritasa\Exceptions\ConfigurationException;
8+
use Saritasa\Laravel\Validation\Rule;
9+
10+
class CustomRulesTest extends TestCase
11+
{
12+
/**
13+
* Reset mocked configuration after each test
14+
*/
15+
protected function tearDown()
16+
{
17+
Config::clearResolvedInstances();
18+
}
19+
20+
/** Mock configuration value to allow custom validation rules
21+
* @param bool $allowed
22+
*/
23+
protected function allowCustomValidation(bool $allowed = true)
24+
{
25+
Config::shouldReceive('get')->with('validation.allow_custom', false)->andReturn($allowed);
26+
}
27+
28+
/**
29+
* Do not allow to use custom validation rules, unless it is set explicitly in configuration
30+
*/
31+
public function testConfigurationRequired()
32+
{
33+
$this->allowCustomValidation(false);
34+
$this->expectException(ConfigurationException::class);
35+
36+
Rule::custom('foo');
37+
}
38+
39+
/**
40+
* Basic form of Rule::custom('foo') works.
41+
*/
42+
public function testCustom()
43+
{
44+
$this->allowCustomValidation(true);
45+
46+
$this->assertEquals('foo', Rule::custom('foo'));
47+
}
48+
}

0 commit comments

Comments
 (0)