コード例 #1
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if the argument is not <c>null</c>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if the argument is not <c>null</c></exception>
        public static void Null <T>(this Argument <T?> arg) where T : struct
        {
            if (arg.ValidationIsDisabled())
            {
                return;
            }

            if (arg.Value != null)
            {
                ValidationErrorExceptionThrower.ArgumentException(arg, GetMessageFotNullValidation(arg));
            }
        }
コード例 #2
0
        /// <summary>
        /// <para>
        /// Throws <see cref="ArgumentNullException"/> if the argument is <c>null</c>
        /// </para>
        /// <para>
        /// Overload for the <see cref="Nullable{T}"/> type is specifically not defined, because this type is used specifically
        /// when the argument should be able to have the value null
        /// </para>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if the argument is <c>null</c></exception>
        public static Argument <T> NotNull <T>(this Argument <T> arg) where T : class
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (arg.Value == null)
            {
                ValidationErrorExceptionThrower.ArgumentNullException(arg);
            }

            return(arg);
        }
コード例 #3
0
        /// <summary>
        /// Throw <see cref="ArgumentException"/> with message <paramref name="message"/> if <paramref name="condition"/> is <c>true</c>
        /// </summary>
        /// <exception cref="ArgumentException">If <paramref name="condition"/> is <c>true</c></exception>
        public static Argument <T> FailedIf <T>(this Argument <T> arg, bool condition, string message)
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (condition)
            {
                ValidationErrorExceptionThrower.ArgumentException(arg, message + Environment.NewLine + $"Argument name: '{arg.Name}'");
            }

            return(arg);
        }
コード例 #4
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if the argument is default value
        /// </summary>
        /// <exception cref="ArgumentException">Throws if the argument is default value</exception>
        public static Argument <T> NotDefault <T>(this Argument <T> arg)
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (ObjectConditionChecker.IsDefault(arg.Value))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg, $"Argument '{arg.Name}' must be not default value");
            }

            return(arg);
        }
コード例 #5
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if the argument is equals <paramref name="value"/>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if the argument is equals <paramref name="value"/></exception>
        public static Argument <T> NotEqual <T>(this Argument <T> arg, T value)
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (ObjectConditionChecker.IsEqual(arg.Value, value))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg, $"Argument '{arg.Name}' must be not equal '{value}'");
            }

            return(arg);
        }
コード例 #6
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if argument is ends with <paramref name="value"/>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if argument is ends with <paramref name="value"/></exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c></exception>
        public static Argument <string> NotEndsWith(this Argument <string> arg, string value, StringComparison comparisonType)
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (EndsWithPrivate(arg, value, comparisonType, methodName: nameof(NotEndsWith)))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' must not ends with {ExceptionMessageHelper.GetStringValueForMessage(value)}. Current value: {ExceptionMessageHelper.GetStringValueForMessage(arg.Value)}");
            }

            return(arg);
        }
コード例 #7
0
        /// <summary>
        /// <para>
        /// Throws <see cref="ArgumentOutOfRangeException"/> if argument is not less than <paramref name="value"/>
        /// </para>
        /// <para>
        /// Note. For validation <see cref="Nullable{T}"/> you must first call the methods
        /// <see cref="ArgumentConditionExtension.IfNotNull{T}(ArgValidation.Argument{System.Nullable{T}})"/> or <see cref="Arg.IfNotNull{T}(System.Nullable{T},string)"/>
        /// </para>
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">Throws if argument is not less than <paramref name="value"/></exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c> or <paramref name="value"/> is <c>null</c></exception>
        public static Argument <T> LessThan <T>(this Argument <T> arg, T value) where T : IComparable <T>
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (!CompatableConditionChecker.LessThan(arg, value))
            {
                ValidationErrorExceptionThrower.ArgumentOutOfRangeException(arg,
                                                                            $"Argument '{arg.Name}' must be less than '{value}'. Current value: '{arg.Value}'");
            }

            return(arg);
        }
コード例 #8
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if argument is not <c>null</c> or length is more than 0
        /// </summary>
        /// <exception cref="ArgumentException">Throws if argument is not <c>null</c> or length is more than 0</exception>
        public static Argument <string> NullOrEmpty(this Argument <string> arg)
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (!string.IsNullOrEmpty(arg.Value))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' must be empty or null. Current value: {ExceptionMessageHelper.GetStringValueForMessage(arg.Value)}");
            }

            return(arg);
        }
コード例 #9
0
        /// <summary>
        /// <para>Throws <see cref="ArgumentException"/> if the argument has <paramref name="flag"/></para>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if the argument has <paramref name="flag"/></exception>
        public static Argument <TEnum> NotHasFlag <TEnum>(this Argument <TEnum> arg, TEnum flag) where TEnum : Enum
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (arg.Value.HasFlag(flag))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' must not have the nexts flag(s): {flag}. Current value: {arg.Value}");
            }

            return(arg);
        }
