From 0699d91b04b69acc2271a40297ed9a57e5a2df2d Mon Sep 17 00:00:00 2001 From: JKorf Date: Mon, 21 Oct 2019 16:36:31 +0200 Subject: [PATCH] Added validation methods --- CryptoExchange.Net/CryptoExchange.Net.xml | 174 ++++++++++++++++++++++ CryptoExchange.Net/ExtensionMethods.cs | 50 +++++++ 2 files changed, 224 insertions(+) diff --git a/CryptoExchange.Net/CryptoExchange.Net.xml b/CryptoExchange.Net/CryptoExchange.Net.xml index d71e803..e8cdf81 100644 --- a/CryptoExchange.Net/CryptoExchange.Net.xml +++ b/CryptoExchange.Net/CryptoExchange.Net.xml @@ -507,6 +507,37 @@ + + + Validates an int is one of the allowed values + + Value of the int + Name of the parameter + Allowed values + + + + Validates an int is between two values + + The value of the int + Name of the parameter + Min value + Max value + + + + Validates a string is not null or empty + + The value of the string + Name of the parameter + + + + Validates an object is not null + + The value of the object + Name of the parameter + Rate limiter interface @@ -2820,5 +2851,148 @@ + + + Specifies that is allowed as an input even if the + corresponding type disallows it. + + + + + Initializes a new instance of the class. + + + + + Specifies that is disallowed as an input even if the + corresponding type allows it. + + + + + Initializes a new instance of the class. + + + + + Specifies that a method that will never return under any circumstance. + + + + + Initializes a new instance of the class. + + + + + Specifies that the method will not return if the associated + parameter is passed the specified value. + + + + + Gets the condition parameter value. + Code after the method is considered unreachable by diagnostics if the argument + to the associated parameter matches this value. + + + + + Initializes a new instance of the + class with the specified parameter value. + + + The condition parameter value. + Code after the method is considered unreachable by diagnostics if the argument + to the associated parameter matches this value. + + + + + Specifies that an output may be even if the + corresponding type disallows it. + + + + + Initializes a new instance of the class. + + + + + Specifies that when a method returns , + the parameter may be even if the corresponding type disallows it. + + + + + Gets the return value condition. + If the method returns this value, the associated parameter may be . + + + + + Initializes the attribute with the specified return value condition. + + + The return value condition. + If the method returns this value, the associated parameter may be . + + + + + Specifies that an output is not even if the + corresponding type allows it. + + + + + Initializes a new instance of the class. + + + + + Specifies that the output will be non- if the + named parameter is non-. + + + + + Gets the associated parameter name. + The output will be non- if the argument to the + parameter specified is non-. + + + + + Initializes the attribute with the associated parameter name. + + + The associated parameter name. + The output will be non- if the argument to the + parameter specified is non-. + + + + + Specifies that when a method returns , + the parameter will not be even if the corresponding type allows it. + + + + + Gets the return value condition. + If the method returns this value, the associated parameter will not be . + + + + + Initializes the attribute with the specified return value condition. + + + The return value condition. + If the method returns this value, the associated parameter will not be . + + diff --git a/CryptoExchange.Net/ExtensionMethods.cs b/CryptoExchange.Net/ExtensionMethods.cs index 116a904..9fd7e1c 100644 --- a/CryptoExchange.Net/ExtensionMethods.cs +++ b/CryptoExchange.Net/ExtensionMethods.cs @@ -213,5 +213,55 @@ namespace CryptoExchange.Net return null; } } + + /// + /// Validates an int is one of the allowed values + /// + /// Value of the int + /// Name of the parameter + /// Allowed values + 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)}"); + } + + /// + /// Validates an int is between two values + /// + /// The value of the int + /// Name of the parameter + /// Min value + /// Max value + 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}"); + } + + /// + /// Validates a string is not null or empty + /// + /// The value of the string + /// Name of the parameter + public static void ValidateNotNull(this string value, string argumentName) + { + if (string.IsNullOrEmpty(value)) + throw new ArgumentException($"No value provided for parameter {argumentName}"); + } + + /// + /// Validates an object is not null + /// + /// The value of the object + /// Name of the parameter + public static void ValidateNotNull(this object value, string argumentName) + { + if (value == null) + throw new ArgumentException($"No value provided for parameter {argumentName}"); + } } } +