public void Test_FieldIs8_FailWhenTooLong()
        {
            ValidatorMaxFieldLength validator = new ValidatorMaxFieldLength(8);

            string errorMsg;

            bool result = validator.ValidateField("123456789", out errorMsg);

            Assert.IsFalse(result);
            Assert.AreEqual(errorMsg, "Field is longer than maximum length (max length: 8): 123456789", "errorMsg is: " + errorMsg);
        }
        public void Test_FieldIs8_PassWhenShorter()
        {
            ValidatorMaxFieldLength validator = new ValidatorMaxFieldLength(8);

            string errorMsg;

            bool result = validator.ValidateField("1234567", out errorMsg);

            Assert.IsTrue(result);
            Assert.AreEqual(errorMsg, "", "errorMsg is: " + errorMsg);
        }
Exemplo n.º 3
0
        public Dictionary<int, List<IValidator>> GetValidators()
        {
            Dictionary<int, List<IValidator>> validators = new Dictionary<int, List<IValidator>>();

            var section = (Hashtable)ConfigurationManager.GetSection("validators");

            foreach (DictionaryEntry pair in section)
            {
                IValidator validator;

                int key = Int32.Parse(pair.Key.ToString());
                string value = pair.Value.ToString();

                switch (value)
                {
                    case "ValidatorDateValidator_YYMMDD":
                        validator = new ValidatorDate_YYMMDD();
                        break;
                    case "ValidatorDateValidator_YYYYMMDD":
                        validator = new ValidatorDate_YYYYMMDD();
                        break;
                    case "ValidatorDateValidator_MMDDYYYY":
                        validator = new ValidatorDate_MMDDYYYY();
                        break;
                    case "ValidatorNumber":
                        validator = new ValidatorNumber();
                        break;
                    case "ValidatorNoNumbers":
                        validator = new ValidatorNoNumbers();
                        break;
                    case "ValidatorFieldIsNotBlank":
                        validator = new ValidatorFieldIsNotBlank();
                        break;
                    case "ValidatorCurrency":
                        validator = new ValidatorCurrency();
                        break;
                    default: validator = null;
                        break;
                }

                if (value.Contains("ValidatorFieldLength_"))
                {
                    string[] parts = value.Split('_');

                    int fieldLength = Int32.Parse(parts[1].ToString());

                    validator = new ValidatorFieldLength(fieldLength);
                }

                if (value.Contains("ValidatorMaxFieldLength"))
                {
                    string[] parts = value.Split('_');

                    int maxFieldLength = Int32.Parse(parts[1].ToString());

                    validator = new ValidatorMaxFieldLength(maxFieldLength);
                }

                if (validator != null)
                {

                    List<IValidator> validatorList;
                    if (!validators.TryGetValue(key, out validatorList))
                    {
                        validatorList = new List<IValidator>();

                        validators.Add(key, validatorList);
                    }

                    validatorList.Add(validator);
                }
            }

            return validators;
        }