public static ValidateTarget <TValue> IsLessOrEqualThan <TValue>([ValidatedNotNull] this ValidateTarget <TValue> target, object valueToCompare, Func <string> getErrorMessage = null, System.Collections.IComparer customComparer = null) where TValue : IComparable { System.Collections.IComparer comparer = customComparer ?? Comparer <TValue> .Default; if (comparer.Compare(target.Value, valueToCompare) > 0) { ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeLessOrEqualThan(target, valueToCompare)); } return(target); }
public static ValidateTarget <TValue?> IsLessOrEqualThan <TValue>([ValidatedNotNull] this ValidateTarget <TValue?> target, TValue valueToCompare, Func <string> getErrorMessage = null, IComparer <TValue> customComparer = null) where TValue : struct, IComparable <TValue> { if (target.Value.HasValue) { IComparer <TValue> comparer = customComparer ?? Comparer <TValue> .Default; if (comparer.Compare(target.Value.Value, valueToCompare) > 0) { ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeLessOrEqualThan(target, valueToCompare)); } } return(target); }
public static ValidateTarget <double?> IsLessOrEqualThan([ValidatedNotNull] this ValidateTarget <double?> target, double valueToCompare, double allowedError, Func <string> getErrorMessage = null) { if (target.Value.HasValue) { var diff = Math.Abs(target.Value.Value - valueToCompare); if (diff > allowedError && target.Value > valueToCompare) { ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeLessOrEqualThan(target, valueToCompare)); } } return(target); }