public void PropertyValidator_returns_validation_errors_if_primitive_property_value_is_not_valid()
        {
            var mockValidator = new Mock <IValidator>();

            mockValidator
            .Setup(v => v.Validate(It.IsAny <EntityValidationContext>(), It.IsAny <InternalMemberEntry>()))
            .Returns(() => new[] { new DbValidationError("Name", "error") });

            var propertyValidator = new PropertyValidator(
                "Name",
                new[] { mockValidator.Object });

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(
                new Dictionary <string, object>
            {
                { "Name", "" }
            });

            var results = propertyValidator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Member("Name"));

            ValidationErrorHelper.VerifyResults(
                new[] { new Tuple <string, string>("Name", "error") },
                results);
        }
Ejemplo n.º 2
0
        public IDisposable SetupValidation <TProperty>(string name, Func <T, TProperty, string> validateFunc)
        {
            var property  = (IProperty <TProperty>)_container.PropertyStore.GetProperty(name);
            var validator = new PropertyValidator <TProperty>(this, property, validateFunc);

            validator.Validate();

            return(validator);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Force the object to validate itself using the assigned business rules.
        /// </summary>
        /// <param name="propertyName">Name of the property to validate.</param>
        public void Validate(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return;
            }
            Validator propertyValidator = new PropertyValidator(this, propertyName);

            _validationResults = propertyValidator.Validate(this);
        }
Ejemplo n.º 4
0
        public virtual IEnumerable <DbValidationError> GetValidationErrors()
        {
            ValidationProvider validationProvider = this.InternalEntityEntry.InternalContext.ValidationProvider;
            PropertyValidator  propertyValidator  = validationProvider.GetPropertyValidator(this._internalEntityEntry, this);

            if (propertyValidator == null)
            {
                return(Enumerable.Empty <DbValidationError>());
            }
            return(propertyValidator.Validate(validationProvider.GetEntityValidationContext(this._internalEntityEntry, (IDictionary <object, object>)null), this));
        }
Ejemplo n.º 5
0
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var validatable = _selector((TInstance)context.Instance);
            var faults      = _validator.Validate(validatable);

            if (faults.Count <= 0)
            {
                return(true);
            }
            var message = string.Join(Environment.NewLine, faults.Select(x => x.Message).ToArray());

            context.MessageFormatter.AppendArgument("ValidationMessage", message);
            return(false);
        }
Ejemplo n.º 6
0
        public void IfTest_trials()
        {
            PropertyValidator <Row> validator = PropertyValidator <Row> .For(x => x.DecimalValue);

            validator.If(x => x.Key == "P", p => p.IsNotNull().TryParseDecimal());

            validator.Validate(new Row()
            {
                Key = "Z"
            });
            Assert.True(validator.IsValid);

            validator.Validate(new Row()
            {
                Key = "P"
            });
            Assert.False(validator.IsValid);

            validator.Validate(new Row()
            {
                Key = "P", DecimalValue = "123"
            });
            Assert.True(validator.IsValid);

            validator.Validate(new Row()
            {
                Key = "P", DecimalValue = "123,12"
            });                                                                   // test decimal separator
            Assert.True(validator.IsValid);

            validator.Validate(new Row()
            {
                Key = "Z", DecimalValue = "123,12"
            });
            Assert.True(validator.IsValid);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Gets the <see cref="T:String"/> with the specified column name.
 /// </summary>
 /// <value></value>
 public string this[string columnName]
 {
     get
     {
         string errorDescription = string.Empty;
         if (!IsValid)
         {
             System.Text.StringBuilder errorBuilder = new System.Text.StringBuilder();
             Validator         propertyValidator    = new PropertyValidator(this, columnName);
             ValidationResults tempResults          = propertyValidator.Validate(this);
             foreach (ValidationResult vr in tempResults)
             {
                 errorBuilder.AppendLine(vr.Key);
                 errorBuilder.AppendLine(vr.Message);
             }
             errorDescription = errorBuilder.ToString();
         }
         return(errorDescription);
     }
 }
        public void PropertyValidator_does_not_return_errors_if_primitive_property_value_is_valid()
        {
            var mockValidator = new Mock <IValidator>();

            mockValidator
            .Setup(v => v.Validate(It.IsAny <EntityValidationContext>(), It.IsAny <InternalMemberEntry>()))
            .Returns(() => Enumerable.Empty <DbValidationError>());
            var propertyValidator = new PropertyValidator(
                "Name",
                new[] { mockValidator.Object });

            var mockInternalEntityEntry = Internal.MockHelper.CreateMockInternalEntityEntry(
                new Dictionary <string, object>
            {
                { "Name", "abc" }
            });

            var results = propertyValidator.Validate(
                MockHelper.CreateEntityValidationContext(mockInternalEntityEntry.Object),
                mockInternalEntityEntry.Object.Member("Name"));

            Assert.False(results.Any());
        }
Ejemplo n.º 9
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.º 10
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.º 11
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.º 12
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);
        }