示例#1
0
        public void NegatedRejectsIntIncludedInTheDomain()
        {
            DomainValidator <int> validator = new DomainValidator <int>(true, 1, 2, 3);

            ValidationResults results = validator.Validate(2);

            Assert.IsFalse(results.IsValid);
        }
示例#2
0
        public void NegatedAcceptsIntNotIncludedInTheDomain()
        {
            DomainValidator <int> validator = new DomainValidator <int>(true, 1, 2, 3);

            ValidationResults results = validator.Validate(4);

            Assert.IsTrue(results.IsValid);
        }
示例#3
0
        public void NegatedRejectsStringIncludedInTheDomain()
        {
            DomainValidator <string> validator = new DomainValidator <string>(true, "a", "b", "c");

            ValidationResults results = validator.Validate("b");

            Assert.IsFalse(results.IsValid);
        }
示例#4
0
        public void NegatedAcceptsStringNotIncludedInTheDomain()
        {
            DomainValidator <string> validator = new DomainValidator <string>(true, "a", "b", "c");

            ValidationResults results = validator.Validate("d");

            Assert.IsTrue(results.IsValid);
        }
示例#5
0
        public void NonNegatedRejectsNull()
        {
            DomainValidator <object> validator = new DomainValidator <object>(false);

            ValidationResults results = validator.Validate(null);

            Assert.IsFalse(results.IsValid);
        }
示例#6
0
        public void DomainValidator()
        {
            var v = new DomainValidator(PropertyName, Description, new [] { "ONE", "TWO" });

            AssertValidatorProperties(v);

            var o = new SimpleObject();

            Assert.IsTrue(v.Validate(o));
            o.SimpleProperty = string.Empty;
            Assert.IsTrue(v.Validate(o));
            o.SimpleProperty = "ONE";
            Assert.IsTrue(v.Validate(o));
            o.SimpleProperty = "TWO";
            Assert.IsTrue(v.Validate(o));

            o.SimpleProperty = "THREE";
            Assert.IsFalse(v.Validate(o));
        }
示例#7
0
        static void CreatingAndUsingValidatorsDirectly()
        {
            // Create a Contains Characters Validator and use it to validate a String value.
            Validator charsValidator = new ContainsCharactersValidator("cat", ContainsCharacters.All,
                                                                       "Value must contain {4} of the characters '{3}'.");

            Console.WriteLine("Validating a string value using a Contains Characters Validator...");
            charsValidator.Tag = "Validating the String value 'disconnected'";
            // This overload of the Validate method returns a new ValidationResults
            // instance populated with any/all of the validation errors.
            ValidationResults valResults = charsValidator.Validate("disconnected");
            // Create a Domain Validator and use it to validate an Integer value.
            Validator integerValidator = new DomainValidator <int>("Value must be in the list 1, 3, 7, 11, 13.",
                                                                   new int[] { 1, 3, 7, 11, 13 });

            integerValidator.Tag = "Validating the Integer value '42'";
            Console.WriteLine("Validating an integer value using a Domain Validator...");
            // This overload of the Validate method takes an existing ValidationResults
            // instance and adds any/all of the validation errors to it.
            integerValidator.Validate(42, valResults);
            // Create an Or Composite Validator containing two validators.
            // Note that the NotNullValidator is negated to allow NULL values.
            Validator[] valArray = new Validator[] {
                new NotNullValidator(true, "Value can be NULL."),
                new StringLengthValidator(5, RangeBoundaryType.Inclusive, 5, RangeBoundaryType.Inclusive,
                                          "Value must be between {3} ({4}) and {5} ({6}) chars.")
            };
            Validator orValidator = new OrCompositeValidator("Value can be NULL or a string of 5 characters.", valArray);

            // Validate two strings using the Or Composite Validator.
            Console.WriteLine("Validating a NULL value using an Or Composite Validator...");
            orValidator.Validate(null, valResults);  // this will not cause a validation error
            Console.WriteLine("Validating a string value using an Or Composite Validator...");
            orValidator.Validate("MoreThan5Chars", valResults);
            // Validate a single property of an existing class instance.
            // First create a Product instance with an invalid ID value.
            IProduct productWithID = new Product();

            PopulateInvalidProduct(productWithID);
            // Create a Property Value Validator that will use a RegexValidator
            // to validate the property value.
            Validator propValidator = new PropertyValueValidator <Product>("ID",
                                                                           new RegexValidator("[A-Z]{2}[0-9]{4}", "Product ID must be 2 capital letters and 4 numbers."));

            Console.WriteLine("Validating one property of an object using a Property Value Validator...");
            propValidator.Validate(productWithID, valResults);
            // Now display the results of all the previous validation operations.
            ShowValidationResults(valResults);
        }
