Exemplo n.º 1
0
        public AnnotationPropertiesValidator()
        {
            foreach (PropertyInfo property in typeof(T).GetProperties())
            {
                IValidationAttribute attribute = GetAnnotation <NotNullAttribute>(property);
                if (attribute != null)
                {
                    this.AddAgnosticPropertyValidator(property.PropertyType, property.Name, attribute.GetValidator(property.PropertyType));
                }

                attribute = GetAnnotation <NotEmptyAttribute>(property);
                if (attribute != null)
                {
                    this.AddAgnosticPropertyValidator(property.PropertyType, property.Name, attribute.GetValidator(property.PropertyType));
                }

                if (IsAnnotationPresent <EmailAttribute>(property))
                {
                    this.AddAgnosticPropertyValidator(property.PropertyType, property.Name, new EmailPatternValidator());
                }

                attribute = GetAnnotation <LengthAttribute>(property);
                if (IsAnnotationPresent <LengthAttribute>(property))
                {
                    this.AddAgnosticPropertyValidator(property.PropertyType, property.Name, attribute.GetValidator(property.PropertyType));
                }
            }
        }
Exemplo n.º 2
0
        public void CorrectlyReturnsValidResults(int minLength, int maxLength, object objectToValidate)
        {
            // Arrange
            sut = new StringLengthAttribute(minLength, maxLength);

            // Act
            var validationResult = sut.IsValid(objectToValidate, null);

            // Assert
            Assert.Equal(ValidationResult.Success, validationResult);
        }
Exemplo n.º 3
0
        public void CanNotBeCreatedWithNegativeLengths(int minLength, int maxLength)
        {
            // Arrange
            // Act
            var exception = Assert.ThrowsAny <ArgumentException>(() =>
            {
                sut = new StringLengthAttribute(minLength, maxLength);
            });

            // Assert
            Assert.Equal($"Range [{minLength}:{maxLength}] contains negative lengths.", exception.Message);
        }
Exemplo n.º 4
0
        public void CanNotBeCreatedWithIncorrectRange(int minLength, int maxLength)
        {
            // Arrange
            // Act
            var exception = Assert.ThrowsAny <ArgumentException>(() =>
            {
                sut = new StringLengthAttribute(minLength, maxLength);
            });

            // Assert
            Assert.Equal($"Min length ({minLength}) must be not greater than max length ({maxLength}).", exception.Message);
        }
Exemplo n.º 5
0
        public void CorrectlyReturnsInValidResults(int minLength, int maxLength, object objectToValidate)
        {
            // Arrange
            sut = new StringLengthAttribute(minLength, maxLength);

            // Act
            var validationResult = sut.IsValid(objectToValidate, ValidationContextFactory.Create());

            // Assert
            Assert.NotEqual(ValidationResult.Success, validationResult);

            Assert.Contains("The field", validationResult.ErrorMessage);
            Assert.Contains("is invalid.", validationResult.ErrorMessage);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Public method for object validation
        /// </summary>
        /// <param name="obj">Object needed to be validated</param>
        /// <returns><see cref="ValidationResult"/> object that represent the result of validation</returns>
        /// <exception cref="ValidationModel">Throws when <param name="value"></param> is equal to null</exception>
        public List <ValidationModel> Validate(T obj)
        {
            List <ValidationModel> errors = new List <ValidationModel>();

            IList <PropertyInfo> properties = obj.GetType().GetProperties
                                              (
                BindingFlags.Public
                | BindingFlags.NonPublic
                | BindingFlags.Instance
                | BindingFlags.Static
                                              );

            foreach (PropertyInfo property in properties)
            {
                IList <Attribute> attributes = property.GetCustomAttributes().ToList();

                foreach (Attribute attribute in attributes)
                {
                    IValidationAttribute customAttribute =
                        attribute as IValidationAttribute;

                    if (customAttribute != null)
                    {
                        ValidationModel result =
                            customAttribute.IsValid(property.GetValue(obj));

                        if (result.ValidationResult != ValidationResult.Success)
                        {
                            errors.Add(new ValidationModel(
                                           result.ValidationResult,
                                           result.ErrorMessage
                                           ));
                            _logger.Warn(result.ErrorMessage);
                        }
                    }
                }
            }
            return(errors);
        }
Exemplo n.º 7
0
 public UniqueRecordValidationAttribute(Type type,
                                        params object[] arguments)
 {
     DynamicInstance =
         Activator.CreateInstance(type, arguments) as IValidationAttribute;
 }