1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-15 10:22:57 +00:00

Added validation methods

This commit is contained in:
JKorf
2019-10-21 16:36:31 +02:00
parent b2b5b0fef0
commit 0699d91b04
2 changed files with 224 additions and 0 deletions
+50
View File
@@ -213,5 +213,55 @@ namespace CryptoExchange.Net
return null;
}
}
/// <summary>
/// Validates an int is one of the allowed values
/// </summary>
/// <param name="value">Value of the int</param>
/// <param name="argumentName">Name of the parameter</param>
/// <param name="allowedValues">Allowed values</param>
public static void ValidateIntValues(this int value, string argumentName, params int[] allowedValues)
{
if (!allowedValues.Contains(value))
throw new ArgumentException(
$"{value} not allowed for parameter {argumentName}, allowed values: {string.Join(", ", allowedValues)}");
}
/// <summary>
/// Validates an int is between two values
/// </summary>
/// <param name="value">The value of the int</param>
/// <param name="argumentName">Name of the parameter</param>
/// <param name="minValue">Min value</param>
/// <param name="maxValue">Max value</param>
public static void ValidateIntBetween(this int value, string argumentName, int minValue, int maxValue)
{
if (value < minValue || value > maxValue)
throw new ArgumentException(
$"{value} not allowed for parameter {argumentName}, min: {minValue}, max: {maxValue}");
}
/// <summary>
/// Validates a string is not null or empty
/// </summary>
/// <param name="value">The value of the string</param>
/// <param name="argumentName">Name of the parameter</param>
public static void ValidateNotNull(this string value, string argumentName)
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException($"No value provided for parameter {argumentName}");
}
/// <summary>
/// Validates an object is not null
/// </summary>
/// <param name="value">The value of the object</param>
/// <param name="argumentName">Name of the parameter</param>
public static void ValidateNotNull(this object value, string argumentName)
{
if (value == null)
throw new ArgumentException($"No value provided for parameter {argumentName}");
}
}
}