Skip to content

Commit

Permalink
fix(enums): make Enum rule accept pure enums when passing enum instan…
Browse files Browse the repository at this point in the history
  • Loading branch information
trckster authored and bert-w committed Nov 28, 2022
1 parent 561d7b5 commit ee65232
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Validation/Rules/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public function __construct($type)
*/
public function passes($attribute, $value)
{
if (is_null($value) || ! function_exists('enum_exists') || ! enum_exists($this->type) || ! method_exists($this->type, 'tryFrom')) {
return false;
}

if ($value instanceof $this->type) {
return true;
}

if (is_null($value) || ! function_exists('enum_exists') || ! enum_exists($this->type) || ! method_exists($this->type, 'tryFrom')) {
return false;
}

try {
return ! is_null($this->type::tryFrom($value));
} catch (TypeError $e) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Validation/ValidationEnumRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public function testvalidationPassesWhenPassingInstanceOfEnum()
$this->assertFalse($v->fails());
}

public function testvalidationPassesWhenPassingInstanceOfPureEnum()
{
$v = new Validator(
resolve('translator'),
[
'status' => PureEnum::one,
],
[
'status' => new Enum(PureEnum::class),
]
);

$this->assertFalse($v->fails());
}

public function testValidationFailsWhenProvidingNoExistingCases()
{
$v = new Validator(
Expand Down

0 comments on commit ee65232

Please sign in to comment.