From b001850b172dedd0730686fbef534670fe8132e5 Mon Sep 17 00:00:00 2001 From: Erik Perri Date: Wed, 22 Nov 2023 18:39:38 -0500 Subject: [PATCH] update: Added `hex_color` rule helper --- composer.json | 2 +- src/Rule.php | 11 +++++++++++ src/RuleSet.php | 11 +++++++++++ tests/Unit/RuleTest.php | 25 +++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a657257..cd6e0b4 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "minimum-stability": "stable", "require": { "php": "^8.1", - "laravel/framework": "^10.32" + "laravel/framework": "^10.33" }, "require-dev": { "ext-json": "*", diff --git a/src/Rule.php b/src/Rule.php index a4050c7..e4f3e2e 100644 --- a/src/Rule.php +++ b/src/Rule.php @@ -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). * diff --git a/src/RuleSet.php b/src/RuleSet.php index 14ce124..5d99853 100644 --- a/src/RuleSet.php +++ b/src/RuleSet.php @@ -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). * diff --git a/tests/Unit/RuleTest.php b/tests/Unit/RuleTest.php index fdae608..9eda937 100644 --- a/tests/Unit/RuleTest.php +++ b/tests/Unit/RuleTest.php @@ -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(),