public void Validate_OptionalCollection_Using_Registered_Specification()
        {
            //Build test data
            var customer = new Customer() { Name = "TestCustomer"};
            var validContact = new Contact() {FirstName = "Johnny B", LastName = "Good"};
            var invalidContact = new Contact() { FirstName = "Baddy"};

            customer.Contacts = new List<Contact>() {validContact, invalidContact};

            //Build specifications
            ValidationCatalog.AddSpecification<Customer>(spec =>
                                                               {
                                                                   spec.Check(cust => cust.Name).Required();
                                                                   spec.Check(cust => cust.Contacts).Required();
                                                               });

            ValidationCatalog.AddSpecification<Contact>(spec =>
                                                              {
                                                                  spec.Check(c => c.FirstName).Required();
                                                                  spec.Check(c => c.LastName).Required();
                                                              });

            ValidationCatalog.ValidateObjectGraph = true;

            //Validate
            var results = ValidationCatalog.Validate(customer);

            Assert.That(results.Errors.Count, Is.AtLeast(1));
        }
        public void ForEachSpecification_IsInvalid()
        {
            //Don't implicitly validate object graph
            ValidationCatalog.ValidateObjectGraph = false;

            //create list of contacts to validate
            var contacts = new List<Contact>
                               {
                                   new Contact() {FirstName = String.Empty, LastName = "Smith"},
                                   new Contact() {FirstName = String.Empty, LastName = String.Empty},
                                   new Contact() {FirstName = "Joe", LastName = "Smith"}
                               };

            var customer = new Customer() {Name = "Smith Industries", Contacts = contacts};

            //Add Specification for Customer and Address
            ValidationCatalog.AddSpecification<Customer>(spec =>
               {
                   spec.Check(c => c.Contacts).Required().ForEachSpecification<Contact>(
                       cspec =>
                           {
                               cspec.Check(c => c.LastName).Required();
                               cspec.Check(c => c.FirstName).Required();
                           });
               });

            //Validate Customer
            var results = ValidationCatalog.Validate(customer);

            Assert.That(results.Errors, Is.Not.Empty);

            var allerrors = results.Errors.First().AllErrorMessages().ToList();

            Assert.That(results.Errors.First().NestedValidationResults, Is.Not.Empty);
        }
        public void CustomerName_Optional_IsValid()
        {
            var customer = new Customer();

            var spec = new CustomerSpecification();
            spec.Check(cust => cust.Name).Optional();

            List<ValidationResult> notification = spec.Validate(customer);
            Assert.IsEmpty(notification);
        }
        public void CustomerName_OptionalAndLength_IsNotValid()
        {
            var customer = new Customer() { Name = "A"};

            var spec = new CustomerSpecification();
            spec.Check(cust => cust.Name).Optional().LengthBetween(2, 100);

            List<ValidationResult> notification = spec.Validate(customer);
            Assert.IsNotEmpty(notification);
            Assert.AreEqual(1, notification.Count);
        }
        public void Validate_OptionalNestedProperty_WithNullValue_IsValid()
        {
            var customer = new Customer();

            ValidationCatalog.AddSpecification<Customer>( spec => spec.Check(cust => cust.Address.Street).Optional()
                .MaxLength(255));

            var results = ValidationCatalog.Validate(customer);

            Assert.That(results.Errors, Is.Empty);
        }
        public void CustomerAddressCountry_Required_IsValid()
        {
            var customer = new Customer() { Address = new Address() { Country = new Country() } };

            var spec = new CustomerSpecification();
            spec.Check(cust => cust.Address.Country.Name).Required();

            List<ValidationResult> notification = spec.Validate(customer);
            Assert.That(notification, Is.Not.Empty);
            Assert.That(notification.Count, Is.EqualTo(1));
            Assert.That(notification[0].Message, Is.EqualTo("Address Country Name is required."));
        }
