mirror of
https://github.com/flarum/core.git
synced 2025-08-06 08:27:42 +02:00
fix: improve the flarum validator (#4133)
This commit is contained in:
@@ -1,31 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of Flarum.
|
|
||||||
*
|
|
||||||
* For detailed copyright and license information, please view the
|
|
||||||
* LICENSE file that was distributed with this source code.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Flarum\ExtensionManager;
|
|
||||||
|
|
||||||
use Illuminate\Validation\Validator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @todo: fix in 2.0
|
|
||||||
*/
|
|
||||||
trait AllValidatorRules
|
|
||||||
{
|
|
||||||
protected function makeValidator(array $attributes): Validator
|
|
||||||
{
|
|
||||||
$rules = $this->getRules();
|
|
||||||
|
|
||||||
$validator = $this->validator->make($attributes, $rules, $this->getMessages());
|
|
||||||
|
|
||||||
foreach ($this->configuration as $callable) {
|
|
||||||
$callable($this, $validator);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $validator;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -13,7 +13,7 @@ use Flarum\Foundation\AbstractValidator;
|
|||||||
|
|
||||||
class ConfigureAuthValidator extends AbstractValidator
|
class ConfigureAuthValidator extends AbstractValidator
|
||||||
{
|
{
|
||||||
use AllValidatorRules;
|
protected bool $validateMissingKeys = true;
|
||||||
|
|
||||||
protected array $rules = [
|
protected array $rules = [
|
||||||
'github-oauth' => ['sometimes', 'array'],
|
'github-oauth' => ['sometimes', 'array'],
|
||||||
|
@@ -13,7 +13,7 @@ use Flarum\Foundation\AbstractValidator;
|
|||||||
|
|
||||||
class ConfigureComposerValidator extends AbstractValidator
|
class ConfigureComposerValidator extends AbstractValidator
|
||||||
{
|
{
|
||||||
use AllValidatorRules;
|
protected bool $validateMissingKeys = true;
|
||||||
|
|
||||||
protected array $rules = [
|
protected array $rules = [
|
||||||
'minimum-stability' => ['sometimes', 'in:stable,RC,beta,alpha,dev'],
|
'minimum-stability' => ['sometimes', 'in:stable,RC,beta,alpha,dev'],
|
||||||
|
@@ -10,7 +10,8 @@
|
|||||||
namespace Flarum\Foundation;
|
namespace Flarum\Foundation;
|
||||||
|
|
||||||
use Flarum\Locale\TranslatorInterface;
|
use Flarum\Locale\TranslatorInterface;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Validation\Factory;
|
use Illuminate\Validation\Factory;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
use Illuminate\Validation\Validator;
|
use Illuminate\Validation\Validator;
|
||||||
@@ -22,13 +23,12 @@ abstract class AbstractValidator
|
|||||||
*/
|
*/
|
||||||
protected array $configuration = [];
|
protected array $configuration = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected array $rules = [];
|
protected array $rules = [];
|
||||||
|
|
||||||
protected ?Validator $laravelValidator = null;
|
protected ?Validator $laravelValidator = null;
|
||||||
|
|
||||||
|
protected bool $validateMissingKeys = false;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected Factory $validator,
|
protected Factory $validator,
|
||||||
protected TranslatorInterface $translator
|
protected TranslatorInterface $translator
|
||||||
@@ -54,6 +54,16 @@ abstract class AbstractValidator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to validate missing keys or to only validate provided data keys.
|
||||||
|
*/
|
||||||
|
public function validateMissingKeys(bool $validateMissingKeys = true): static
|
||||||
|
{
|
||||||
|
$this->validateMissingKeys = $validateMissingKeys;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function prepare(array $attributes): static
|
public function prepare(array $attributes): static
|
||||||
{
|
{
|
||||||
$this->laravelValidator ??= $this->makeValidator($attributes);
|
$this->laravelValidator ??= $this->makeValidator($attributes);
|
||||||
@@ -71,6 +81,27 @@ abstract class AbstractValidator
|
|||||||
return $this->rules;
|
return $this->rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getActiveRules(array $attributes): array
|
||||||
|
{
|
||||||
|
$rules = $this->getRules();
|
||||||
|
|
||||||
|
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
|
protected function getMessages(): array
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
@@ -78,7 +109,7 @@ abstract class AbstractValidator
|
|||||||
|
|
||||||
protected function makeValidator(array $attributes): Validator
|
protected function makeValidator(array $attributes): Validator
|
||||||
{
|
{
|
||||||
$rules = Arr::only($this->getRules(), array_keys($attributes));
|
$rules = $this->getActiveRules($attributes);
|
||||||
|
|
||||||
$validator = $this->validator->make($attributes, $rules, $this->getMessages());
|
$validator = $this->validator->make($attributes, $rules, $this->getMessages());
|
||||||
|
|
||||||
|
@@ -18,7 +18,7 @@ use PHPUnit\Framework\Attributes\Test;
|
|||||||
|
|
||||||
class ValidatorTest extends TestCase
|
class ValidatorTest extends TestCase
|
||||||
{
|
{
|
||||||
private function extendToRequireLongPassword()
|
private function extendToRequireLongPassword(): void
|
||||||
{
|
{
|
||||||
$this->extend((new Extend\Validator(CustomUserValidator::class))->configure(function ($flarumValidator, $validator) {
|
$this->extend((new Extend\Validator(CustomUserValidator::class))->configure(function ($flarumValidator, $validator) {
|
||||||
$validator->setRules([
|
$validator->setRules([
|
||||||
@@ -30,7 +30,7 @@ class ValidatorTest extends TestCase
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function extendToRequireLongPasswordViaInvokableClass()
|
private function extendToRequireLongPasswordViaInvokableClass(): void
|
||||||
{
|
{
|
||||||
$this->extend((new Extend\Validator(CustomUserValidator::class))->configure(CustomValidatorClass::class));
|
$this->extend((new Extend\Validator(CustomUserValidator::class))->configure(CustomValidatorClass::class));
|
||||||
}
|
}
|
||||||
@@ -74,6 +74,51 @@ class ValidatorTest extends TestCase
|
|||||||
// If we have gotten this far, no validation exception has been thrown, so the test is successful.
|
// If we have gotten this far, no validation exception has been thrown, so the test is successful.
|
||||||
$this->assertTrue(true);
|
$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
|
class CustomValidatorClass
|
||||||
@@ -142,3 +187,13 @@ class CustomValidator extends AbstractValidator
|
|||||||
'name_plural' => ['required']
|
'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']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user