private static bool EndsWithPrivate(Argument <string> arg, string value, StringComparison comparisonType, string methodName) { InvalidMethodArgumentThrower.IfArgumentValueIsNull(arg, methodName); InvalidMethodArgumentThrower.IfArgumentOfMethodIsNull(value, nameof(value), methodName); return(arg.Value.EndsWith(value, comparisonType)); }
// todo: OnlyDigits() // todo: NotMatch(this Argument<string> arg, Regex regex) // todo: Char(5).IsDigit() private static bool ContainsPrivate(Argument <string> arg, string value, StringComparison comparisonType, string methodName) { InvalidMethodArgumentThrower.IfArgumentValueIsNull(arg, methodName); InvalidMethodArgumentThrower.IfArgumentOfMethodIsNull(value, nameof(value), methodName); return(arg.Value.IndexOf(value, comparisonType) >= 0); }
internal static bool InRange <T>(Argument <T> arg, T min, T max) where T : IComparable <T> { InvalidMethodArgumentThrower.ForRange.IfValueIsNull(max, nameof(max)); InvalidMethodArgumentThrower.ForRange.IfValueIsNull(min, nameof(min)); InvalidMethodArgumentThrower.IfNotRange(min, max); InvalidMethodArgumentThrower.ForRange.IfArgumentIsNull(arg, min, max); return(arg.Value.InRange(min, max)); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
internal static bool In <T>(Argument <T> arg, T[] values) { InvalidMethodArgumentThrower.IfArgumentIsNullForOnlyValues(values, nameof(values)); return(values.Any(x => IsEqual(arg.Value, x))); }