Skip to content

Commit

Permalink
Introduce AVD::Constraints::Email::Mode::HTML5_ALLOW_NO_TLD (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacksmoke16 authored Dec 5, 2022
1 parent 0a88d5e commit d9aff13
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
44 changes: 25 additions & 19 deletions spec/constraints/email_validator_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ struct EmailValidatorTest < AVD::Spec::ConstraintValidatorTestCase
self.assert_no_violation
end

@[DataProvider("valid_emails_html5")]
def test_valid_emails_html5(value : String) : Nil
self.validator.validate value, self.new_constraint mode: CONSTRAINT::Mode::HTML5
@[DataProvider("valid_emails")]
def test_valid_emails(value : String) : Nil
self.validator.validate value, self.new_constraint
self.assert_no_violation
end

def valid_emails_html5 : Tuple
def valid_emails : Tuple
{
{"blacksmoke16@dietrich.app"},
{"example@example.co.uk"},
Expand All @@ -45,21 +45,6 @@ struct EmailValidatorTest < AVD::Spec::ConstraintValidatorTestCase
end

def invalid_emails : Tuple
{
{"example"},
{"example@"},
{"example@localhost"},
{"foo@example.com bar"},
}
end

@[DataProvider("invalid_emails_html5")]
def test_invalid_emails_html5(value : String) : Nil
self.validator.validate value, self.new_constraint mode: CONSTRAINT::Mode::HTML5, message: "my_message"
self.assert_violation "my_message", CONSTRAINT::INVALID_FORMAT_ERROR, value
end

def invalid_emails_html5 : Tuple
{
{"example"},
{"example@"},
Expand All @@ -80,6 +65,27 @@ struct EmailValidatorTest < AVD::Spec::ConstraintValidatorTestCase
}
end

@[DataProvider("invalid_emails_html5_allow_no_tld")]
def test_invalid_emails_html5_allow_no_tld(value : String) : Nil
self.validator.validate value, self.new_constraint mode: CONSTRAINT::Mode::HTML5_ALLOW_NO_TLD, message: "my_message"
self.assert_violation "my_message", CONSTRAINT::INVALID_FORMAT_ERROR, value
end

def invalid_emails_html5_allow_no_tld : Tuple
{
{"example bar"},
{"example@"},
{"example@ bar"},
{"example@localhost bar"},
{"foo@example.com bar"},
}
end

def test_valid_email_html5_allow_no_tld : Nil
self.validator.validate "example@example", self.new_constraint mode: CONSTRAINT::Mode::HTML5_ALLOW_NO_TLD
self.assert_no_violation
end

private def create_validator : AVD::ConstraintValidatorInterface
CONSTRAINT::Validator.new
end
Expand Down
10 changes: 7 additions & 3 deletions src/constraints/email.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# ### mode
#
# **Type:** `AVD::Constraints::Email::Mode` **Default:** `AVD::Constraints::Email::Mode::Loose`
# **Type:** `AVD::Constraints::Email::Mode` **Default:** `AVD::Constraints::Email::Mode::HTML5`
#
# Defines the pattern that should be used to validate the email address.
#
Expand Down Expand Up @@ -42,16 +42,20 @@
class Athena::Validator::Constraints::Email < Athena::Validator::Constraint
# Determines _how_ the email address should be validated.
enum Mode
# Validates the email against the [HTML5 input pattern](https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address).
# Validates the email against the [HTML5 input pattern](https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address), but requires a [TLD](https://en.wikipedia.org/wiki/Top-level_domain) to be present.
HTML5

# Same as `HTML5`, but follows the pattern exactly, allowing there to be no [TLD](https://en.wikipedia.org/wiki/Top-level_domain).
HTML5_ALLOW_NO_TLD

# TODO: Implement this mode.
# STRICT

# Returns the `::Regex` pattern for `self`.
def pattern : ::Regex
case self
in .html5? then /^[a-zA-Z0-9.!\#$\%&\'*+\\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/
in .html5? then /^[a-zA-Z0-9.!\#$\%&\'*+\\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/
in .html5_allow_no_tld? then /^[a-zA-Z0-9.!\#$\%&\'*+\\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
end
end
end
Expand Down

0 comments on commit d9aff13

Please sign in to comment.