示例#8
0
 static void CreatingAndUsingValidatorsDirectly()
 {
     // Create a Contains Characters Validator and use it to validate a String value.
     Validator charsValidator = new ContainsCharactersValidator("cat", ContainsCharacters.All,
                                                                  "Value must contain {4} of the characters '{3}'.");
     Console.WriteLine("Validating a string value using a Contains Characters Validator...");
     charsValidator.Tag = "Validating the String value 'disconnected'";
     // This overload of the Validate method returns a new ValidationResults
     // instance populated with any/all of the validation errors.
     ValidationResults valResults = charsValidator.Validate("disconnected");
     // Create a Domain Validator and use it to validate an Integer value.
     Validator integerValidator = new DomainValidator<int>("Value must be in the list 1, 3, 7, 11, 13.",
                                                              new int[] { 1, 3, 7, 11, 13 });
     integerValidator.Tag = "Validating the Integer value '42'";
     Console.WriteLine("Validating an integer value using a Domain Validator...");
     // This overload of the Validate method takes an existing ValidationResults
     // instance and adds any/all of the validation errors to it.
     integerValidator.Validate(42, valResults);
     // Create an Or Composite Validator containing two validators.
     // Note that the NotNullValidator is negated to allow NULL values.
     Validator[] valArray = new Validator[] {
           new NotNullValidator(true, "Value can be NULL."),
           new StringLengthValidator(5, RangeBoundaryType.Inclusive, 5, RangeBoundaryType.Inclusive,
                                         "Value must be between {3} ({4}) and {5} ({6}) chars.")
         };
     Validator orValidator = new OrCompositeValidator("Value can be NULL or a string of 5 characters.", valArray);
     // Validate two strings using the Or Composite Validator.
     Console.WriteLine("Validating a NULL value using an Or Composite Validator...");
     orValidator.Validate(null, valResults);  // this will not cause a validation error
     Console.WriteLine("Validating a string value using an Or Composite Validator...");
     orValidator.Validate("MoreThan5Chars", valResults);
     // Validate a single property of an existing class instance.
     // First create a Product instance with an invalid ID value.
     IProduct productWithID = new Product();
     PopulateInvalidProduct(productWithID);
     // Create a Property Value Validator that will use a RegexValidator
     // to validate the property value.
     Validator propValidator = new PropertyValueValidator<Product>("ID",
                         new RegexValidator("[A-Z]{2}[0-9]{4}", "Product ID must be 2 capital letters and 4 numbers."));
     Console.WriteLine("Validating one property of an object using a Property Value Validator...");
     propValidator.Validate(productWithID, valResults);
     // Now display the results of all the previous validation operations.
     ShowValidationResults(valResults);
 }
示例#9
0
        public HttpResponseMessage Add(DomainModel model)
        {
            var dom = new Domain
            {
                DomainName = model.NewDomain
            };
            var domainval = new DomainValidator();

            if (model != null && ModelState.IsValid && domainval.Validate(dom).IsValid)
            {
                using (DataContext context = new DataContext())
                {
                    _domainRepository.InsertDomain(model.NewDomain);
                    _domainRepository.Save();
                    return(Request.CreateResponse(HttpStatusCode.Accepted, true));
                }
            }
            return(Request.CreateResponse(HttpStatusCode.Forbidden, "Wrong Data!"));
        }
        public void NegatedAcceptsIntNotIncludedInTheDomain()
        {
            DomainValidator<int> validator = new DomainValidator<int>(true, 1, 2, 3);

            ValidationResults results = validator.Validate(4);

            Assert.IsTrue(results.IsValid);
        }
        public void NegatedRejectsIntIncludedInTheDomain()
        {
            DomainValidator<int> validator = new DomainValidator<int>(true, 1, 2, 3);

            ValidationResults results = validator.Validate(2);

            Assert.IsFalse(results.IsValid);
        }
        public void NegatedAcceptsStringNotIncludedInTheDomain()
        {
            DomainValidator<string> validator = new DomainValidator<string>(true, "a", "b", "c");

            ValidationResults results = validator.Validate("d");

            Assert.IsTrue(results.IsValid);
        }
        public void NegatedRejectsStringIncludedInTheDomain()
        {
            DomainValidator<string> validator = new DomainValidator<string>(true, "a", "b", "c");

            ValidationResults results = validator.Validate("b");

            Assert.IsFalse(results.IsValid);
        }
        public void NonNegatedRejectsNull()
        {
            DomainValidator<object> validator = new DomainValidator<object>(false);

            ValidationResults results = validator.Validate(null);

            Assert.IsFalse(results.IsValid);
        }