Example #1
0
        /// <summary>
        /// Validates an argument.
        /// This is the simplest form, but can only be used when the parameter being validated is the only such type in the list of method parameters. If there are more parameters of the same type, or you are validating anything but a parameter, use the expression form.</summary>
        /// <returns>In case the validation succeeds, returns the argument value.</returns>
        /// <exception cref="ArgumentNullException"/>
        public static T AgainstNull <T>(T arg, string argName = null, string message = null)
        {
            if (arg == null)
            {
                throw new ArgumentNullException(argName ?? ArgHelper.ArgName(typeof(T)), message ?? "Parameter may not be null");
            }

            return(arg);
        }
Example #2
0
        /// <summary>
        /// Throws if the specified value is not a string representation of an Int32.
        /// This is the simplest form, but can only be used when the parameter being validated is the only such type in the list of method parameters. If there are more parameters of the same type, or you are validating anything but a parameter, use the expression form.</summary>
        /// </summary>
        /// <returns>In case the valisation succeeds, returns the argument value.</returns>
        /// <exception cref="ArgumentException"/>
        public static string AgainstNonIntString(string arg, string argName = null)
        {
            if (!Int32.TryParse(arg, out int dummy))
            {
                throw new ArgumentException(
                          $"'{arg}' is expected to be an integer.", argName ?? ArgHelper.ArgName(typeof(string)));
            }

            return(arg);
        }
Example #3
0
        /// <summary>
        /// Throws if the specified integer value is negative.
        /// This is the simplest form, but can only be used when the parameter being validated is the only such type in the list of method parameters. If there are more parameters of the same type, or you are validating anything but a parameter, use the expression form.</summary>
        /// </summary>
        /// <returns>In case the valisation succeeds, returns the argument value.</returns>
        /// <exception cref="ArgumentException"/>
        public static Int64 AgainstNegativeInt(Int64 arg, string argName = null, string message = null)
        {
            if (arg < 0)
            {
                throw new ArgumentException(
                          message ?? "Parameter cannot be negative.", argName ?? ArgHelper.ArgName(typeof(Int64)));
            }

            return(arg);
        }
Example #4
0
        /// <summary>
        /// Validates an argument type.
        /// The test will succeed if the argument
        ///  - is of the specified type
        ///  - is derived from the specifeid type
        ///  - implements the specified interface
        /// <returns>In case the validation succeeds, returns the argument value.</returns>
        /// <exception cref="ArgumentNullException"/>
        public static TRequired AgainstInvalidType <TRequired>(object arg, string argName = null, string message = null)
        {
            if (arg != null && !typeof(TRequired).IsAssignableFrom(arg.GetType()))
            {
                throw new ArgumentException(
                          message ?? $"Parameter must be of type {typeof(TRequired)} or assignable to it.",
                          argName ?? ArgHelper.ArgName(typeof(object)));
            }

            return((TRequired)arg);
        }
Example #5
0
        /// <summary>
        /// Validates an argument against a set of acceptable values.
        /// This is the simplest form, but can only be used when the parameter being validated is the only such type in the list of method parameters. If there are more parameters of the same type, or you are validating anything but a parameter, use the expression form.</summary>
        /// </summary>
        /// <param name="argName">The name of the argument to include in case of an exception. If omitted, the argumnet name in the exception will include an educated guess.</param>
        /// <returns>In case the valisation succeeds, returns the argument value.</returns>
        /// <exception cref="ArgumentException"/>
        public static T AgainstUnsupportedValues <T>(
            T arg, IEnumerable <T> supportedValues, string argName = null, string message = null)
        {
            if ((supportedValues == null) || !supportedValues.Contains(arg))
            {
                throw new ArgumentException(
                          message
                          ?? $"Argument value not supported. Supported values are {string.Join(", ", supportedValues)}",
                          argName ?? ArgHelper.ArgName(typeof(T)));
            }

            return(arg);
        }
Example #6
0
        /// <summary>
        /// Throws if the specified value is a string that is too long.
        /// This is the simplest form, but can only be used when the parameter being validated is the only such type in the list of method parameters. If there are more parameters of the same type, or you are validating anything but a parameter, use the expression form.</summary>
        /// </summary>
        /// <returns>In case the valisation succeeds, returns the argument value.</returns>
        /// <exception cref="ArgumentException"/>
        public static string AgainstLongString(
            string arg, int maxAcceptableLength, string argName = null, string message = null)
        {
            if (!string.IsNullOrWhiteSpace(arg) && (arg.Length > maxAcceptableLength))
            {
                throw new ArgumentException(
                          message
                          ?? string.Format("String argument too long, {0} characters, max {1} allowed.", arg.Length, maxAcceptableLength),
                          argName ?? ArgHelper.ArgName(typeof(string)));
            }

            return(arg);
        }
Example #7
0
 /// <summary>Returns the specified string if it is at most the specified length.</summary>
 /// <param name="argName">If specified and a validation error occurs, the resulting exception will contain this string as the name of the argument, instead of the name of the validated argument.</param>
 /// <exception cref="ArgumentException"/>
 public static string IfNotTooLong(string value, int maxAcceőtableLength, string argName = null)
 {
     Guard.AgainstLongString(value, maxAcceőtableLength, argName: argName ?? ArgHelper.ArgName(typeof(string)));
     return(value);
 }
