Example #1
0
        public static ValidateTarget <T?> NotNullArgument <T>([ValidatedNotNull] T?targetValue, string targetName)
            where T : struct
        {
            if (!targetValue.HasValue)
            {
                ExceptionFactory.ThrowException(typeof(ArgumentNullException), ErrorMessageFactory.ShouldNotBeNull(targetName));
            }

            return(ArgumentValidateTargetFactory.Create(targetValue, targetName));
        }
Example #2
0
        public static ValidateTarget <TValue?> IsNegative <TValue>([ValidatedNotNull] this ValidateTarget <TValue?> target, Func <string> getErrorMessage = null, IComparer <TValue> customComparer = null)
            where TValue : struct, IComparable <TValue>
        {
            TValue             valueToCompare = default(TValue);
            IComparer <TValue> comparer       = customComparer ?? Comparer <TValue> .Default;

            if (!target.Value.HasValue || comparer.Compare(target.Value.Value, valueToCompare) >= 0)
            {
                ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeLessThan(target, valueToCompare));
            }

            return(target);
        }
        public static ValidateTarget <TValue> IsGreaterOrEqualThan <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.ShouldBeGreaterOrEqualThan(target, valueToCompare));
            }

            return(target);
        }
        public static ValidateTarget <TCollection> CountMaxByEnumeration <TCollection>([ValidatedNotNull] this ValidateTarget <TCollection> target, int valueToCompare, Func <string> getErrorMessage = null)
            where TCollection : IEnumerable
        {
            if (target.Value == null || CollectionProxy <TCollection> .GetCountByEnumeration(target.Value, valueToCompare + 1) > valueToCompare)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldHaveMaxCount(target, valueToCompare));
            }

            return(target);
        }
Example #5
0
        public static ValidateTarget <TSet> NotProperSupersetOf <TSet, TItem>([ValidatedNotNull] this ValidateTarget <TSet> target, IEnumerable <TItem> valueToCompare, Func <string> getErrorMessage = null)
            where TSet : ISet <TItem>
        {
            if (target.Value != null)
            {
                if (target.Value.IsProperSupersetOf(valueToCompare))
                {
                    ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeProperSupersetOf(target));
                }
            }

            return(target);
        }
        private static ValidateTarget <TValue> HasAnyBitsSet <TValue>([ValidatedNotNull] this ValidateTarget <TValue> target, TValue valueToCompare, Func <string> getErrorMessage = null)
            where TValue : struct, IComparable <TValue>, IEquatable <TValue>
        {
            if (IntegerProxy <TValue> .BitwiseAnd(target.Value, valueToCompare).CompareTo(default(TValue)) == 0)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldHaveAnyBitsSet(target, valueToCompare));
            }

            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);
        }
Example #8
0
        public static ValidateTarget <double> IsNaN([ValidatedNotNull] this ValidateTarget <double> target, Func <string> getErrorMessage = null)
        {
            if (!double.IsNaN(target.Value))
            {
                ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeNaN(target));
            }

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

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

            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 : 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 <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);
        }
        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);
        }
Example #14
0
        public static ValidateTarget <TDictionary> DoesNotContainKey <TDictionary, TKey, TValue>([ValidatedNotNull] this ValidateTarget <TDictionary> target, TKey valueToCompare, Func <string> getErrorMessage = null)
            where TDictionary : IDictionary <TKey, TValue>
        {
            if (target.Value != null && target.Value.ContainsKey(valueToCompare))
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotContain(target, valueToCompare));
            }

            return(target);
        }
Example #15
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);
        }
Example #16
0
        public static ValidateTarget <string> DoesNotStartWith([ValidatedNotNull] this ValidateTarget <string> target, string valueToCompare, Func <string> getErrorMessage = null, StringComparison stringComparison = StringComparison.Ordinal)
        {
            if (target.Value != null && target.Value.StartsWith(valueToCompare, stringComparison))
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotStartWith(target, valueToCompare));
            }

            return(target);
        }
        public static ValidateTarget <bool?> NotTrue([ValidatedNotNull] this ValidateTarget <bool?> target, Func <string> getErrorMessage = null)
        {
            if (target.Value.HasValue && target.Value.Value)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeTrue(target));
            }

            return(target);
        }
        public static ValidateTarget <float> NotNegativeInfinity([ValidatedNotNull] this ValidateTarget <float> target, Func <string> getErrorMessage = null)
        {
            if (float.IsNegativeInfinity(target.Value))
            {
                ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeNegativeInfinity(target));
            }

            return(target);
        }
Example #19
0
        public static ValidateTarget <string> NotWhiteSpace([ValidatedNotNull] this ValidateTarget <string> target, Func <string> getErrorMessage = null)
        {
            if (target.Value != null && string.IsNullOrWhiteSpace(target.Value))
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeWhiteSpace(target));
            }

            return(target);
        }
        public static ValidateTarget <double> IsInRange([ValidatedNotNull] this ValidateTarget <double> target, double minValue, double maxValue, double allowedError, Func <string> getErrorMessage = null)
        {
            var diffFromMin = Math.Abs(target.Value - minValue);
            var diffFromMax = Math.Abs(target.Value - maxValue);

            if ((diffFromMin > allowedError && target.Value < minValue) || (diffFromMax > allowedError && target.Value > maxValue))
            {
                ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeInRange(target, minValue, maxValue));
            }

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

            if (diff <= allowedError || target.Value <= valueToCompare)
            {
                ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeGreaterThan(target, valueToCompare));
            }

            return(target);
        }
        public static ValidateTarget <double?> NotInRange([ValidatedNotNull] this ValidateTarget <double?> target, double minValue, double maxValue, double allowedError, Func <string> getErrorMessage = null)
        {
            if (target.Value.HasValue)
            {
                var diffFromMin = Math.Abs(target.Value.Value - minValue);
                var diffFromMax = Math.Abs(target.Value.Value - maxValue);
                if ((diffFromMin < allowedError || target.Value.Value >= minValue) && (diffFromMax < allowedError || target.Value.Value <= maxValue))
                {
                    ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeInRange(target, minValue, maxValue));
                }
            }

            return(target);
        }
        public static ValidateTarget <TValue> NotSameAs <TValue>([ValidatedNotNull] this ValidateTarget <TValue> target, TValue valueToCompare, Func <string> getErrorMessage = null)
            where TValue : class
        {
            if (object.ReferenceEquals(target.Value, valueToCompare))
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeSame(target, valueToCompare));
            }

            return(target);
        }
        public static ValidateTarget <TValue?> IsInRange <TValue>([ValidatedNotNull] this ValidateTarget <TValue?> target, object minValue, object maxValue, Func <string> getErrorMessage = null, System.Collections.IComparer customComparer = null)
            where TValue : struct, IComparable
        {
            if (target.Value.HasValue)
            {
                System.Collections.IComparer comparer = customComparer ?? Comparer <TValue> .Default;
                if (comparer.Compare(target.Value.Value, minValue) < 0 || comparer.Compare(target.Value.Value, maxValue) > 0)
                {
                    ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeInRange(target, minValue, maxValue));
                }
            }

            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);
        }
        public static ValidateTarget <TValue> IsInRange <TValue>([ValidatedNotNull] this ValidateTarget <TValue> target, TValue minValue, TValue maxValue, Func <string> getErrorMessage = null, IComparer <TValue> customComparer = null)
            where TValue : IComparable <TValue>
        {
            IComparer <TValue> comparer = customComparer ?? Comparer <TValue> .Default;

            if (comparer.Compare(target.Value, minValue) < 0 || comparer.Compare(target.Value, maxValue) > 0)
            {
                ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeInRange(target, minValue, maxValue));
            }

            return(target);
        }
        public static ValidateTarget <double?> IsGreaterOrEqualThan([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.ShouldBeGreaterOrEqualThan(target, valueToCompare));
                }
            }

            return(target);
        }
        public static ValidateTarget <TCollection> NotEmptyByEnumeration <TCollection>([ValidatedNotNull] this ValidateTarget <TCollection> target, Func <string> getErrorMessage = null)
            where TCollection : IEnumerable
        {
            if (target.Value != null && CollectionProxy <TCollection> .GetCountByEnumeration(target.Value, 1) == 0)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeEmpty(target));
            }

            return(target);
        }
        public static ValidateTarget <TValue?> IsGreaterOrEqualThan <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.ShouldBeGreaterOrEqualThan(target, valueToCompare));
                }
            }

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

            if (diff <= allowedError || target.Value >= valueToCompare)
            {
                ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeLessThan(target, valueToCompare));
            }

            return(target);
        }