Today's tip started as a little bit of trivia discussion with Aaron. We always enforce max validation lengths in our validation rules, but the question came up: Is max length redundant with email validation? Doesn't the email specification itself enforce a maximum length?
This led me to do some research into the various RFCs that govern SMTP and email delivery. In poking around, I discovered two length limits that are relevant here:
- The local part of an email address (the part before the
@
) is limited to 64 characters. - The domain part of an email address (the part after the
@
) is limited to 63 characters.
Typically, we'd store an email address in a 255-character field in the database. So based on the RFC rules, it seems like max:255
really would be redundant with the email
validation rule, which enforces RFC rules.
But, this isn't actually the case. By default, Laravel's email
rule doesn't enforce strict RFC interpretations. It allows some warnings to pass.
So while email
and max:255
isn't redundant, email:strict
and max:255
is redundant.
Honestly, though, this won't change how I write my rules. I still like the explicit nature of having a max length rule visible, even if strict RFC validation technically makes it redundant.
Hope this helps,
Joel
P.S. Would you like a deeper understanding of how Laravel validation works and how to use it effectively in your app?