Last Updated: 2021-05-15
I had the following code to validate emails:
validates :sender_email, email: true validates_presence_of :sender_email
It used a custom validator, implemented as follows:
class EmailValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) simple_email_regex = /.+@.+\..+/i return if value.match(simple_email_regex) record.errors.add attribute, 'email looks invalid' end end
What went wrong? Despite the
validates_presence_of :sender_email elsewhere in
the file, this validator sometimes ran with
nil for the
value, causing it to
nil#match does not exist.
Custom validators may run even if you have other code that supposedly checks for
non nil-iness. Therefore it is wiser to add guard clauses to protect against
nil in your validators.