public void UsingDefaultRules_IrregularEmailsNotAllowed(string email) { var options = new EmailAddressOptions() { AllowAnyCharacter = false }; var result = EmailAddressCharacterValidator.GetInvalidCharacters(email, options); result.Should().HaveCountGreaterOrEqualTo(1); }
public void UsingDefaultRules_AcceptsValidEmails(string email) { var options = new EmailAddressOptions() { AllowAnyCharacter = false }; var result = EmailAddressCharacterValidator.GetInvalidCharacters(email, options); result.Should().BeEmpty(); }
public void AllowAny_AcceptsAnything(string email) { var options = new EmailAddressOptions() { AllowAnyCharacter = true }; var result = EmailAddressCharacterValidator.GetInvalidCharacters(email, options); result.Should().BeEmpty(); }
public void WhenAllowsSpecificChars_ReturnInvalid(string email, string expected) { var options = new EmailAddressOptions() { AdditionalAllowedCharacters = "cat.", AllowAnyCharacter = false, AllowAnyLetter = false, AllowAnyDigit = false }; var result = EmailAddressCharacterValidator.GetInvalidCharacters(email, options); result.Should().BeEquivalentTo(expected); }
public void WhenAllowsOnlyDigit_ReturnsNonDigits(string email, string expected) { var options = new EmailAddressOptions() { AdditionalAllowedCharacters = null, AllowAnyCharacter = false, AllowAnyLetter = false, AllowAnyDigit = true }; var result = EmailAddressCharacterValidator.GetInvalidCharacters(email, options); result.Should().BeEquivalentTo(expected); }
/// <summary> /// Validates that the email contains only the characters permitted by the /// <see cref="EmailAddressOptions"/> configuration settings. /// </summary> protected virtual ValidationError ValidateAllowedCharacters(IEmailAddressValidationContext context) { var options = _userAreaDefinitionRepository.GetOptionsByCode(context.UserAreaCode).EmailAddress; var invalidCharacters = EmailAddressCharacterValidator.GetInvalidCharacters(context.Email.NormalizedEmailAddress, options); if (!invalidCharacters.Any()) { return(null); } // Be careful here, because we're handling user input. Any message should be escaped when // rendered, but to be safe we'll only include a single invalid character return(UserValidationErrors .EmailAddress .InvalidCharacters .Customize() .WithMessageFormatParameters(invalidCharacters.First().ToString()) .WithProperties(context.PropertyName) .Create()); }