using SafeMobileLib.WebsocketClient.Exceptions; using System.Collections.Generic; using System.Linq; namespace SafeMobileLib.WebsocketClient.Validations { internal static class Validations { /// /// It throws if value is null or empty/white spaces /// /// The value to be validated /// Input parameter name public static void ValidateInput(string value, string name) { if (string.IsNullOrWhiteSpace(value)) { throw new WebsocketBadInputException($"Input string parameter '{name}' is null or empty. Please correct it."); } } /// /// It throws if value is null /// /// The value to be validated /// Input parameter name public static void ValidateInput(T value, string name) { if (Equals(value, default(T))) { throw new WebsocketBadInputException($"Input parameter '{name}' is null. Please correct it."); } } /// /// It throws if collection is null or collection is empty /// /// The collection to be validated /// Input parameter name public static void ValidateInputCollection(IEnumerable collection, string name) { // ReSharper disable once PossibleMultipleEnumeration ValidateInput(collection, name); // ReSharper disable once PossibleMultipleEnumeration if (!collection.Any()) { throw new WebsocketBadInputException($"Input collection '{name}' is empty. Please correct it."); } } /// /// It throws if value is not in specified range /// /// The value to be validated /// Input parameter name /// Minimal value of input /// Maximum value of input public static void ValidateInput(int value, string name, int minValue = int.MinValue, int maxValue = int.MaxValue) { if (value < minValue) { throw new WebsocketBadInputException($"Input parameter '{name}' is lower than {minValue}. Please correct it."); } if (value > maxValue) { throw new WebsocketBadInputException($"Input parameter '{name}' is higher than {maxValue}. Please correct it."); } } /// /// It throws if value is not in specified range /// /// The value to be validated /// Input parameter name /// Minimal value of input /// Maximum value of input public static void ValidateInput(long value, string name, long minValue = long.MinValue, long maxValue = long.MaxValue) { if (value < minValue) { throw new WebsocketBadInputException($"Input parameter '{name}' is lower than {minValue}. Please correct it."); } if (value > maxValue) { throw new WebsocketBadInputException($"Input parameter '{name}' is higher than {maxValue}. Please correct it."); } } /// /// It throws if value is not in specified range /// /// The value to be validated /// Input parameter name /// Minimal value of input /// Maximum value of input public static void ValidateInput(double value, string name, double minValue = double.MinValue, double maxValue = double.MaxValue) { if (value < minValue) { throw new WebsocketBadInputException($"Input parameter '{name}' is lower than {minValue}. Please correct it."); } if (value > maxValue) { throw new WebsocketBadInputException($"Input parameter '{name}' is higher than {maxValue}. Please correct it."); } } } }