Skip to content

Commit 90da947

Browse files
authored
Merge pull request #36 from sourcetoad/add-present-and-hex-color-rules
Add `present_*` and `hex_color` rule helpers
2 parents 98e487f + 1f0d999 commit 90da947

File tree

5 files changed

+237
-2
lines changed

5 files changed

+237
-2
lines changed

UPGRADE.md

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

33
## v4
44

5+
### Upgrading from v4.0 to v4.1
6+
7+
- Minimum Laravel version increased from `10.14` to `10.33`.
8+
59
### Upgrading from v3.2 to v4.0
610

711
- Minimum Laravel version increased from `9.50.2` to `10.14`.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"minimum-stability": "stable",
77
"require": {
88
"php": "^8.1",
9-
"laravel/framework": "^10.14"
9+
"laravel/framework": "^10.33"
1010
},
1111
"require-dev": {
1212
"ext-json": "*",

src/Rule.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,17 @@ public static function gte(BigNumber|int|float|string $field): string
515515
return sprintf('gte:%s', $field);
516516
}
517517

518+
/**
519+
* The field under validation must contain a valid color value in hexadecimal format.
520+
*
521+
* @link https://laravel.com/docs/10.x/validation#rule-hex-color
522+
* @link https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color
523+
*/
524+
public static function hexColor(): string
525+
{
526+
return 'hex_color';
527+
}
528+
518529
/**
519530
* The file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp).
520531
*
@@ -826,6 +837,42 @@ public static function present(): string
826837
return 'present';
827838
}
828839

840+
/**
841+
* The field under validation must be present but can be empty if *anotherField* under validation is equal to a
842+
* specified value.
843+
*/
844+
public static function presentIf(string $anotherField, string ...$value): string
845+
{
846+
return sprintf('present_if:%s,%s', $anotherField, implode(',', $value));
847+
}
848+
849+
/**
850+
* The field under validation must be present but can be empty unless the *anotherField* field is equal to any
851+
* *value*.
852+
*/
853+
public static function presentUnless(string $anotherField, string ...$value): string
854+
{
855+
return sprintf('present_unless:%s,%s', $anotherField, implode(',', $value));
856+
}
857+
858+
/**
859+
* The field under validation must be present but can be empty *only if* any of the other specified fields are
860+
* present and not empty.
861+
*/
862+
public static function presentWith(string ...$field): string
863+
{
864+
return 'present_with:'.implode(',', $field);
865+
}
866+
867+
/**
868+
* The field under validation must be present but can be empty *only if* all the other specified fields are present
869+
* and not empty.
870+
*/
871+
public static function presentWithAll(string ...$field): string
872+
{
873+
return 'present_with_all:'.implode(',', $field);
874+
}
875+
829876
/**
830877
* The field under validation must be empty or not present.
831878
*

src/RuleSet.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Brick\Math\BigNumber;
99
use DateTimeInterface;
1010
use Illuminate\Contracts\Support\Arrayable;
11-
use Illuminate\Validation\Rules\Can;
1211
use Illuminate\Validation\Rules\RequiredIf;
1312
use Illuminate\Validation\Rules\Password;
1413
use IteratorAggregate;
@@ -573,6 +572,17 @@ public function gte(BigNumber|int|float|string $field): self
573572
return $this->rule(Rule::gte($field));
574573
}
575574

575+
/**
576+
* The field under validation must contain a valid color value in hexadecimal format.
577+
*
578+
* @link https://laravel.com/docs/10.x/validation#rule-hex-color
579+
* @link https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color
580+
*/
581+
public function hexColor(): self
582+
{
583+
return $this->rule(Rule::hexColor());
584+
}
585+
576586
/**
577587
* The file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp).
578588
*
@@ -891,6 +901,42 @@ public function present(): self
891901
return $this->rule(Rule::present());
892902
}
893903

904+
/**
905+
* The field under validation must be present but can be empty if *anotherField* under validation is equal to a
906+
* specified value.
907+
*/
908+
public function presentIf(string $anotherField, string ...$value): self
909+
{
910+
return $this->rule(Rule::presentIf($anotherField, ...$value));
911+
}
912+
913+
/**
914+
* The field under validation must be present but can be empty unless the *anotherField* field is equal to any
915+
* *value*.
916+
*/
917+
public function presentUnless(string $anotherField, string ...$value): self
918+
{
919+
return $this->rule(Rule::presentUnless($anotherField, ...$value));
920+
}
921+
922+
/**
923+
* The field under validation must be present but can be empty *only if* any of the other specified fields are
924+
* present and not empty.
925+
*/
926+
public function presentWith(string ...$field): self
927+
{
928+
return $this->rule(Rule::presentWith(...$field));
929+
}
930+
931+
/**
932+
* The field under validation must be present but can be empty *only if* all the other specified fields are present
933+
* and not empty.
934+
*/
935+
public function presentWithAll(string ...$field): self
936+
{
937+
return $this->rule(Rule::presentWithAll(...$field));
938+
}
939+
894940
/**
895941
* The field under validation must be empty or not present.
896942
*

tests/Unit/RuleTest.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,31 @@ public function ruleDataProvider(): array
10781078
],
10791079
'fails' => true,
10801080
],
1081+
'hex color valid short' => [
1082+
'data' => fn() => '#fff',
1083+
'rules' => fn() => RuleSet::create()->hexColor(),
1084+
'fails' => false,
1085+
],
1086+
'hex color valid short with alpha' => [
1087+
'data' => fn() => '#FFFA',
1088+
'rules' => fn() => RuleSet::create()->hexColor(),
1089+
'fails' => false,
1090+
],
1091+
'hex color valid' => [
1092+
'data' => fn() => '#ffeedd',
1093+
'rules' => fn() => RuleSet::create()->hexColor(),
1094+
'fails' => false,
1095+
],
1096+
'hex color valid with alpha' => [
1097+
'data' => fn() => '#FFEEDDAA',
1098+
'rules' => fn() => RuleSet::create()->hexColor(),
1099+
'fails' => false,
1100+
],
1101+
'hex color invalid' => [
1102+
'data' => fn() => 'what',
1103+
'rules' => fn() => RuleSet::create()->hexColor(),
1104+
'fails' => true,
1105+
],
10811106
'image valid' => [
10821107
'data' => fn() => $this->mockFile('/code/image.jpg'),
10831108
'rules' => fn() => RuleSet::create()->image(),
@@ -1722,6 +1747,119 @@ public function ruleDataProvider(): array
17221747
],
17231748
'fails' => true,
17241749
],
1750+
'presentIf valid' => [
1751+
'data' => [
1752+
'field-b' => '',
1753+
],
1754+
'rules' => fn() => [
1755+
'field-a' => RuleSet::create()->presentIf('field-b', 'a'),
1756+
],
1757+
'fails' => false,
1758+
],
1759+
'presentIf valid with value' => [
1760+
'data' => [
1761+
'field-a' => '',
1762+
'field-b' => 'a',
1763+
],
1764+
'rules' => fn() => [
1765+
'field-a' => RuleSet::create()->presentIf('field-b', 'a'),
1766+
],
1767+
'fails' => false,
1768+
],
1769+
'presentIf invalid' => [
1770+
'data' => [
1771+
'field-b' => 'a',
1772+
],
1773+
'rules' => fn() => [
1774+
'field-a' => RuleSet::create()->presentIf('field-b', 'a'),
1775+
],
1776+
'fails' => true,
1777+
],
1778+
'presentUnless valid' => [
1779+
'data' => [
1780+
'field-a' => '',
1781+
],
1782+
'rules' => fn() => [
1783+
'field-a' => RuleSet::create()->presentUnless('field-b', 'a'),
1784+
],
1785+
'fails' => false,
1786+
],
1787+
'presentUnless valid with value' => [
1788+
'data' => [
1789+
'field-b' => 'a',
1790+
],
1791+
'rules' => fn() => [
1792+
'field-a' => RuleSet::create()->presentUnless('field-b', 'a'),
1793+
],
1794+
'fails' => false,
1795+
],
1796+
'presentUnless invalid' => [
1797+
'data' => [
1798+
'field-b' => 'b',
1799+
],
1800+
'rules' => fn() => [
1801+
'field-a' => RuleSet::create()->presentUnless('field-b', 'a'),
1802+
],
1803+
'fails' => true,
1804+
],
1805+
'presentWith valid' => [
1806+
'data' => [
1807+
'field-a' => '',
1808+
'field-b' => 'b',
1809+
],
1810+
'rules' => fn() => [
1811+
'field-a' => RuleSet::create()->presentWith('field-b'),
1812+
],
1813+
'fails' => false,
1814+
],
1815+
'presentWith valid without' => [
1816+
'data' => [
1817+
'field-c' => 'c',
1818+
],
1819+
'rules' => fn() => [
1820+
'field-a' => RuleSet::create()->presentWith('field-b'),
1821+
],
1822+
'fails' => false,
1823+
],
1824+
'presentWith invalid' => [
1825+
'data' => [
1826+
'field-b' => 'b',
1827+
],
1828+
'rules' => fn() => [
1829+
'field-a' => RuleSet::create()->presentWith('field-b'),
1830+
],
1831+
'fails' => true,
1832+
],
1833+
'presentWithAll valid' => [
1834+
'data' => [
1835+
'field-a' => '',
1836+
'field-b' => 'b',
1837+
'field-c' => 'c',
1838+
],
1839+
'rules' => fn() => [
1840+
'field-a' => RuleSet::create()->presentWithAll('field-b', 'field-c'),
1841+
],
1842+
'fails' => false,
1843+
],
1844+
'presentWithAll valid without' => [
1845+
'data' => [
1846+
'field-c' => 'c',
1847+
],
1848+
'rules' => fn() => [
1849+
'field-a' => RuleSet::create()->presentWithAll('field-b', 'field-c'),
1850+
],
1851+
'fails' => false,
1852+
],
1853+
'presentWithAll invalid' => [
1854+
'data' => [
1855+
'field-b' => 'b',
1856+
'field-c' => 'c',
1857+
],
1858+
'rules' => fn() => [
1859+
'field-a' => RuleSet::create()->presentWithAll('field-b', 'field-c'),
1860+
],
1861+
'fails' => true,
1862+
],
17251863
'prohibited valid' => [
17261864
'data' => [
17271865
'field-a' => '',

0 commit comments

Comments
 (0)