private static ValidationCheckResult CheckRangeBetweenDateTime(ValidationCheckParameters parameters, PropertyInfo propertyInfoFrom, PropertyInfo propertyInfoTo)
        {
            var value     = parameters.PropertyValue as DateTime? ?? DateTime.MinValue;
            var valueFrom = propertyInfoFrom.GetValue(parameters.Model) as DateTime?;
            var valueTo   = propertyInfoTo.GetValue(parameters.Model) as DateTime?;

            if (BaseRangeValidation.CheckRangeValue(valueFrom, valueTo, false, value))
            {
                return(new ValidationCheckResult {
                    IsValid = true
                });
            }

            return(new ValidationCheckResult {
                ValidationError = $"{nameof(CheckRangeBetween)}->{parameters.PropertyInfo.Name}->{propertyInfoFrom.Name}-and-{propertyInfoTo.Name}->CheckRangeValueDateTimeValue"
            });
        }
Esempio n. 2
0
        private static ValidationCheckResult CheckRangeFromOrTo <T>(ValidationCheckParameters parameters, bool invertProperties, [CallerMemberName] string memberName = null) where T : AbstractRangeFromOrToAttribute
        {
            var propertyInfoTarget = parameters.PropertyInfo;
            var rangeSource        = Attribute.GetCustomAttribute(propertyInfoTarget, typeof(T)) as AbstractRangeFromOrToAttribute;
            var dataType           = parameters.PropertyInfo.GetDataType();

            if (rangeSource == null)
            {
                return(new ValidationCheckResult {
                    IsValid = true
                });
            }

            //Determining the proterty for the start value of the value range
            var propertiesSource = rangeSource.PropertyName.Contains(",") ? rangeSource.PropertyName.Split(',') : new[] { rangeSource.PropertyName };
            var results          = new List <ValidationCheckResult>();

            foreach (var propertySource in propertiesSource)
            {
                var propertyInfoSource = parameters.DataType.GetProperty(propertySource.Trim());

                if (propertyInfoSource == null)
                {
                    results.Add(new ValidationCheckResult {
                        ValidationError = $"{memberName}->{propertyInfoTarget.Name}->propertyInfoFromIsNull"
                    });

                    continue;
                }

                var dataTypeFrom = propertyInfoSource.PropertyType.GetDataType();

                //Check whether the data types match
                if (dataType != dataTypeFrom)
                {
                    results.Add(new ValidationCheckResult {
                        ValidationError = $"{memberName}->{propertyInfoTarget.Name}->{propertyInfoSource.Name}->DataTypesNotEqual"
                    });

                    continue;
                }

                parameters.AllowNull = rangeSource.AllowNull;

                if (invertProperties)
                {
                    results.Add(BaseRangeValidation.CheckRangeValue(parameters, propertyInfoTarget, propertyInfoSource));
                }
                else
                {
                    results.Add(BaseRangeValidation.CheckRangeValue(parameters, propertyInfoSource, propertyInfoTarget));
                }
            }

            if (results.All(result => result.IsValid))
            {
                return(results.First());
            }

            return(new ValidationCheckResult {
                ValidationError = String.Join(Environment.NewLine, results.Where(result => !result.IsValid).Select(result => result.ValidationError))
            });
        }
Esempio n. 3
0
        private static ValidationCheckResult CheckRangeFromOrTo <T>(ValidationCheckParameters parameters, bool invertProperties) where T : AbstractRangeFromOrToAttribute
        {
            var propertyInfoTarget = parameters.PropertyInfo;
            var rangeSource        = Attribute.GetCustomAttribute(propertyInfoTarget, typeof(T)) as AbstractRangeFromOrToAttribute;
            var dataType           = parameters.PropertyInfo.GetDataType();

            if (rangeSource == null)
            {
                return(new ValidationCheckResult());
            }

            //Determining the property for the start value of the value range
            var propertiesSource = rangeSource.PropertyName.Contains(",") ? rangeSource.PropertyName.Split(',') : new[] { rangeSource.PropertyName };
            var results          = new List <ValidationCheckResultEntry>();

            foreach (var propertySource in propertiesSource)
            {
                var propertyInfoSource = parameters.DataType.GetProperty(propertySource.Trim());

                if (propertyInfoSource == null)
                {
                    results.Add(GetErrorResult(ValidationErrorType.PropertyNotFoundFrom, propertySource.Trim(), propertyInfoTarget.Name));

                    continue;
                }

                var dataTypeFrom = propertyInfoSource.PropertyType.GetDataType();

                //Check whether the data types match
                if (dataType != dataTypeFrom)
                {
                    results.Add(GetErrorResult(ValidationErrorType.DataTypesNotEqual, propertyInfoSource.Name, propertyInfoTarget.Name));

                    continue;
                }

                parameters.AllowNull = rangeSource.AllowNull;

                if (invertProperties)
                {
                    var result = BaseRangeValidation.CheckRangeValue(parameters, propertyInfoTarget, propertyInfoSource);
                    if (!result.IsValid)
                    {
                        results.AddRange(result.ValidationErrors);
                    }
                }
                else
                {
                    var result = BaseRangeValidation.CheckRangeValue(parameters, propertyInfoSource, propertyInfoTarget);
                    if (!result.IsValid)
                    {
                        results.AddRange(result.ValidationErrors);
                    }
                }
            }

            if (results.Count == 0)
            {
                return(new ValidationCheckResult());
            }

            return(new ValidationCheckResult {
                ValidationErrors = results
            });
        }