Example #1
0
    public static bool SatisfiesType(string input, string typeName)
    {
        ValidationSection config = (ValidationSection)WebConfigurationManager.GetSection("validation");

        if (config == null)
        {
            throw new Exception("Missing validation section in web.config");
        }
        string expression = config.ValidationTypes.Get(typeName);

        return(SatisifiesExpression(input, expression));
    }
Example #2
0
        public void TestCase03_ReadDutchPhoneNumberValidationConfiguration_ClearAreaCodes()
        {
            ValidationSection validationSection = ConfigurationManager.GetSection("Enkoni.DataAnnotations.TestCase03") as ValidationSection;

            Assert.IsNotNull(validationSection);
            Assert.AreEqual(1, validationSection.DutchPhoneNumberValidators.Count);

            Assert.IsTrue(validationSection.DutchPhoneNumberValidators[string.Empty].AllowCountryCallingCode);
            Assert.IsFalse(validationSection.DutchPhoneNumberValidators[string.Empty].AllowCarrierPreselect);

            Assert.AreEqual(1, validationSection.DutchPhoneNumberValidators[string.Empty].AreaCodes.Count);
            Assert.IsNotNull(validationSection.DutchPhoneNumberValidators[string.Empty].AreaCodes["0123"]);
            Assert.AreEqual("0123", validationSection.DutchPhoneNumberValidators[string.Empty].AreaCodes["0123"].AreaCode);
        }
Example #3
0
        public void TestCase06_ReadEmailValidationConfiguration_Default()
        {
            ValidationSection validationSection = ConfigurationManager.GetSection("Enkoni.DataAnnotations.TestCase06") as ValidationSection;

            Assert.IsNotNull(validationSection);
            Assert.IsNotNull(validationSection.EmailValidators);
            Assert.AreEqual(2, validationSection.EmailValidators.Count);

            Assert.IsFalse(validationSection.EmailValidators[string.Empty].AllowComments);
            Assert.IsFalse(validationSection.EmailValidators[string.Empty].AllowIPAddresses);
            Assert.IsFalse(validationSection.EmailValidators[string.Empty].RequireTopLevelDomain);

            Assert.AreEqual(0, validationSection.EmailValidators[string.Empty].IncludeDomains.Count);
            Assert.AreEqual(0, validationSection.EmailValidators[string.Empty].ExcludeDomains.Count);
        }
Example #4
0
        public void TestCase07_ReadEmailValidationConfiguration_IncludeExcludeDomains()
        {
            ValidationSection validationSection = ConfigurationManager.GetSection("Enkoni.DataAnnotations.TestCase07") as ValidationSection;

            Assert.IsNotNull(validationSection);
            Assert.AreEqual(1, validationSection.EmailValidators.Count);

            Assert.IsFalse(validationSection.EmailValidators[string.Empty].AllowComments);
            Assert.IsTrue(validationSection.EmailValidators[string.Empty].AllowIPAddresses);
            Assert.IsFalse(validationSection.EmailValidators[string.Empty].RequireTopLevelDomain);

            Assert.AreEqual(2, validationSection.EmailValidators[string.Empty].IncludeDomains.Count);
            Assert.IsNotNull(validationSection.EmailValidators[string.Empty].IncludeDomains["microsoft.com"]);
            Assert.AreEqual("microsoft.com", validationSection.EmailValidators[string.Empty].IncludeDomains["microsoft.com"].Pattern);
            Assert.AreEqual(2, validationSection.EmailValidators[string.Empty].ExcludeDomains.Count);
            Assert.IsNotNull(validationSection.EmailValidators[string.Empty].ExcludeDomains["*yahoo.com"]);
            Assert.AreEqual("*yahoo.com", validationSection.EmailValidators[string.Empty].ExcludeDomains["*yahoo.com"].Pattern);
        }