コード例 #10
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if the argument is contained in <paramref name="values"/>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if the argument is contained in <paramref name="values"/></exception>
        /// <exception cref="ArgValidationException">Throws if <paramref name="values"/> is <c>null</c></exception>
        public static Argument <T> NotIn <T>(this Argument <T> arg, params T[] values)
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (ObjectConditionChecker.In(arg, values))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' can not have the following values: {values.JoinForMessage()}. Current value: {arg.Value.GetStringValueForMessage()}");
            }

            return(arg);
        }
コード例 #11
0
        /// <summary>
        /// <para>
        /// Throws <see cref="ArgumentOutOfRangeException"/> if argument is more than <paramref name="value"/>
        /// </para>
        /// <para>
        /// Note. For validation <see cref="Nullable{T}"/> you must first call the methods
        /// <see cref="ArgumentConditionExtension.IfNotNull{T}(ArgValidation.Argument{System.Nullable{T}})"/> or <see cref="Arg.IfNotNull{T}(System.Nullable{T},string)"/>
        /// </para>
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">Throws if argument is more than <paramref name="value"/></exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c> or <paramref name="value"/> is <c>null</c></exception>
        public static Argument <T> Max <T>(this Argument <T> arg, T value) where T : IComparable <T>
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (!CompatableConditionChecker.Max(arg, value))
            {
                ValidationErrorExceptionThrower.ArgumentOutOfRangeException(arg,
                                                                            $"The maximum value for the argument '{arg.Name}' is '{value}'. Current value: '{arg.Value}'");
            }

            return(arg);
        }
コード例 #12
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if argument is <c>null</c> or argument is contains only whitespaces
        /// </summary>
        /// <exception cref="ArgumentException">Throws if argument is <c>null</c> or argument is contains only whitespaces</exception>
        public static Argument <string> NotNullOrWhitespace(this Argument <string> arg)
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (string.IsNullOrWhiteSpace(arg.Value))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' cannot be empty or whitespace. Current value: {ExceptionMessageHelper.GetStringValueForMessage(arg.Value)}");
            }

            return(arg);
        }
コード例 #13
0
        /// <summary>
        /// <para>
        /// Throws <see cref="ArgumentOutOfRangeException"/> if argument is not in range <paramref name="min"/> - <paramref name="max"/>
        /// </para>
        /// <para>
        /// Note. For validation <see cref="Nullable{T}"/> you must first call the methods
        /// <see cref="ArgumentConditionExtension.IfNotNull{T}(ArgValidation.Argument{System.Nullable{T}})"/> or <see cref="Arg.IfNotNull{T}(System.Nullable{T},string)"/>
        /// </para>
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">Throws if argument is not in range <paramref name="min"/> - <paramref name="max"/></exception>
        /// <exception cref="ArgValidationException">
        /// <para>Throws in the following cases:</para>
        /// <para>- argument is <c>null</c></para>
        /// <para>- <paramref name="min"/> is <c>null</c></para>
        /// <para>- <paramref name="max"/> is <c>null</c></para>
        /// <para>- <paramref name="min"/> - <paramref name="max"/> is not range</para>
        /// </exception>
        public static Argument <T> InRange <T>(this Argument <T> arg, T min, T max) where T : IComparable <T>
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (!CompatableConditionChecker.InRange(arg, min, max))
            {
                ValidationErrorExceptionThrower.ArgumentOutOfRangeException(arg,
                                                                            $"Argument '{arg.Name}' must be in range from '{min}' to '{max}'. Current value: '{arg.Value}'");
            }

            return(arg);
        }
コード例 #14
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if argument is <c>null</c> or elements count is equals 0
        /// </summary>
        /// <exception cref="ArgumentException">Throws if argument is <c>null</c> or elements count is equals 0</exception>
        public static Argument <TEnumerable> NotNullOrEmpty <TEnumerable>(this Argument <TEnumerable> arg)
            where TEnumerable : IEnumerable
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (arg.Value == null || !arg.Value.Any())
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' cannot be null or empty. {GetCurrentValuesString(arg.Value)}");
            }

            return(arg);
        }
コード例 #15
0
        /// <summary>
        /// <para>Throws <see cref="ArgumentException"/> if the argument is not defined in enum <typeparamref name="TEnum"/>.</para>
        /// <para>Also works with flags.</para>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if the argument is not defined in enum <typeparamref name="TEnum"/></exception>
        public static Argument <TEnum> DefinedInEnum <TEnum>(this Argument <TEnum> arg)
            where TEnum : Enum
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            if (!DefinedInEnum(arg.Value))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' must be defined in enum type {typeof(TEnum).FullName}. Current value: {arg.Value}");
            }

            return(arg);
        }
コード例 #16
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if length is less than <paramref name="value"/>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if length is less than <paramref name="value"/></exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c></exception>
        public static Argument <string> MinLength(this Argument <string> arg, int value)
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            InvalidMethodArgumentThrower.IfArgumentValueIsNull(arg, methodName: nameof(MinLength));

            if (arg.Value.Length < value)
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' has a minimum length of {value}. Current length: {GetLengthValueForMessage(arg.Value)}");
            }

            return(arg);
        }
