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

[11.x] Conditional Validation for Fluent Email Rules #54255

Open
wants to merge 6 commits into
base: 11.x
Choose a base branch
from

Conversation

SanderMuller
Copy link
Contributor

Add Conditional Validation for Fluent Email Rules

This PR enhances the Email validation rule in Laravel by introducing conditional validation capabilities. It allows developers to dynamically enable or disable specific email validation rules, making the validation logic more flexible and adaptable to specific use cases.

Key Features:

  • Unset Default Rules: Developers can now unset default Email::default() rules, such as disabling MX record validation for certain routes. This is particularly useful for cases like Single Sign-On (SSO) emails without inboxes.
  • Dynamic Rule Application: Rules can now be conditionally applied using closures, enabling context-specific validation logic, such as skipping DNS checks for whitelisted email domains.

Key Changes:

  • Updated the Email class to accept bool|Closure conditions for rules like rfcCompliant, validateMxRecord, preventSpoofing, and withNativeValidation.
  • Modified the buildValidationRules method to evaluate conditions dynamically based on the provided attribute and value.
  • Added extensive tests in ValidationEmailRuleTest to ensure the new functionality works as intended for various scenarios.

Benefits:

  • Increased Flexibility: Developers can customize email validation rules based on the context, such as bypassing specific checks for certain email patterns or routes.
  • Enhanced Use Case Support: Scenarios like SSO emails or whitelisting specific domains are now easier to handle.
  • Backward Compatibility: Existing behavior remains unchanged unless conditional logic is explicitly applied.

This update aligns with Laravel's focus on making validation rules expressive and adaptable while maintaining backward compatibility.

@SanderMuller SanderMuller changed the title Conditional Validation for Fluent Email Rules [11.x] Conditional Validation for Fluent Email Rules Jan 18, 2025
src/Illuminate/Validation/Rules/Email.php Outdated Show resolved Hide resolved
src/Illuminate/Validation/Rules/Email.php Outdated Show resolved Hide resolved
Comment on lines +221 to 227
if (value($this->rfcCompliant, $attribute, $value)) {
$rules[] = 'rfc';
}

if ($this->strictRfcCompliant) {
if (value($this->strictRfcCompliant, $attribute, $value)) {
$rules[] = 'strict';
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Furthermore, it would be interesting, yet not easily implemented with the current implementation, if the method didn't only return whether to apply a specific rule but also if the more general rule should be applied with a flag (in this case strict). Implementing this by making the first param of the setter function bool|\Closure(): bool as well, would be one option, only passing in bool|Closure(): RfcComplianceEnum|null $condition another.

Our hypothetical RfcComplianceEnum would look something like this:

enum RfcComplianceEnum
{
    case Loose; // or Basic
    case Strict;
}

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.

2 participants