Example #5
0
            /// <summary>Reads the configuration and sets the configured values.</summary>
            /// <param name="sectionName">The name of the config section that must be read. Use <see langword="null"/> or <see cref="string.Empty"/> to use the default section name.</param>
            /// <returns>The values that were read from the configuration or <see langword="null"/> if there was no configuration.</returns>
            private static Dictionary <string, ConfiguredValuesContainer> ReadConfiguration(string sectionName)
            {
                if (string.IsNullOrEmpty(sectionName))
                {
                    sectionName = ValidationSection.DefaultSectionName;
                }

                ValidationSection validatorsSection = ConfigurationManager.GetSection(sectionName) as ValidationSection;

                if (validatorsSection == null || validatorsSection.EmailValidators.Count == 0)
                {
                    return(null);
                }

                Dictionary <string, ConfiguredValuesContainer> configuredValues = new Dictionary <string, ConfiguredValuesContainer>();

                foreach (EmailValidationConfigElement validatorConfig in validatorsSection.EmailValidators.Values)
                {
                    ConfiguredValuesContainer valuesContainer = new ConfiguredValuesContainer {
                        AllowComments         = validatorConfig.AllowComments,
                        AllowIPAddresses      = validatorConfig.AllowIPAddresses,
                        RequireTopLevelDomain = validatorConfig.RequireTopLevelDomain
                    };

                    List <string> configuredIncludeDomains = new List <string>(validatorConfig.IncludeDomains.Count);
                    foreach (EmailDomainConfigElement domain in validatorConfig.IncludeDomains)
                    {
                        configuredIncludeDomains.Add(domain.Pattern);
                    }

                    List <string> configuredExcludeDomains = new List <string>(validatorConfig.ExcludeDomains.Count);
                    foreach (EmailDomainConfigElement domain in validatorConfig.ExcludeDomains)
                    {
                        configuredExcludeDomains.Add(domain.Pattern);
                    }

                    valuesContainer.IncludeDomains = string.Join(";", configuredIncludeDomains.ToArray());
                    valuesContainer.ExcludeDomains = string.Join(";", configuredExcludeDomains.ToArray());

                    configuredValues.Add(validatorConfig.Name, valuesContainer);
                }

                return(configuredValues);
            }
            /// <summary>Reads the configuration and sets the configured values.</summary>
            /// <param name="sectionName">The name of the config section that must be read. Use <see langword="null"/> or <see cref="string.Empty"/> to use the default section name.</param>
            /// <returns>The values that were read from the configuration or <see langword="null"/> if there was no configuration.</returns>
            private static Dictionary <string, ConfiguredValuesContainer> ReadConfiguration(string sectionName)
            {
                if (string.IsNullOrEmpty(sectionName))
                {
                    sectionName = ValidationSection.DefaultSectionName;
                }

                ValidationSection validatorsSection = ConfigurationManager.GetSection(sectionName) as ValidationSection;

                if (validatorsSection == null || validatorsSection.DutchPhoneNumberValidators.Count == 0)
                {
                    return(null);
                }

                Dictionary <string, ConfiguredValuesContainer> configuredValues = new Dictionary <string, ConfiguredValuesContainer>();

                foreach (DutchPhoneNumberValidationConfigElement validatorConfig in validatorsSection.DutchPhoneNumberValidators.Values)
                {
                    List <string> configuredAreaCodes = new List <string>(validatorConfig.AreaCodes.Count);
                    foreach (DutchPhoneNumberAreaCodeConfigElement areaCode in validatorConfig.AreaCodes)
                    {
                        configuredAreaCodes.Add(areaCode.AreaCode.TrimStart('0'));
                    }

                    string[]             defaultAreaCodes = Resources.AreaCodes_NL.Split(';');
                    IEnumerable <string> difference       = defaultAreaCodes.Except(configuredAreaCodes);
                    difference.Concat(configuredAreaCodes.Except(defaultAreaCodes));

                    ConfiguredValuesContainer valuesContainer = new ConfiguredValuesContainer {
                        AllowCountryCallingCode = validatorConfig.AllowCountryCallingCode,
                        AllowCarrierPreselect   = validatorConfig.AllowCarrierPreselect,
                        AreaCodesOverridden     = difference.Any(),
                        AreaCodes = string.Join(";", configuredAreaCodes.ToArray())
                    };
                    configuredValues.Add(validatorConfig.Name, valuesContainer);
                }

                return(configuredValues);
            }