public override bool Validate(IValidationEntity entity)
        {
            //Contract.Requires<ArgumentNullException>(entity != null);
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            decimal value;

            decimal.TryParse(entity.GetType().GetProperty(PropertyName).GetValue(entity).ToString(), out value);

            if (_operatorCompare == OperatorCompare.Equals)
            {
                ErrorMessage = string.Format("Значение поля {0} должно равняться {1} символам", PropertyName, _value);
                return(value == _value);
            }

            if (_operatorCompare == OperatorCompare.MoreThan)
            {
                ErrorMessage = string.Format("Значение поля {0} не должно быть менее {1} символов", PropertyName, _value);
                return(value > _value);
            }

            if (_operatorCompare == OperatorCompare.LessThan)
            {
                ErrorMessage = string.Format("Значение поля {0} не должно превышать {1} символов", PropertyName, _value);
                return(value < _value);
            }

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Validates that entity passes the length validation check
        /// </summary>
        /// <param name="entity">entity to be validated</param>
        /// <returns>Yes if all checks are successfull false or exception in other cases</returns>
        /// <exception cref="ArgumentNullException">Throws if the entity passed to the method is null</exception>
        /// <exception cref="InvalidCastException">Throws if the value is not of string type</exception>
        public override bool Validate(IValidationEntity entity)
        {
            //Contract.Requires<ArgumentNullException>(entity != null);
            if (entity == null)
            {
                throw new ArgumentNullException($"{nameof(entity)} shouldn't be null.");
            }

            var value = entity.GetType().GetProperty(PropertyName).GetValue(entity) as string;

            if (value == null)
            {
                throw new InvalidCastException("Field is not of a string type.");
            }

            if (_operatorCompare == OperatorCompare.Equals)
            {
                ErrorMessage = string.Format("{0} field length  should be equal to {1} symbols", PropertyName, _value);
                return(value.Length == _value);
            }

            if (_operatorCompare == OperatorCompare.MoreThan)
            {
                ErrorMessage = string.Format("{0} field length shouldn't be less than {1} symbols", PropertyName, _value);
                return(value.Length > _value);
            }

            if (_operatorCompare == OperatorCompare.LessThan)
            {
                ErrorMessage = string.Format("{0} field length shouldn't be greater than {1} symbols", PropertyName, _value);
                return(value.Length < _value);
            }

            return(true);
        }
Beispiel #3
0
        public override bool Validate(IValidationEntity entity)
        {
            //Contract.Requires<ArgumentNullException>(entity != null);
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            return(Regex.IsMatch(entity.GetType().GetProperty(PropertyName).GetValue(entity).ToString(), _pattern));
        }
Beispiel #4
0
        /// <summary>
        /// Validates the entity for the specified rule above.
        /// </summary>
        /// <param name="entity">Validated entity. Should Implement interface <see cref="IValidationEntity"/></param>
        /// <returns>Validation result. If yes it gives the True, false if not.</returns>
        public override bool Validate(IValidationEntity entity)
        {
            //Contract.Requires<ArgumentNullException>(entity != null);
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            return(entity.GetType().GetProperty(PropertyName).GetValue(entity) != null);
        }
Beispiel #5
0
        /// <summary>
        /// Gets the rules set for the validated entity.
        /// </summary>
        /// <param name="entity">Validated entity</param>
        /// <returns>Rule Set <see cref="{IEnumerable<BaseValidationRule>}"/></returns>
        private static IEnumerable <BaseValidationRule> GetValidationRules(IValidationEntity entity)
        {
            //Contract.Requires<ArgumentNullException>(entity != null);
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            return(entity.GetType().GetTypeInfo().GetCustomAttributes <ValidationRuleAttribute>().Select(it => it.Rule).ToList());
        }
Beispiel #6
0
        /// <summary>
        /// Validates the provided entity
        /// </summary>
        /// <param name="entity">Entity to validate.</param>
        /// <returns>Validation result.</returns>
        public static IEnumerable <ValidationErrorInfo> Validate(IValidationEntity entity)
        {
            //Contract.Requires<ArgumentNullException>(entity != null);
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            var rules = GetValidationRules(entity);

            return(rules.Where(it => !it.Validate(entity)).Select(it => new ValidationErrorInfo(it)).ToList());
        }
        public override bool Validate(IValidationEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            var value = entity.GetType().GetProperty(PropertyName).GetValue(entity) as string;

            if (value == null)
            {
                throw new InvalidCastException("Поле имеет тип отличный от строкового");
            }

            return(Regex.IsMatch(value.Length.ToString(CultureInfo.InvariantCulture), _pattern));
        }
 /// <summary>
 /// Validate an object to be compliant with the rules.
 /// </summary>
 /// <param name="entity">Entity(object) to be validated. The Entity should implement <see cref="IValidationEntity"/> interface</param>
 /// <returns><c>True</c>, если объект соответствует правилу,<c>False</c>, если не соответствует</returns>
 public abstract bool Validate(IValidationEntity entity);
Beispiel #9
0
 /// <summary>
 /// Validates the entity by the rules set.
 /// </summary>
 /// <param name="entity">Validated entity</param>
 /// <param name="rules">Validation set</param>
 /// <returns>Errors which are the validation result. The result is  <see cref="{IEnumerable<ValidationErrorInfo>}"/></returns>
 public static IEnumerable <ValidationErrorInfo> Validate(this IValidationEntity entity, IEnumerable <BaseValidationRule> rules)
 {
     return(rules.Where(it => !it.Validate(entity)).Select(it => new ValidationErrorInfo(it)).ToList());
 }