コード例 #17
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if element count is more than 0
        /// </summary>
        /// <exception cref="ArgumentException">Throws if element count is more than 0</exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c></exception>
        public static Argument <TEnumerable> Empty <TEnumerable>(this Argument <TEnumerable> arg)
            where TEnumerable : IEnumerable
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            InvalidMethodArgumentThrower.IfArgumentValueIsNull(arg, methodName: nameof(Empty));

            if (arg.Value.Any())
            {
                ValidationErrorExceptionThrower.ArgumentException(arg, $"Argument '{arg.Name}' must be empty. {GetCurrentValuesString(arg.Value)}");
            }

            return(arg);
        }
コード例 #18
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if length is not equals <paramref name="value"/>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if length is not equals <paramref name="value"/></exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c></exception>
        public static Argument <string> LengthEqual(this Argument <string> arg, int value)
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            InvalidMethodArgumentThrower.IfArgumentValueIsNull(arg, methodName: nameof(LengthEqual));

            if (arg.Value.Length != value)
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' must be length {value}. Current length: {GetLengthValueForMessage(arg.Value)}");
            }

            return(arg);
        }
コード例 #19
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if element count is not less than <paramref name="value"/>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if element count is not less than <paramref name="value"/></exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c></exception>
        public static Argument <TEnumerable> CountLessThan <TEnumerable>(this Argument <TEnumerable> arg, int value)
            where TEnumerable : IEnumerable
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            InvalidMethodArgumentThrower.IfArgumentIsNullForCount(arg);

            if (!arg.Value.CountLessThan(value))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' must contains less than {value} elements. Current count elements: {arg.Value.Count()}");
            }

            return(arg);
        }
コード例 #20
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if element count is equals <paramref name="value"/>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if element count is equals <paramref name="value"/></exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c></exception>
        public static Argument <TEnumerable> CountNotEqual <TEnumerable>(this Argument <TEnumerable> arg, int value)
            where TEnumerable : IEnumerable
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            InvalidMethodArgumentThrower.IfArgumentIsNullForCount(arg);

            if (arg.Value.CountEquals(value))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' not must contains {arg.Value.Count()} elements");
            }

            return(arg);
        }
コード例 #21
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if argument is contains <c>null</c>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if argument is contains <c>null</c></exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c></exception>
        public static Argument <TEnumerable> NotContainsNull <TEnumerable>(this Argument <TEnumerable> arg)
            where TEnumerable : IEnumerable
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            InvalidMethodArgumentThrower.IfArgumentValueIsNull(arg, methodName: nameof(NotContainsNull));

            if (arg.Value.Contains(null))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' contains null. {GetCurrentValuesString(arg.Value)}");
            }

            return(arg);
        }
コード例 #22
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if argument is contains <paramref name="elem"/>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if argument is contains <paramref name="elem"/></exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c></exception>
        public static Argument <TEnumerable> NotContains <TEnumerable, T>(this Argument <TEnumerable> arg, T elem)
            where TEnumerable : IEnumerable <T>
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            InvalidMethodArgumentThrower.IfArgumentValueIsNull(arg, methodName: nameof(NotContains));

            if (arg.Value.Contains(elem))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' contains {ExceptionMessageHelper.GetStringValueForMessage(elem)} value. {GetCurrentValuesString(arg.Value)}");
            }

            return(arg);
        }
コード例 #23
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if argument is match <paramref name="pattern"/>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if argument is match <paramref name="pattern"/></exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c> or <paramref name="pattern"/> is <c>null</c></exception>
        public static Argument <string> NotMatch(this Argument <string> arg, string pattern)
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            InvalidMethodArgumentThrower.IfArgumentValueIsNull(arg, methodName: nameof(NotMatch));
            InvalidMethodArgumentThrower.IfArgumentOfMethodIsNull(arg: pattern, argName: nameof(pattern), methodName: nameof(NotMatch));

            if (StringConditionChecker.Match(arg, pattern))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' not must be match with pattern '{pattern}'. Current value: {ExceptionMessageHelper.GetStringValueForMessage(arg.Value)}");
            }

            return(arg);
        }
コード例 #24
0
        /// <summary>
        /// Throws <see cref="ArgumentException"/> if length is not in range from <paramref name="min"/> to <paramref name="max"/>
        /// </summary>
        /// <exception cref="ArgumentException">Throws if length is not in range from <paramref name="min"/> to <paramref name="max"/></exception>
        /// <exception cref="ArgValidationException">Throws if argument is <c>null</c> or <paramref name="min"/> - <paramref name="max"/> is not range</exception>
        public static Argument <string> LengthInRange(this Argument <string> arg, int min, int max)
        {
            if (arg.ValidationIsDisabled())
            {
                return(arg);
            }

            InvalidMethodArgumentThrower.IfArgumentValueIsNull(arg, methodName: nameof(LengthInRange));
            InvalidMethodArgumentThrower.IfNotRange(min, max);

            if (!arg.Value.Length.InRange(min, max))
            {
                ValidationErrorExceptionThrower.ArgumentException(arg,
                                                                  $"Argument '{arg.Name}' must be length in range {min} - {max}. Current length: {GetLengthValueForMessage(arg.Value)}");
            }


            return(arg);
        }