Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to optionally merge automatically inferred rules with manual rules #848

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

CWAscend
Copy link

@CWAscend CWAscend commented Aug 19, 2024

💪 Motivation

I would like the ability to use a combination of Attribute rules and manual rules that are defined within the static rules method in a Data object. For me, this will massively clean up my rules method when I have to reach for it, such as which I might be reaching for a callable rule, or using the Rule::in() rule where the array is dynamic - just to name some examples.

🛠 Changes

  • Added a new Spatie\LaravelData\Attributes\MergeRules attribute
  • This will merge the rules from the rules method rather than overwriting them
  • Example usage can be found in the test Data class here: tests/Fakes/DataWithMergedRuleset.php

🧪 Testing

  • A new test has been created: it will merge validation rules, which ensures both validation rules from the Attributes and the rules method are picked up.
  • A new test has been created: it will merge validation rules using string rules which ensures the validation rules can be a piped string of rules instead of an array of files for each attribute

🧙 Reminders (Author)

  • Have you read through your own diff? 👀
  • Are all tests passing? 🧪

@CWAscend CWAscend changed the title Add the ability to optionally merge attribute rules with method rules Add the ability to optionally merge automatically inferred rules with manual rules Aug 19, 2024
@CheesyTech
Copy link

Up

Copy link
Member

@rubenvanassche rubenvanassche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Some changes required in order to get it merged.

Comment on lines +247 to +249
$manualRules = collect($manualRules)->map(
fn (string|array $rules) => is_array($rules) ? $rules : explode('|', $rules)
)->all();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the RuleDenormalizer here?

Also: when we add an attribute not to validate the property it will still be validated we don't want that.

I think a better solution would be to implement the logic within the loop where addRules isn't called since it replaces them but rather rules are merged over there (or not if the property shouldn"t be validated).


protected function shouldMergeRules(DataClass $class): bool
{
return $class->attributes->contains(fn (object $attribute) => $attribute::class === MergeRules::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for an extra function here, just inline the variable

@@ -223,7 +224,7 @@ protected function resolveToplevelRules(
}


protected function resolveOverwrittenRules(
protected function resolveRules(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us rename this to appendOverwrittenRules, resolveRules is too general.

@@ -240,9 +241,19 @@ protected function resolveOverwrittenRules(
$path
);

$overwrittenRules = app()->call([$class->name, 'rules'], ['context' => $validationContext]);
$manualRules = app()->call([$class->name, 'rules'], ['context' => $validationContext]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this as overwrittenRules.

Comment on lines +18 to +22
public static function rules(): array
{
return [
'first_name' => ['min:2']
];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline these classes within the tests, that makes the fakes directory a bit less heavy.

@CWAscend
Copy link
Author

@rubenvanassche thanks for the feedback, I'll action this over the weekend 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants