Пример #1
0
        public static ValidateTarget <TCollection> CountNotInRange <TCollection>([ValidatedNotNull] this ValidateTarget <TCollection> target, int min, int max, Func <string> getErrorMessage = null)
            where TCollection : IEnumerable
        {
            bool isValidationFailed = true;

            int collectionCount = 0;

            if (target.Value == null)
            {
                isValidationFailed = false;
            }
            else
            {
                collectionCount = CollectionProxy <TCollection> .GetCount(target.Value);

                if (collectionCount < min || collectionCount > max)
                {
                    isValidationFailed = false;
                }
            }

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

            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);
        }
Пример #3
0
        /// <summary>
        /// Create error message for "ShouldBeType".
        /// </summary>
        /// <typeparam name="T">Target type.</typeparam>
        public static string ShouldBeType <T>([ValidatedNotNull] ValidateTarget <T> target, Type valueToCompare)
        {
            if (valueToCompare == null)
            {
                throw new ArgumentNullException(nameof(valueToCompare));
            }

            return(CreateErrorMessage(target, valueToCompare.FullName, "{0} is not an object of type {1}."));
        }
Пример #4
0
        public static ValidateTarget <double?> NotInfinity([ValidatedNotNull] this ValidateTarget <double?> target, Func <string> getErrorMessage = null)
        {
            if (target.Value.HasValue && double.IsInfinity(target.Value.Value))
            {
                ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeInfinity(target));
            }

            return(target);
        }
Пример #5
0
        public static ValidateTarget <string> LengthNotInRange([ValidatedNotNull] this ValidateTarget <string> target, int min, int max, Func <string> getErrorMessage = null)
        {
            if (target.Value != null && target.Value.Length >= min && target.Value.Length <= max)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotHaveLengthInRange(target, min, max));
            }

            return(target);
        }
        public static ValidateTarget <string> Contains([ValidatedNotNull] this ValidateTarget <string> target, string valueToCompare, Func <string> getErrorMessage = null, StringComparison stringComparison = StringComparison.Ordinal)
        {
            if (target.Value == null || target.Value.IndexOf(valueToCompare, stringComparison) < 0)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldContain(target, valueToCompare));
            }

            return(target);
        }
Пример #7
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);
        }
Пример #8
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 <string> NotEmpty([ValidatedNotNull] this ValidateTarget <string> target, Func <string> getErrorMessage = null)
        {
            if (target.Value != null && target.Value.Length == 0)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeEmpty(target));
            }

            return(target);
        }
Пример #10
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 <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);
        }
Пример #12
0
        public static ValidateTarget <TValue?> NotNull <TValue>([ValidatedNotNull] this ValidateTarget <TValue?> target, Func <string> getErrorMessage = null)
            where TValue : struct
        {
            if (!target.Value.HasValue)
            {
                ExceptionFactory.ThrowException(target.Traits.ObjectNullExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeNull(target));
            }

            return(target);
        }
        public static ValidateTarget <TCollection> Contains <TCollection, TItem>([ValidatedNotNull] this ValidateTarget <TCollection> target, TItem valueToCompare, Func <string> getErrorMessage = null)
            where TCollection : IEnumerable <TItem>
        {
            if (target.Value == null || !TypedCollectionProxy <TCollection, TItem> .Contains(target.Value, valueToCompare))
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldContain(target, valueToCompare));
            }

            return(target);
        }
Пример #14
0
        public static ValidateTarget <string> LengthMax([ValidatedNotNull] this ValidateTarget <string> target, int valueToCompare, Func <string> getErrorMessage = null)
        {
            // As other string API does in C#, null is smaller than empty, so the length of null is not 0.
            if (target.Value == null || target.Value.Length > valueToCompare)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldHaveMaxLength(target, valueToCompare));
            }

            return(target);
        }
Пример #15
0
        public static ValidateTarget <TValue?> DoesNotHaveFlag <TValue>([ValidatedNotNull] this ValidateTarget <TValue?> target, TValue valueToComapre, Func <string> getErrorMessage = null)
            where TValue : struct, Enum
        {
            if (target.Value.HasValue && target.Value.Value.HasFlag(valueToComapre))
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotHaveFlag(target, valueToComapre));
            }

            return(target);
        }
Пример #16
0
        public static ValidateTarget <string> NotEqual([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.ShouldNotBeEqualTo(target, valueToCompare));
            }

            return(target);
        }
Пример #17
0
        public static ValidateTarget <TValue> IsNull <TValue>([ValidatedNotNull] this ValidateTarget <TValue> target, Func <string> getErrorMessage = null)
            where TValue : class
        {
            if (target.Value != null)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeNull(target));
            }

            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 <TCollection> CountMax <TCollection>([ValidatedNotNull] this ValidateTarget <TCollection> target, int valueToCompare, Func <string> getErrorMessage = null)
            where TCollection : IEnumerable
        {
            if (target.Value == null || CollectionProxy <TCollection> .GetCount(target.Value) > valueToCompare)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldHaveMaxCount(target, valueToCompare));
            }

            return(target);
        }
        public static ValidateTarget <TCollection> NotEmpty <TCollection>([ValidatedNotNull] this ValidateTarget <TCollection> target, Func <string> getErrorMessage = null)
            where TCollection : IEnumerable
        {
            if (target.Value != null && CollectionProxy <TCollection> .GetCount(target.Value) == 0)
            {
                ExceptionFactory.ThrowException(target.Traits.GenericFailureExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeEmpty(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 <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 <double> IsGreaterOrEqualThan([ValidatedNotNull] this ValidateTarget <double> target, double valueToCompare, double 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.ShouldBeGreaterOrEqualThan(target, valueToCompare));
            }

            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);
        }
Пример #25
0
        public static ValidateTarget <TValue> Equal <TValue>([ValidatedNotNull] this ValidateTarget <TValue> target, TValue valueToCompare, Func <string> getErrorMessage = null, IEqualityComparer <TValue> customComparer = null)
        {
            IEqualityComparer <TValue> comparer = customComparer ?? EqualityComparer <TValue> .Default;

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

            return(target);
        }
Пример #26
0
        public static ValidateTarget <float> Equal([ValidatedNotNull] this ValidateTarget <float> target, float valueToCompare, float 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?> 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> NotInRange <TValue>([ValidatedNotNull] this ValidateTarget <TValue> target, object minValue, object maxValue, 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, minValue) >= 0 && comparer.Compare(target.Value, maxValue) <= 0)
            {
                ExceptionFactory.ThrowException(target.Traits.OutOfRangeExceptionType, getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeInRange(target, minValue, maxValue));
            }

            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> NotInRange([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.ShouldNotBeInRange(target, minValue, maxValue));
            }

            return(target);
        }