Skip to content

Commit

Permalink
update: Added hex_color rule helper
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-perri committed Nov 22, 2023
1 parent 86061cc commit b001850
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"minimum-stability": "stable",
"require": {
"php": "^8.1",
"laravel/framework": "^10.32"
"laravel/framework": "^10.33"
},
"require-dev": {
"ext-json": "*",
Expand Down
11 changes: 11 additions & 0 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,17 @@ public static function gte(BigNumber|int|float|string $field): string
return sprintf('gte:%s', $field);
}

/**
* The field under validation must contain a valid color value in hexadecimal format.
*
* @link https://laravel.com/docs/10.x/validation#rule-hex-color
* @link https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color
*/
public static function hexColor(): string
{
return 'hex_color';
}

/**
* The file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp).
*
Expand Down
11 changes: 11 additions & 0 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,17 @@ public function gte(BigNumber|int|float|string $field): self
return $this->rule(Rule::gte($field));
}

/**
* The field under validation must contain a valid color value in hexadecimal format.
*
* @link https://laravel.com/docs/10.x/validation#rule-hex-color
* @link https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color
*/
public function hexColor(): self
{
return $this->rule(Rule::hexColor());
}

/**
* The file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp).
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,31 @@ public function ruleDataProvider(): array
],
'fails' => true,
],
'hex color valid short' => [
'data' => fn() => '#fff',
'rules' => fn() => RuleSet::create()->hexColor(),
'fails' => false,
],
'hex color valid short with alpha' => [
'data' => fn() => '#FFFA',
'rules' => fn() => RuleSet::create()->hexColor(),
'fails' => false,
],
'hex color valid' => [
'data' => fn() => '#ffeedd',
'rules' => fn() => RuleSet::create()->hexColor(),
'fails' => false,
],
'hex color valid with alpha' => [
'data' => fn() => '#FFEEDDAA',
'rules' => fn() => RuleSet::create()->hexColor(),
'fails' => false,
],
'hex color invalid' => [
'data' => fn() => 'what',
'rules' => fn() => RuleSet::create()->hexColor(),
'fails' => true,
],
'image valid' => [
'data' => fn() => $this->mockFile('/code/image.jpg'),
'rules' => fn() => RuleSet::create()->image(),
Expand Down

0 comments on commit b001850

Please sign in to comment.