Beispiel #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 <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);
        }
        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);
        }
        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);
        }
Beispiel #6
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);
        }
Beispiel #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);
        }
Beispiel #8
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);
        }
Beispiel #9
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);
        }
Beispiel #10
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);
        }
Beispiel #11
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> DoesNotContain <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.ShouldNotContain(target, valueToCompare));
            }

            return(target);
        }
Beispiel #13
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);
        }
        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);
        }
Beispiel #15
0
        public static void NotDisposed <TException>(bool isDisposed, string objectName, Func <string> getErrorMessage = null)
            where TException : Exception
        {
            if (!isDisposed)
            {
                return;
            }

            ExceptionFactory.ThrowException(typeof(TException), getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldNotBeDisposed(objectName));
        }
Beispiel #16
0
        public static void IsTrue <TException>(bool isValid, Func <string> getErrorMessage = null)
            where TException : Exception
        {
            if (isValid)
            {
                return;
            }

            ExceptionFactory.ThrowException(typeof(TException), getErrorMessage != null ? getErrorMessage.Invoke() : ErrorMessageFactory.ShouldBeTrueOnCustomAssertion());
        }
Beispiel #17
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);
        }
Beispiel #18
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);
        }
Beispiel #19
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);
        }
        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);
        }
Beispiel #21
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);
        }
        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> 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);
        }
Beispiel #24
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));
        }
        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);
        }
Beispiel #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 <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 <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> 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 <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);
        }