View Source Dsv.Email (Dsv v0.2.1)
Dsv.Email module offers functions to validate an email address and optionally checks specific parts of it.
Notes:
This function uses basic email format validation and does not guarantee that the email address is deliverable or exists.
Summary
Functions
The valid?/2
function provides a robust mechanism to validate an email address
while applying a set of associated validators based on provided criteria.
The validate/2
function provides a robust mechanism to validate an email address
while applying a set of associated validators based on provided criteria.
Functions
The valid?/2
function provides a robust mechanism to validate an email address
while applying a set of associated validators based on provided criteria.
Parameters
email
(string) - The email address to be validated.options
(keyword list) - A keyword list containing validation criteria for specific parts of the email. Valid keys include::username
- Validators for the username part of the email (before "@").:mail_server
- Validators for the mail server part of the email (between "@" and the last dot).:top_level_domain
- Validators for the top-level domain part of the email (after the last dot).
Returns
:true
Represents successful validation, where the email is correct and meets all the specified criteria.:false
if the data (email) fails validation.
Example
iex> Dsv.Email.valid?("mail.test@domain.com")
:true
iex> Dsv.Email.valid?("not-existing-email-address@domain.com")
:true
iex> Dsv.Email.valid?("this is not email address")
:false
iex> Dsv.Email.valid?("mail.test@domain.com", top_level_domain: [equal: "domain"], username: [format: ~r/m.*t.*/])
:false
iex> Dsv.Email.valid?("not-existing-email-address@domain.com", top_level_domain: [equal: "domain"], username: [format: ~r/m.*t.*/])
:false
iex> Dsv.Email.valid?("mail.test@domain.com", [])
:true
iex> Dsv.Email.valid?("this_is_not_real_email_address.com", top_level_domain: [equal: "com"])
:false
The validate/2
function provides a robust mechanism to validate an email address
while applying a set of associated validators based on provided criteria.
Parameters
email
(string) - The email address to be validated.options
(keyword list) - A keyword list containing validation criteria for specific parts of the email. Valid keys include::username
- Validators for the username part of the email (before "@").:mail_server
- Validators for the mail server part of the email (between "@" and the last dot).:top_level_domain
- Validators for the top-level domain part of the email (after the last dot).:message
- Custom message returned in case of the validation failure.
Returns
:ok
- Represents successful validation, where the email is correct and meets all the specified criteria.{:error, errors}
- if the data (email) fails validation. Theerrors
map provides detailed information about each part of the email that did not pass validation.
Example
iex> Dsv.Email.validate("mail.test@domain.com")
:ok
iex> Dsv.Email.validate("not-existing-email-address@domain.com")
:ok
iex> Dsv.Email.validate("this is not email address")
{:error, "Invalid email address."}
iex> Dsv.Email.validate("mail.test@domain.com", top_level_domain: [equal: "domain"], username: [format: ~r/m.*t.*/])
{:error, %{top_level_domain: ["Values must be equal"]}}
iex> Dsv.Email.validate("not-existing-email-address@domain.com", top_level_domain: [equal: "domain"], username: [format: ~r/m.*t.*/])
{:error, %{top_level_domain: ["Values must be equal"], username: ["Value not-existing-email-address does not match pattern m.*t.*"]}}
iex> Dsv.Email.validate("mail.test@domain.com", [])
:ok
iex> Dsv.Email.validate("this_is_not_real_email_address.com", top_level_domain: [equal: "com"])
{:error, "Invalid email address."}
iex> Dsv.Email.validate("mail.test@domain.com", top_level_domain: [equal: "domain"], username: [format: ~r/m.*t.*/], message: "Email address don't meet validation criteria.")
{:error, "Email address don't meet validation criteria."}