From eb1063eefc9a68f4166bd683f83e420b5657bee6 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Fri, 6 Dec 2024 09:37:57 +0100 Subject: [PATCH] feat: allow validating missing keys --- .../package-manager/src/AllValidatorRules.php | 31 ------------------- .../src/ConfigureAuthValidator.php | 2 +- .../src/ConfigureComposerValidator.php | 2 +- .../core/src/Foundation/AbstractValidator.php | 26 +++++++++++++--- 4 files changed, 24 insertions(+), 37 deletions(-) delete mode 100644 extensions/package-manager/src/AllValidatorRules.php diff --git a/extensions/package-manager/src/AllValidatorRules.php b/extensions/package-manager/src/AllValidatorRules.php deleted file mode 100644 index e2d8757e75..0000000000 --- a/extensions/package-manager/src/AllValidatorRules.php +++ /dev/null @@ -1,31 +0,0 @@ -getRules(); - - $validator = $this->validator->make($attributes, $rules, $this->getMessages()); - - foreach ($this->configuration as $callable) { - $callable($this, $validator); - } - - return $validator; - } -} diff --git a/extensions/package-manager/src/ConfigureAuthValidator.php b/extensions/package-manager/src/ConfigureAuthValidator.php index 14a9a03ed4..528a12c403 100644 --- a/extensions/package-manager/src/ConfigureAuthValidator.php +++ b/extensions/package-manager/src/ConfigureAuthValidator.php @@ -13,7 +13,7 @@ class ConfigureAuthValidator extends AbstractValidator { - use AllValidatorRules; + protected bool $validateMissingKeys = true; protected array $rules = [ 'github-oauth' => ['sometimes', 'array'], diff --git a/extensions/package-manager/src/ConfigureComposerValidator.php b/extensions/package-manager/src/ConfigureComposerValidator.php index 6292e5539c..a58a172d08 100644 --- a/extensions/package-manager/src/ConfigureComposerValidator.php +++ b/extensions/package-manager/src/ConfigureComposerValidator.php @@ -13,7 +13,7 @@ class ConfigureComposerValidator extends AbstractValidator { - use AllValidatorRules; + protected bool $validateMissingKeys = true; protected array $rules = [ 'minimum-stability' => ['sometimes', 'in:stable,RC,beta,alpha,dev'], diff --git a/framework/core/src/Foundation/AbstractValidator.php b/framework/core/src/Foundation/AbstractValidator.php index 788b85a7c0..12eafcd59d 100644 --- a/framework/core/src/Foundation/AbstractValidator.php +++ b/framework/core/src/Foundation/AbstractValidator.php @@ -22,13 +22,12 @@ abstract class AbstractValidator */ protected array $configuration = []; - /** - * @var array - */ protected array $rules = []; protected ?Validator $laravelValidator = null; + protected bool $validateMissingKeys = false; + public function __construct( protected Factory $validator, protected TranslatorInterface $translator @@ -54,6 +53,16 @@ public function assertValid(array $attributes): void } } + /** + * 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 { $this->laravelValidator ??= $this->makeValidator($attributes); @@ -71,6 +80,15 @@ protected function getRules(): array return $this->rules; } + protected function getActiveRules(array $attributes): array + { + $rules = $this->getRules(); + + return $this->validateMissingKeys + ? $rules + : Arr::only($rules, array_keys($attributes)); + } + protected function getMessages(): array { return []; @@ -78,7 +96,7 @@ protected function getMessages(): array 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());