Skip to content

fix(validators): respect empty allowed_domains list in EmailValidator - #2422

Merged
waketzheng merged 3 commits into
tortoise:developfrom
60507:fix/email-validator-empty-allowed-domains
Nov 21, 2026
Merged

waketzheng merged 3 commits into
tortoise:developfrom
60507:fix/email-validator-empty-allowed-domains

Conversation

@60507

@60507 60507 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes EmailValidator ignoring an explicitly empty allowed_domains list (allowed_domains=[]). With this fix:

  • Providing allowed_domains=[] blocks all domains (raising InvalidEmailAddress), consistent with URLValidator(allowed_schemes=[]) fixed in Respect empty URL scheme allowlists #2394.
  • Omitting allowed_domains or passing None preserves default syntax validation across all valid domains.
  • Non-empty allowlists continue to restrict acceptance to matching domains (case-insensitive).

Motivation and Context

EmailValidator previously initialized self.allowed_domains = [i.lower() for i in allowed_domains or ()]. When allowed_domains=[] was provided, bool(self.allowed_domains) evaluated to False in __call__, falling back to standard domain syntax validation and accepting any valid domain instead of enforcing the empty allowlist.

This brings symmetry with #2394 (URLValidator), where empty allowlists block all schemes.

How Has This Been Tested?

  • Ran pytest tests/test_validators.py (83 passed).
  • Added test_email_validator_rejects_all_domains_when_allowlist_is_empty covering both EmailValidator([]) and EmailValidator(allowed_domains=[]).
  • Verified ruff check and ruff format --check clean.
  • Verified mypy type checking clean.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed

codspeed Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 21 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 24 untouched benchmarks


Comparing 60507:fix/email-validator-empty-allowed-domains (6452351) with develop (938163c)

Open in CodSpeed

@waketzheng

Copy link
Copy Markdown
Contributor

Leaving a note here for anyone reviewing this later or tracing the change via git log: the empty list meaning "reject everything" rather than "no restriction" is deliberate, and it's the reading that matches both the type annotation and set semantics.

  • The type already says they're different. allowed_domains is list[str] | None. None is "no restriction"; a list is "this set of domains". Collapsing [] into None at runtime (allowed_domains or []) throws away the distinction the annotation promises, and the old if self.allowed_domains: check then makes [] a no-op instead of an empty allowlist.
  • An empty allowlist permits nothing. An allowlist is a set of permitted values; the empty set permits none. Anyone who wants "no restriction" has a clearer spelling: omit the argument or pass None. [] was only ever read as "no restriction" because the implementation silently converted it — not because that's what an empty list means.
  • It lines up with URLValidator. allowed_schemes=[] has the same shape and is fixed the same way (Respect empty URL scheme allowlists #2394). Keeping the two validators consistent avoids a second round of "why does this one treat empty differently".
  • Nothing is lost for callers. The previous behavior is one keyword away: EmailValidator() or EmailValidator(allowed_domains=None) still skips the allowlist. The CHANGELOG entry is under Changed with that migration note.

The alternative — keeping [] as "no restriction" — would mean None and [] stay indistinguishable at runtime while the annotation says they aren't, and it would leave the two validators disagreeing about what an empty allowlist means.

@waketzheng
waketzheng merged commit 37eba85 into tortoise:develop Nov 21, 2026
29 checks passed
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