/// <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); }
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); }
/// <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); }
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)); }
/// <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()); }
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)); }