示例#7
0
        public void When_Required_And_CollectionValue_Is_Null()
        {
            var customer = new Customer();

            var validator = new Required<Customer, IEnumerable>();
            var context = new RuleValidatorContext<Customer, IEnumerable>(customer, "Contacts", customer.Contacts, null, null);

            //Validate the validator only, return true of no error returned
            var result = validator.Validate(context, null);

            Assert.IsNotEmpty(result.Message);
        }
示例#8
0
        public void When_Required_And_StringValue_Is_Null()
        {
            var customer = new Customer();

            var validator = new Required<Customer, string>();
            var context = new RuleValidatorContext<Customer, string>(customer, "Name", customer.Name, null, null);

            //Validate the validator only, return true of no error returned
            var result = validator.Validate(context, null);

            Assert.IsNotEmpty(result.Message);
        }
示例#9
0
        public void ValidationNotification_WithOneError_IsValid()
        {
            ValidationCatalog.AddSpecification<Customer>(spec =>
            {
                spec.Check(c => c.Name).Required();
                spec.Warn(c => c.Address).Required().Specification<AddressSpecification>();
            });

            var customer = new Customer();

            var vn = ValidationCatalog.Validate(customer);

            Assert.That(vn.IsValid, Is.False);
        }
示例#10
0
        public void When_Required_And_StringValue_Is_Null()
        {
            var customer = new Customer();

            var validator = new Required<Customer, string>();
            var context = new RuleValidatorContext<Customer, string>(customer, "Name", customer.Name, null, ValidationLevelType.Error, null);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            validator.Validate(context, null, notification);

            Assert.IsNotEmpty(notification.Errors[0].Message);
        }
示例#11
0
        public void When_Required_And_CollectionValue_Is_Empty_IsInvalid()
        {
            var customer = new Customer() {Contacts = new List<Contact>()};

            var validator = new Required<Customer, IEnumerable>();
            var context = new RuleValidatorContext<Customer, IEnumerable>(customer, "Contacts", customer.Contacts, null, ValidationLevelType.Error, null);

            var notification = new ValidationNotification();

            //Validate the validator only, return true of no error returned
            validator.Validate(context, null, notification);

            Assert.IsNotEmpty(notification.Errors[0].Message);
        }
