public static ValidateTarget <TValue?> IsDefault <TValue>([ValidatedNotNull] this ValidateTarget <TValue?> target, Func <string> getErrorMessage = null)
            where TValue : struct
        {
            // For nullable struct, the default value is null.
            if (target.Value.HasValue)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeEqualTo(target, null));
            }

            return(target);
        }
        public static ValidateTarget <float> IsDefault([ValidatedNotNull] this ValidateTarget <float> target, float allowedError, Func <string> getErrorMessage = null)
        {
            float defaultValue = default(float);
            var   diff         = Math.Abs(target.Value - defaultValue);

            if (diff > allowedError)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeEqualTo(target, defaultValue));
            }

            return(target);
        }
        public static ValidateTarget <TValue> IsDefault <TValue>([ValidatedNotNull] this ValidateTarget <TValue> target, Func <string> getErrorMessage = null)
            where TValue : class
        {
            // Default value of all reference types is null.
            TValue defaultValue = default(TValue);

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

            return(target);
        }
        public static ValidateTarget <TValue> IsDefault <TValue>([ValidatedNotNull] this ValidateTarget <TValue> target, Func <string> getErrorMessage = null, IEqualityComparer <TValue> customComparer = null)
            where TValue : struct
        {
            TValue defaultValue = default(TValue);
            IEqualityComparer <TValue> comparer = customComparer ?? EqualityComparer <TValue> .Default;

            if (!comparer.Equals(target.Value, defaultValue))
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeEqualTo(target, defaultValue));
            }

            return(target);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        public static ValidateTarget <string> Equal([ValidatedNotNull] this ValidateTarget <string> target, string valueToCompare, Func <string> getErrorMessage = null, StringComparison stringComparison = StringComparison.Ordinal)
        {
            // string.Compare can handle null.
            if (string.Compare(target.Value, valueToCompare, stringComparison) != 0)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeEqualTo(target, valueToCompare));
            }

            return(target);
        }
Esempio n. 7
0
        public static ValidateTarget <double> Equal([ValidatedNotNull] this ValidateTarget <double> target, double valueToCompare, double allowedError, Func <string> getErrorMessage = null)
        {
            var diff = Math.Abs(target.Value - valueToCompare);

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

            return(target);
        }
        public static ValidateTarget <TValue?> IsZero <TValue>([ValidatedNotNull] this ValidateTarget <TValue?> target, Func <string> getErrorMessage = null)
            where TValue : struct, IEquatable <TValue>
        {
            TValue defaultValue = default(TValue);

            if (!target.Value.HasValue || !target.Value.Equals(defaultValue))
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeEqualTo(target, defaultValue));
            }

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

            double defaultValue = default(double);

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

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

            return(target);
        }