Skip to content

Commit

Permalink
fixes #3612
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 6, 2024
1 parent eb1063e commit b0a3473
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
20 changes: 17 additions & 3 deletions framework/core/src/Foundation/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Flarum\Locale\TranslatorInterface;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Validation\Factory;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Validator;
Expand Down Expand Up @@ -84,9 +86,21 @@ protected function getActiveRules(array $attributes): array
{
$rules = $this->getRules();

return $this->validateMissingKeys
? $rules
: Arr::only($rules, array_keys($attributes));
if ($this->validateMissingKeys) {
return $rules;
}

return Collection::make($rules)
->filter(function (mixed $rule, string $key) use ($attributes) {
foreach ($attributes as $attributeKey => $attributeValue) {
if ($attributeKey === $key || Str::startsWith($key, $attributeKey . '.')) {
return true;
}
}

return false;
})
->all();
}

protected function getMessages(): array
Expand Down
59 changes: 57 additions & 2 deletions framework/core/tests/integration/extenders/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class ValidatorTest extends TestCase
{
private function extendToRequireLongPassword()
private function extendToRequireLongPassword(): void
{
$this->extend((new Extend\Validator(CustomUserValidator::class))->configure(function ($flarumValidator, $validator) {
$validator->setRules([
Expand All @@ -30,7 +30,7 @@ private function extendToRequireLongPassword()
}));
}

private function extendToRequireLongPasswordViaInvokableClass()
private function extendToRequireLongPasswordViaInvokableClass(): void
{
$this->extend((new Extend\Validator(CustomUserValidator::class))->configure(CustomValidatorClass::class));
}
Expand Down Expand Up @@ -74,6 +74,51 @@ public function custom_validation_rule_doesnt_affect_other_validators()
// If we have gotten this far, no validation exception has been thrown, so the test is successful.
$this->assertTrue(true);
}

#[Test]
public function validator_only_validates_provided_data_by_default()
{
/** @var SecondCustomValidator $validator */
$validator = $this->app()->getContainer()->make(SecondCustomValidator::class);

$validator->assertValid([
'my_key' => 'value',
]);

// If we have gotten this far, no validation exception has been thrown, so the test is successful.
$this->assertTrue(true);
}

#[Test]
public function validator_includes_path_based_rules()
{
/** @var SecondCustomValidator $validator */
$validator = $this->app()->getContainer()->make(SecondCustomValidator::class);

$this->expectException(ValidationException::class);

$validator->assertValid([
'my_key' => 'value',
'my_third_key' => [null],
]);
}

#[Test]
public function validator_can_validate_missing_keys()
{
/** @var SecondCustomValidator $validator */
$validator = $this->app()->getContainer()->make(SecondCustomValidator::class)->validateMissingKeys();

$this->expectException(ValidationException::class);

$validator->validateMissingKeys()->assertValid([
'my_key' => 'value',
'my_third_key' => [
'2021-01-01 00:00:00',
'2021-01-02 00:00:00'
]
]);
}
}

class CustomValidatorClass
Expand Down Expand Up @@ -142,3 +187,13 @@ class CustomValidator extends AbstractValidator
'name_plural' => ['required']
];
}

class SecondCustomValidator extends AbstractValidator
{
protected array $rules = [
'my_key' => ['required'],
'my_other_key' => ['required'],
'my_third_key' => ['required', 'array'],
'my_third_key.*' => ['required', 'date']
];
}

0 comments on commit b0a3473

Please sign in to comment.