Example #8
0
 /// <summary>Returns the specified value if it is not null or whitespace.</summary>
 /// <param name="argName">If specified and a validation error occurs, the resulting exception will contain this string as the name of the argument, instead of the name of the validated argument.</param>
 /// <exception cref="ArgumentException"/>
 public static string IfNotNullOrWhiteSpace(string value, string argName = null)
 {
     Guard.AgainstNullOrWhiteSpaceString(value, argName: argName ?? ArgHelper.ArgName(typeof(string)));
     return(value);
 }
Example #9
0
 /// <summary>Returns the integer value of the specified value if is a string representation of an integer.</summary>
 /// <param name="argName">If specified and a validation error occurs, the resulting exception will contain this string as the name of the argument, instead of the name of the validated argument.</param>
 /// <exception cref="ArgumentException"/>
 public static int IfRepresentsAnIntegerAsInteger(string value, string argName = null)
 {
     Guard.AgainstNonIntString(value, argName: argName ?? ArgHelper.ArgName(typeof(string)));
     return(int.Parse(value));
 }
Example #10
0
 /// <summary>Returns the specified value if it is one of the collection provided. THrows otherwise.</summary>
 /// <param name="argName">If specified and a validation error occurs, the resulting exception will contain this string as the name of the argument, instead of the name of the validated argument.</param>
 /// <exception cref="ArgumentException"/>
 public static TValue IfOneOf <TValue>(TValue value, IEnumerable <TValue> supportedValues, string argName = null)
 {
     Guard.AgainstUnsupportedValues(value, supportedValues, argName: argName ?? ArgHelper.ArgName(typeof(TValue)));
     return(value);
 }
Example #11
0
 /// <summary>Returns the specified value if that is not null.</summary>
 /// <param name="argName">If specified and a validation error occurs, the resulting exception will contain this string as the name of the argument, instead of the name of the validated argument.</param>
 /// <exception cref="ArgumentNullException"/>
 public static TValue IfNotNull <TValue>(TValue value, string argName = null)
 {
     Guard.AgainstNull(value, argName: argName ?? ArgHelper.ArgName(typeof(TValue)));
     return(value);
 }
Example #12
0
 /// <summary>Returns the specified int value if it is positive.</summary>
 /// <param name="argName">If specified and a validation error occurs, the resulting exception will contain this string as the name of the argument, instead of the name of the validated argument.</param>
 /// <exception cref="ArgumentException"/>
 public static int IfPositive(int value, string argName = null)
 {
     Guard.AgainstNonPositiveInt(value, argName: argName ?? ArgHelper.ArgName(typeof(int)));
     return(value);
 }
Example #13
0
 /// <summary>Returns the specified collection if it is not null or empty.</summary>
 /// <param name="argName">If specified and a validation error occurs, the resulting exception will contain this string as the name of the argument, instead of the name of the validated argument.</param>
 /// <exception cref="ArgumentException"/>
 public static TColl IfNotNullOrEmpty <TColl, TItem>(TColl value, string argName = null)
     where TColl : IEnumerable <TItem>
 {
     Guard.AgainstNullOrEmptyCollection(value, argName: argName ?? ArgHelper.ArgName(typeof(TColl)));
     return(value);
 }
Example #14
0
        /// <summary>
        /// Throws if the specified value is null or an empty collection.
        /// This is the simplest form, but can only be used when the parameter being validated is the only such type in the list of method parameters. If there are more parameters of the same type, or you are validating anything but a parameter, use the expression form.</summary>
        /// </summary>
        /// <returns>In case the valisation succeeds, returns the argument value.</returns>
        /// <exception cref="ArgumentException"/>
        public static IEnumerable <T> AgainstNullOrEmptyCollection <T>(
            IEnumerable <T> arg, string argName = null, string message = null)
        {
            if ((arg == null) || (arg.Count() == 0))
            {
                throw new ArgumentException(
                          message ?? "Parameter cannot be null or an empty collection.", argName ?? ArgHelper.ArgName(typeof(IEnumerable <T>)));
            }

            return(arg);
        }
Example #15
0
        /// <summary>
        /// Throws if the specified value is null, an empty string or whitespace.
        /// This is the simplest form, but can only be used when the parameter being validated is the only such type in the list of method parameters. If there are more parameters of the same type, or you are validating anything but a parameter, use the expression form.</summary>
        /// </summary>
        /// <returns>In case the valisation succeeds, returns the argument value.</returns>
        /// <exception cref="ArgumentException"/>
        public static string AgainstNullOrWhiteSpaceString(
            string arg, string argName = null, string message = null)
        {
            if (string.IsNullOrWhiteSpace(arg))
            {
                throw new ArgumentException(
                          message ?? "Parameter cannot be null or whitespace.", argName ?? ArgHelper.ArgName(typeof(string)));
            }

            return(arg);
        }