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 errorMessage = messageService.GetDefaultMessageAndFormat(new MessageContext(context, ruleValidator.GetType(), false, null, null), ruleValidator.Parameters);

            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
        }
示例#2
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, null);

            return context;
        }
        public void Advanced()
        {
            var contact1 = new Contact() { FirstName = "Something", LastName = "Else"};

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

            Assert.That(results.IsValid, Is.False);
        }
        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, null);
            return context;
        }
        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);
        }
        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!");
        }
        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);
        }
 public RuleValidatorContext<Contact, string> BuildContext(string value)
 {
     var contact = new Contact { FirstName = value };
     var context = new RuleValidatorContext<Contact, string>(contact, "First Name", contact.FirstName, null, null);
     return context;
 }
        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.AreNotEqual(2, objectNotification.Errors);

            // Validation contact.LastName should result with only one error.
            var propertyNotification = ValidationCatalog.ValidateProperty(contact, c => c.Addresses);
            Assert.IsFalse(propertyNotification.IsValid);
            Assert.AreNotEqual(1, propertyNotification.Errors);
        }
        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);
        }
        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));
        }
        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);
               });
        }