示例#12
0
        public void Specification_WithWarn_ReturnsValidationResultAsWarn()
        {
            var spec = new CustomerSpecification();

            spec.Check(c => c.Name).Required();
            spec.Warn(c => c.Address).Required().Specification<AddressSpecification>();

            var customer = new Customer();

            var validationResults = spec.Validate(customer);

            var addressValidationResult = validationResults.Errors.Where(vr => vr.Property.Name == "Address").First();
            var nameValidationResult = validationResults.Errors.Where(vr => vr.Property.Name == "Name").First();

            Assert.That(addressValidationResult.Level == ValidationLevelType.Warn);
            Assert.That(nameValidationResult.Level == ValidationLevelType.Error);
        }
        public void CustomerContacts_Lambda_IsNotValid()
        {
            var contact1 = new Contact() { DateOfBirth = DateTime.Now.AddYears(-19) };
            var contact2 = new Contact() { DateOfBirth = DateTime.Now.AddYears(-22) };
            var customer = new Customer() { Contacts = new List<Contact> { contact1, contact2 } };

            var spec = new CustomerSpecification();

            spec.Check(
                c => from contact in c.Contacts where contact.DateOfBirth < DateTime.Now.AddYears(-20) select contact)
                .Optional()
                .ForEach(c => ((Contact)c).Active, "All contacts under age of 20 must be active.");

            List<ValidationResult> notification = spec.Validate(customer);
            Assert.IsNotEmpty(notification);
            Assert.AreEqual(1, notification.Count);
        }
        public void When_validated_with_ExplicitSpecification()
        {
            //Don't implicitly validate object graph
            ValidationCatalog.ValidateObjectGraph = false;

            var customer = new Customer { Name = "SampleCustomer", Address = new Address() { Country = new Country() {Id = "DE", Name = "Germany"}, Street = "1234 Offenbacher Strasse"} };

            //Add Specification for Customer for international addresses
            ValidationCatalog.SpecificationContainer.Add(new InternationalAddressSpecification());
            ValidationCatalog.AddSpecification<Customer>(spec => spec.Check(c => c.Address).Required().Specification<InternationalAddressSpecification>());

            //Validate Customer
            var results = ValidationCatalog.Validate(customer);

            Assert.That(results.Errors, Is.Not.Empty);

            Assert.That(results.Errors.First().NestedValdiationResults, Is.Not.Empty);
        }
        public void When_validated_with_DefaultSpecification()
        {
            //Don't implicitly validate object graph
            ValidationCatalog.ValidateObjectGraph = false;

            var customer = new Customer {Name = "SampleCustomer", Address = new Address()};

            //Add Specification for Customer and Address
            ValidationCatalog.SpecificationContainer.Add(new AddressSpecification());
            ValidationCatalog.SpecificationContainer.Add(new CustomerAddressSpecification());

            //Validate Customer
            var results = ValidationCatalog.Validate(customer);

            Assert.That(results.Errors, Is.Not.Empty);

            Assert.That(results.Errors.First().NestedValdiationResults, Is.Not.Empty);
        }
        public void SpecificationExpression()
        {
            var customer = new Customer {
                Name = "SampleCustomer",
                Contacts = new List<Contact>()
                {
                    new Contact() {LastName = "Smith"}
                },
                Address = new Address()
                {
                    Country = new Country(){Id = "DE", Name = "Germany"}, Street = "1234 Offenbacher Strasse"}
                };

            ValidationCatalog.SpecificationContainer.Add(new CustomerAddressSpecification());

            var results = ValidationCatalog.Validate(customer);

            Assert.That(results.Errors, Is.Not.Empty);
        }
        public void CustomerName_RequiredAndNotMinLength_InvalidLength_IsNotValid()
        {
            var customer = new Customer { Name = string.Empty.PadLeft(105, 'X') };

            var spec = new CustomerSpecification();
            spec.Check(cust => cust.Name).Required().Not.MinLength(100);

            List<ValidationResult> notification = spec.Validate(customer);
            Assert.IsNotEmpty(notification);
            Assert.AreEqual(1, notification.Count);
        }
        public void PastCustomerPromotionDate_IsInFuture_IsNotValid()
        {
            var customer = new Customer() { PromotionDate = DateTime.Now.AddDays(-1) };

            var spec = new CustomerSpecification();
            spec.Check(c => c.PromotionDate).Optional().IsInFuture();

            List<ValidationResult> notification = spec.Validate(customer);
            Assert.That(notification, Is.Not.Empty);
        }
        public void When_Customer_Contacts_IsInitializeButEmpty_And_DefinedRequired_IsInvalid()
        {
            var customer = new Customer { Contacts = new List<Contact>() };

            var spec = new CustomerSpecification();
            spec.Check(cust => cust.Contacts).Required();

            List<ValidationResult> notifications = spec.Validate(customer);

            Assert.IsNotEmpty(notifications);
        }
        public void CustomerName_Required_IsNotValid()
        {
            var customer = new Customer();

            var spec = new CustomerSpecification();
            spec.Check(cust => cust.Name).Required();

            List<ValidationResult> notification = spec.Validate(customer);
            Assert.IsNotEmpty(notification);
        }
示例#21
0
        public void ValidateConcurrently()
        {
            ValidationCatalog.AddSpecification<Customer>(s => s.Check(c => c.Name).Required().MaxLength(50) );
            Customer customer1 = new Customer() { Name = string.Empty.PadLeft(55, 'X') };
            Customer customer2 = new Customer() { Name = string.Empty.PadLeft(45, 'X') };

            var childThread = new Thread(() =>
            {
                var customer1Notification = ValidationCatalog.Validate(customer1);
                Assert.IsFalse(customer1Notification.IsValid);
            });

            childThread.Start();

            var customer2Notification = ValidationCatalog.Validate(customer2);
            Assert.IsTrue(customer2Notification.IsValid);

            childThread.Join();
        }