/// <summary> /// Throws <see cref="ArgumentException"/> if the input is equals to default value /// </summary> /// <typeparam name="T">type of input</typeparam> /// <param name="shieldClause">shield clause</param> /// <param name="input">input</param> /// <param name="parameterName">parameter name</param> /// <exception cref="ArgumentException"></exception> public static void Default <T>(this IShieldClause shieldClause, T input, string parameterName) { if (EqualityComparer <T> .Default.Equals(input, default(T))) { throw new ArgumentException($"Parameter {parameterName} is default value for type {typeof(T).Name}"); } }
/// <summary> /// Throws <see cref="ArgumentException"/> if the input is equals to default value /// </summary> /// <typeparam name="T">type of input</typeparam> /// <param name="shieldClause">shield clause</param> /// <param name="input">input</param> /// <param name="parameterName">parameter name</param> /// <exception cref="ArgumentException"></exception> private static void Zero <T>(this IShieldClause shieldClause, T input, string parameterName) { if (EqualityComparer <T> .Default.Equals(input, default(T))) { throw new ArgumentException($"Required input {StringUtils.FormatParameter(parameterName)} cannot be zero"); } }
/// <summary> /// Throws <see cref="ArgumentOutOfRangeException"/> when the value is greater than limit /// </summary> /// <typeparam name="T">input type</typeparam> /// <param name="shieldClause">shield clause</param> /// <param name="input">input</param> /// <param name="limit">limit</param> /// <param name="parameterName">parameter range</param> /// <param name="message">custom message</param> public static void GreaterThan <T>(this IShieldClause shieldClause, T input, T limit, string parameterName, string message) where T : IComparable <T> { if (input.CompareTo(limit) <= 0) { throw new ArgumentOutOfRangeException(StringUtils.FormatParameter(parameterName), StringUtils.FormatMessage(message)); } }
/// <summary> /// Throws <see cref="ArgumentNullException"/> if the <see cref="input"/> is null; /// </summary> /// <typeparam name="T"></typeparam> /// <param name="shieldClause">Shield clause</param> /// <param name="input">input</param> /// <param name="parameterName">name of the parameter</param> /// <exception cref="ArgumentNullException"></exception> /// <param name="message">custom message</param> public static void Null <T>(this IShieldClause shieldClause, T input, string parameterName, string message) where T : class { if (input == default(T)) { throw new ArgumentNullException(StringUtils.FormatParameter(parameterName), StringUtils.FormatMessage(message)); } }
/// <summary> /// Throws <see cref="ArgumentNullException"/> if the <see cref="input"/> is null; /// </summary> /// <param name="shieldClause">Shield clause</param> /// <param name="input">input</param> /// <param name="parameterName">name of the parameter</param> /// <param name="message">custom message</param> /// <exception cref="ArgumentNullException"></exception> public static void Null(this IShieldClause shieldClause, object input, string parameterName, string message = null) { if (input == null) { throw new ArgumentNullException(StringUtils.FormatParameter(parameterName), StringUtils.FormatMessage(message)); } }
/// <summary> /// Throws <see cref="ArgumentNullException"/> if the <see cref="input"/> is null. /// Throws <see cref="ArgumentException"/> if the <see cref="input"/> is IEnumerable<T> is empty /// </summary> /// <param name="shieldClause">shieldClause</param> /// <param name="input">input</param> /// <param name="parameterName">parameter name</param> /// <param name="customExceptionMessage">custom message</param> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentNullException"></exception> public static void NullOrEmpty <T>(this IShieldClause shieldClause, IEnumerable <T> input, string parameterName, string customExceptionMessage = null) { Shield.Against.Null(input, parameterName, customExceptionMessage); if (!input.Any()) { throw new ArgumentException($"Required input {StringUtils.FormatParameter(parameterName)} was empty. {StringUtils.FormatMessage(customExceptionMessage)}"); } }
/// <summary> /// Throws <see cref="ArgumentNullException"/> if the <see cref="input"/> is null. /// Throws <see cref="ArgumentException"/> if the <see cref="input"/> is an empty string or white space string /// </summary> /// <param name="shieldClause">shieldClause</param> /// <param name="input">input</param> /// <param name="parameterName">parameter name</param> /// <param name="message">custom message</param> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentNullException"></exception> public static void NullOrWhiteSpace(this IShieldClause shieldClause, string input, string parameterName, string message = null) { Shield.Against.Null(input, parameterName, message); if (String.IsNullOrWhiteSpace(input)) { throw new ArgumentException($"Required input {StringUtils.FormatParameter(parameterName)} was empty", StringUtils.FormatMessage(message)); } }
/// <summary> /// Throws <see cref="ArgumentNullException"/> if the <see cref="input"/> is null. /// Throws <see cref="ArgumentException"/> if the <see cref="input"/> is an empty string /// </summary> /// <param name="shieldClause">shieldClause</param> /// <param name="input">input</param> /// <param name="parameterName">parameter name</param> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentNullException"></exception> public static void NullOrEmpty(this IShieldClause shieldClause, string input, string parameterName) { Shield.Against.Null(input, parameterName); if (input == string.Empty) { throw new ArgumentException($"Required input {StringUtils.FormatParameter(parameterName)} was empty"); } }
/// <summary> /// Throws <see cref="ArgumentNullException"/> if the <see cref="input"/> is null; /// Throws <see cref="ArgumentException"/> if the input is not of specified type. /// </summary> /// <param name="shieldClause">Shield clause</param> /// <param name="input">input</param> /// <param name="parameterName">name of the parameter</param> /// <param name="customExceptionMessage">custom message</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> public static void IsTypeOf <T>(this IShieldClause shieldClause, object input, string parameterName) { Shield.Against.Null(input, parameterName); if (!(input is T)) { throw new ArgumentException($"{input.GetType().Name} is not an instance of type {typeof(T).Name}"); } }
/// <summary> /// Throws <see cref="ArgumentException"/> if <see cref="from"/> is greater than <see cref="to"/> /// Throws <see cref="ArgumentOutOfRangeException"/> if <see cref="input"/> is not with in the specified range /// </summary> /// <typeparam name="T">type of param</typeparam> /// <param name="shieldClause">shield clause</param> /// <param name="input">input</param> /// <param name="parameterName">parameter name</param> /// <param name="from">range from</param> /// <param name="to">range to</param> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> private static void OutOfRange <T>(this IShieldClause shieldClause, T input, string parameterName, T from, T to) { Comparer <T> comparer = Comparer <T> .Default; if (comparer.Compare(from, to) > 0) { throw new ArgumentException($"{nameof(from)} should be less or equal to {nameof(to)}"); } if (comparer.Compare(input, from) < 0 || comparer.Compare(input, to) > 0) { throw new ArgumentOutOfRangeException($"Input {StringUtils.FormatParameter(parameterName)} was out of range."); } }
/// <summary> /// Throws <see cref="ArgumentException"/> if the input is equals to Zero /// </summary> /// <param name="shieldClause">shield clause</param> /// <param name="input">input</param> /// <param name="parameterName">parameter name</param> /// <exception cref="ArgumentException"></exception> public static void Zero(this IShieldClause shieldClause, decimal input, string parameterName) { Zero <decimal>(shieldClause, input, parameterName); }
/// <summary> /// Throws <see cref="ArgumentException"/> if the input is equals to Zero /// </summary> /// <param name="shieldClause">shield clause</param> /// <param name="input">input</param> /// <param name="parameterName">parameter name</param> /// <exception cref="ArgumentException"></exception> public static void Zero(this IShieldClause shieldClause, long input, string parameterName) { Zero <long>(shieldClause, input, parameterName); }
/// <summary> /// Throws <see cref="ArgumentException"/> if <see cref="from"/> is greater than <see cref="to"/> /// Throws <see cref="ArgumentOutOfRangeException"/> if <see cref="input"/> value is not with in the specified range /// </summary> /// <param name="shieldClause">shield clause</param> /// <param name="input">input</param> /// <param name="parameterName">parameter name</param> /// <param name="from">range from</param> /// <param name="to">range to</param> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> public static void OutOfRange(this IShieldClause shieldClause, int input, string parameterName, int from, int to) { OutOfRange <int>(shieldClause, input, parameterName, from, to); }
/// <summary> /// Throws <see cref="ArgumentException"/> if <see cref="from"/> is greater than <see cref="to"/> /// Throws <see cref="ArgumentOutOfRangeException"/> if <see cref="input"/> datetime is not with in the specified range /// </summary> /// <param name="shieldClause">shield clause</param> /// <param name="input">input</param> /// <param name="parameterName">parameter name</param> /// <param name="from">range from</param> /// <param name="to">range to</param> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> public static void OutOfRange(this IShieldClause shieldClause, DateTime input, string parameterName, DateTime from, DateTime to) { OutOfRange <DateTime>(shieldClause, input, parameterName, from, to); }