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);
        }
Exemple #6
0
        /// <summary>
        /// Validates the specified <paramref name="emailAddress"/>, returning
        /// any charcters that are not valid according to the rules set out
        /// in the specified <paramref name="options"/>.
        /// </summary>
        /// <param name="emailAddress">The email to validate.</param>
        /// <param name="options">The configuraton options to use during validation.</param>
        /// <returns>Enumerable collection of invalid characters.</returns>
        public static IEnumerable <char> GetInvalidCharacters(string emailAddress, EmailAddressOptions options)
        {
            if (emailAddress == null)
            {
                throw new ArgumentNullException(nameof(emailAddress));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.AllowAnyCharacter)
            {
                return(Array.Empty <char>());
            }

            // Always allow @ because an email is invalid without it
            var badCharacters = emailAddress
                                .Distinct()
                                .Where(c => c != '@');

            if (options.AllowAnyDigit)
            {
                badCharacters = badCharacters.Where(c => !Char.IsDigit(c));
            }

            if (options.AllowAnyLetter)
            {
                badCharacters = badCharacters.Where(c => !Char.IsLetter(c));
            }

            if (!string.IsNullOrEmpty(options.AdditionalAllowedCharacters))
            {
                badCharacters = badCharacters.Where(c => !options.AdditionalAllowedCharacters.Contains(c));
            }

            return(badCharacters);
        }