Exemplo n.º 1
0
        public void ValidateProperty_CollectionProperty_ReturnsValidationNotification()
        {
            //Create Rules Adhoc
            ValidationCatalog.AddSpecification<Address>(x =>
                                                            {
                                                                x.Check(a => a.Street)
                                                                    .Required()
                                                                    .MaxLength(50);
                                                            });

            ValidationCatalog.AddSpecification<Contact>(x =>
                                                            {
                                                                x.Check(c => c.Addresses).Required()
                                                                    .ForEachSpecification<Address>();
                                                                x.Check(c => c.FirstName).Required().MaxLength(100);
                                                            });

            var contact = new Contact();
            contact.Addresses = new List<Address>() {new Address()};

            // Validating contact as a whole should result in two errors.
            var objectNotification = ValidationCatalog.Validate(contact);
            Assert.IsFalse(objectNotification.IsValid);
            Assert.AreEqual(2, objectNotification.Errors.Count);

            // Validation contact.LastName should result with only one error.
            var propertyNotification = ValidationCatalog.ValidateProperty(contact, c => c.Addresses);
            Assert.IsFalse(propertyNotification.IsValid);
            Assert.AreEqual(1, propertyNotification.Errors.Count);
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        public RuleValidatorContext<Contact, bool> BuildContextForContactActive(bool value)
        {
            var contact = new Contact { Active = value };
            var context = new RuleValidatorContext<Contact, bool>(contact, "Active", contact.Active, null, ValidationLevelType.Error, null);

            return context;
        }
        public void Collection()
        {
            var contact1 = new Contact() {
                FirstName = "Something",
                LastName = "Else",
                Addresses = new List<Entities.Address>()
                                //{
                                //    new Entities.Address()B
                                //        {
                                //            Street = "Main",
                                //            City = "Someplace",
                                //            Country = new Country()
                                //                          {
                                //                              Id = "US",
                                //                              Name = "United States"
                                //                          },
                                //                          PostalCode = "12345",

                                //                          Province = "AA"
                                //        }
                                //}
            };

            var results = ValidationCatalog.Validate<ContactSpecification>(contact1);

            Assert.That(results.IsValid, Is.False);
        }
        public void Advanced()
        {
            var contact1 = new Contact() { FirstName = "Something", LastName = "Else"};

            var results = ValidationCatalog.Validate<ContactSpecification>(contact1);

            Assert.That(results.IsValid, Is.False);
        }
        public void InvalidExpression_IsInvalid()
        {
            ValidationCatalog.AddSpecification<Contact>(x => x.Check(c => c.NumberOfDependents).Required().
                                                                 GreaterThan( z=> new BadWolf().Max(z.NumberOfDependents)));

            var contact = new Contact() {LastName = "Bill"};

            var results = ValidationCatalog.Validate(contact);

            Assert.That(results.Errors, Is.Not.True);
        }
Exemplo n.º 7
0
        public RuleValidatorContext<Contact, string> BuildContextForLength(string firstName, string lastName)
        {
            if (string.IsNullOrEmpty(lastName))
            {
                lastName = "Default";
            }

            var contact = new Contact { FirstName = firstName, LastName = lastName };
            var context = new RuleValidatorContext<Contact, string>(contact, "First Name", contact.FirstName, null, ValidationLevelType.Error, null);
            return context;
        }
        public void GetMessageForRuleWithMessageOverrride()
        {
            ValidationCatalog.Configure(x => x.AddMessageStore(new ResourceMessageStore(TestRuleErrorMessages.ResourceManager), "OverrideMessages"));

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

            //Create an Entity
            var contact = new Contact();
            contact.FirstName = null;
            contact.LastName = "1111";

            var results = ValidationCatalog.ValidateProperty(contact, c => c.LastName);

            Assert.That(results.Errors.ToList().First().Message == "Last Name should only contain letters, big boy!");
        }
Exemplo n.º 9
0
        public void ValidatePropertyWithPropertyString_SimpleProperty_ReturnsValidationNotification()
        {
            //Create Rules Adhoc
            ValidationCatalog.AddSpecification<Contact>(x =>
            {
                x.Check(c => c.LastName).Required();
                x.Check(c => c.FirstName).Required();
            });

            var contact = new Contact();

            // Validating contact as a whole should result in two errors.
            var objectNotification = ValidationCatalog.Validate(contact);
            Assert.IsFalse(objectNotification.IsValid);
            Assert.AreNotEqual(2, objectNotification.Errors);

            // Validation contact.LastName should result with only one error.
            var propertyNotification = ValidationCatalog.ValidateProperty(contact,"LastName");
            Assert.IsFalse(propertyNotification.IsValid);
            Assert.AreNotEqual(1, propertyNotification.Errors);
        }
Exemplo n.º 10
0
        public void Validate_OptionalProperty_WithNoValue_IsValid()
        {
            var emptyContact = new Contact();
            emptyContact.FirstName = string.Empty;
            emptyContact.LastName = string.Empty;

            var propertyValidator =
                new PropertyValidator<Contact, string>(contact => contact.LastName);

            //add a single rule
            var lengthValidator = new LengthBetween<Contact>(1, 5);
            propertyValidator.AndRule(lengthValidator); //.Rules.Add(lengthValidator);

            var notification = new ValidationNotification();

            //Validate
            var result = propertyValidator.Validate(emptyContact, null, notification);

            Assert.That(result, Is.True);
            Assert.That(notification.Errors, Is.Empty);
        }
Exemplo n.º 11
0
        public void GetFormattedErrorMessage_ReturnsFormattedString()
        {
            //Create an Entity
            var emptyContact = new Contact();
            emptyContact.FirstName = null;
            emptyContact.LastName = null;

            //Create PropertyValidator
            var propertyValidator =
                new PropertyValidator<Contact, string>(contact => contact.LastName);

            //Create a rule
            RuleValidator<Contact, string> ruleValidator = new LengthBetween<Contact>(1, 5);

            //Create a context
            var context = new RuleValidatorContext<Contact, string>(emptyContact, propertyValidator, null);

            //create it like this? IOC? Factory?
            //IMessageStore messageStore = new ResourceMessageStore();

            //string errorMessage = messageStore.GetFormattedDefaultMessage(ruleValidator.GetType().Name, context, ruleValidator.Parameters);
            var messageService = new MessageService();

            var messageContext = new MessageContext(context, ruleValidator.GetType(), false, null, null);
            var paramValues =
                (from ruleParameter in ruleValidator.Params select (object) ruleParameter.GetParamValue())
                    .ToList();
            var errorMessage = messageService.GetDefaultMessageAndFormat(messageContext, paramValues);

            Assert.That(errorMessage, Is.Not.Null.Or.Empty);

            Assert.That(errorMessage, Is.StringContaining("Last Name"));
            Assert.That(errorMessage, Is.StringContaining("1"));
            Assert.That(errorMessage, Is.StringContaining("5"));
            //null: Search for Actual value but it's empty b/c the value is null
        }
Exemplo n.º 12
0
        public void Validate_Property_Label_Is_Available_On_Error_When_Invalid()
        {
            const string expected = "This is a custom rule label";

            ValidationCatalog.AddSpecification<Contact>(spec =>
                spec.Check(c => c.FirstName).WithLabel(expected).Required());

            var contact = new Contact();
            var vn = ValidationCatalog.Validate(contact);
            Assert.That(vn.IsValid, Is.False);

            var error = vn.Errors.Single();
            Assert.AreEqual(expected, error.Label);
        }
Exemplo n.º 13
0
 public RuleValidatorContext<Contact, string> BuildContextForLength(string value)
 {
     var contact = new Contact {FirstName = value};
     var context = new RuleValidatorContext<Contact, string>(contact, "First Name", contact.FirstName, null, ValidationLevelType.Error, null);
     return context;
 }
Exemplo n.º 14
0
        public void Validate_Property_With_PropertyNameOverrideExpression_IsValid()
        {
            var emptyContact = new Contact();
            emptyContact.FirstName = "George's last name";
            emptyContact.LastName = string.Empty;

            var propertyValidator =
                new PropertyValidator<Contact, string>(contact => contact.LastName);

            propertyValidator.PropertyNameOverrideExpression = new Func<Contact, string>( o => o.FirstName);

            //add a single rule
            var lengthValidator = new LengthBetween<Contact>(1, 5);
            propertyValidator.AndRule(lengthValidator);

            //Validate
            ValidationNotification notification = new ValidationNotification();
            propertyValidator.Validate(emptyContact, null, notification);

            Assert.That(notification.Errors, Is.Not.Empty);
        }
Exemplo n.º 15
0
        public void Validate_Collection_Using_Specified_Specification_WithoutValidateObjectGraph()
        {
            //Build test data
            var validContact = new Contact() { FirstName = "Johnny B", LastName = "Good" };
            var invalidContact = new Contact() { FirstName = "Baddy" };

            var contacts = new List<Contact>() { validContact, invalidContact };

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

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

            Assert.That(results.Errors.Count, Is.AtLeast(1));
        }
Exemplo n.º 16
0
        public void ValidateProperty_NoValidationForProperty_ThrowsArgumentException()
        {
            //Create Rules Adhoc
            ValidationCatalog.AddSpecification<Contact>(x =>
            {
                x.Check(c => c.FirstName).Required();
            });

            var contact = new Contact();

            // Validation contact.LastName should result with only one error.

            Assert.Throws<ArgumentException>(
               () =>
               {
                   var propertyNotification = ValidationCatalog.ValidateProperty(contact, c => c.LastName);
               });
        }
Exemplo n.º 17
0
        public void ValidationContainer_Initialize()
        {
            //Create Rules Adhoc
            ValidationCatalog.AddSpecification<Contact>(x =>
                                                              {
                                                                  x.Check(contact => contact.LastName).Required();
                                                                  x.Check(contact => contact.FirstName).Required();
                                                                  x.Check(contact => contact.DateOfBirth).Optional()
                                                                      .GreaterThan(
                                                                      new DateTime(1950, 1, 1));
                                                              });

            //Dummy Contact
            var emptyContact = new Contact();
            emptyContact.FirstName = null;
            emptyContact.LastName = null;

            //Validate
            ValidationNotification notification = ValidationCatalog.Validate(emptyContact);

            Assert.That(notification.Errors, Is.Not.Empty);
        }