Exemplo n.º 1
0
        public static ValidateTarget <float?> Equal([ValidatedNotNull] this ValidateTarget <float?> target, float valueToCompare, float allowedError, Func <string> getErrorMessage = null)
        {
            bool isValidationFailed = true;

            if (target.Value.HasValue)
            {
                var diff = Math.Abs(target.Value.Value - valueToCompare);
                if (diff <= allowedError)
                {
                    isValidationFailed = false;
                }
            }

            if (isValidationFailed)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeEqualTo(target, valueToCompare));
            }

            return(target);
        }
        public static ValidateTarget <TValue> IsType <TValue>([ValidatedNotNull] this ValidateTarget <TValue> target, Type valueToCompare, Func <string> getErrorMessage = null)
        {
            if (valueToCompare == null)
            {
                throw new ArgumentNullException(nameof(valueToCompare));
            }

            var targetType = typeof(TValue);

            if (target.Value != null)
            {
                targetType = target.Value.GetType();
            }

            if (!valueToCompare.IsAssignableFrom(targetType))
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeType(target, valueToCompare));
            }

            return(target);
        }
Exemplo n.º 3
0
        public static ValidateTarget <float?> IsNegative([ValidatedNotNull] this ValidateTarget <float?> target, float allowedError, Func <string> getErrorMessage = null)
        {
            bool isValidationFailed = true;

            float valueToCompare = 0;

            if (target.Value.HasValue)
            {
                var diff = Math.Abs(target.Value.Value - valueToCompare);
                if (diff <= allowedError || target.Value.Value >= valueToCompare)
                {
                    isValidationFailed = false;
                }
            }

            if (isValidationFailed)
            {
                ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeLessThan(target, valueToCompare));
            }

            return(target);
        }
        public static ValidateTarget <double?> NotZero([ValidatedNotNull] this ValidateTarget <double?> target, double allowedError, Func <string> getErrorMessage = null)
        {
            bool isValidationFailed = false;

            double defaultValue = default(double);

            if (target.Value.HasValue)
            {
                var diff = Math.Abs(target.Value.Value - defaultValue);
                if (diff <= allowedError)
                {
                    isValidationFailed = true;
                }
            }

            if (isValidationFailed)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeEqualTo(target, defaultValue));
            }

            return(target);
        }
        public static ValidateTarget <TCollection> UntypedAny <TCollection>([ValidatedNotNull] this ValidateTarget <TCollection> target, Func <object, bool> predicate, Func <string> getErrorMessage = null)
            where TCollection : IEnumerable
        {
            bool passedPredicate = false;

            if (target.Value != null)
            {
                foreach (var item in target.Value)
                {
                    if (predicate.Invoke(item))
                    {
                        passedPredicate = true;
                        break;
                    }
                }
            }

            if (!passedPredicate)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldHaveAny(target));
            }

            return(target);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Throw exception with specified exception type and error message.
 /// </summary>
 /// <typeparam name="T">Exception type.</typeparam>
 /// <param name="errorMessage">Error message.</param>
 public static void ThrowException <T>(string errorMessage)
     where T : Exception
 {
     ExceptionFactory.ThrowException(typeof(T), errorMessage);
 }
Exemplo n.º 7
0
 public static void NotSupported <TException>(string functionName, Func <string> getErrorMessage = null)
     where TException : Exception
 {
     ExceptionFactory.ThrowException(typeof(TException), getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeSupported(functionName));
 }
Exemplo n.º 8
0
 public static void UnreachableCode <TException>(Func <string> getErrorMessage = null)
     where TException : Exception
 {
     ExceptionFactory.ThrowException(typeof(TException), getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeUnreachable());
 }