Skip to content

Commit 5b762f0

Browse files
authored
Merge pull request #8 from ash-jc-allen/fix/locale-validation-message
Fix bug that wasn't reading the `Locale` enum value correctly
2 parents d0d2e39 + 8212b96 commit 5b762f0

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/Rules/Clean.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
2828
if (Str::contains(Str::lower(Str::remove($tolerated, $value)), $profanities)) {
2929
$fail(trans('message'))->translate([
3030
'attribute' => $attribute,
31-
], $locale);
31+
], $this->getLocaleValue($locale));
3232
}
3333
}
3434
}
@@ -51,15 +51,21 @@ protected function ensureLocalesAreValid(array $locales): void
5151
}
5252

5353
/**
54-
* Get the name of the config file for the given locale. If a Locale enum is
55-
* provided, then use the underlying value.
54+
* Get the locale value expressed as a string.
5655
*/
57-
protected function configFileName(string|Locale $locale): string
56+
protected function getLocaleValue(string|Locale $locale): string
5857
{
59-
$locale = $locale instanceof Locale
58+
return $locale instanceof Locale
6059
? $locale->value
6160
: $locale;
61+
}
6262

63-
return 'profanify-' . $locale;
63+
/**
64+
* Get the name of the config file for the given locale. If a Locale enum is
65+
* provided, then use the underlying value.
66+
*/
67+
protected function configFileName(string|Locale $locale): string
68+
{
69+
return 'profanify-' . $this->getLocaleValue($locale);
6470
}
6571
}

tests/CleanTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
it('fails', function ($word) {
1616
$v = new Validator($this->translator, ['name' => $word], ['name' => new Clean]);
1717

18-
expect($v->fails())->toBeTrue();
18+
expect($v->fails())->toBeTrue()
19+
->and($v->errors()->all())->toBe(['The name field is not clean']);
1920
})->with([
2021
'fuck',
2122
'shit',
@@ -53,6 +54,13 @@
5354
expect($v->passes())->toBeTrue();
5455
})->with(Locale::cases());
5556

57+
it('fails when using enums', function () {
58+
$v = new Validator($this->translator, ['name' => 'fuck'], ['name' => new Clean([Locale::English])]);
59+
60+
expect($v->fails())->toBeTrue()
61+
->and($v->errors()->all())->toBe(['The name field is not clean']);
62+
});
63+
5664
it('throws an exception if one of the locales is not a string or enum', function () {
5765
(new Validator(
5866
$this->translator,

0 commit comments

Comments
 (0)