Ejemplo n.º 1
0
 /// <summary>
 /// Facilitates Registering a RuleValidator on the PropertyValidator
 /// </summary>
 /// <param name="validator"><see cref="RuleValidator&lt;T, TProperty&gt;"/></param>
 /// <returns><see cref="IRuleBuilder&lt;T, TProperty&gt;"/></returns>
 IRuleBuilder <T, TProperty> IRuleBuilder <T, TProperty> .RegisterValidator(RuleValidator <T, TProperty> validator)
 {
     validator.Negate = _negate;
     if (OrNextRule)
     {
         _propertyValidator.OrRule(validator);
         OrNextRule = false;
     }
     else
     {
         _propertyValidator.AndRule(validator);
     }
     return(this);
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }