Class Validations
Provides helper methods for validating arguments and values with consistent exception messages.
public static class Validations
- Inheritance
-
Validations
- Inherited Members
Methods
ArgMustBeANumber<T>(T, string)
Ensures that the numeric argument is not NaN (or the type-specific equivalent).
public static T ArgMustBeANumber<T>(this T value, string valueName = "") where T : INumberBase<T>
Parameters
valueTValue to validate.
valueNamestringName of the argument for diagnostic messages.
Returns
- T
The validated value.
Type Parameters
TNumeric type of the value.
Exceptions
- ArgumentOutOfRangeException
Thrown when the value represents an undefined number.
ArgMustBeBetween<T>(T, T, T, string)
Ensures that the argument falls within the specified inclusive range.
public static T ArgMustBeBetween<T>(this T value, T min, T max, string valueName = "") where T : IComparable
Parameters
valueTValue to validate.
minTMinimum inclusive value.
maxTMaximum inclusive value.
valueNamestringName of the argument for diagnostic messages.
Returns
- T
The validated value.
Type Parameters
TType of the compared values.
Exceptions
- ArgumentOutOfRangeException
Thrown when the value is outside the allowed range.
ArgMustBeEqualsTo<T>(T, T, string)
Ensures that the argument is equal to the specified target value.
public static T ArgMustBeEqualsTo<T>(this T value, T targetValue, string valueName = "") where T : IComparable
Parameters
valueTValue to validate.
targetValueTValue that
valuemust equal.valueNamestringName of the argument for diagnostic messages.
Returns
- T
The validated value.
Type Parameters
TType of the compared values.
Exceptions
- ArgumentOutOfRangeException
Thrown when the values are not equal.
ArgMustBeGreaterOrEqualsThan<T>(T, T, string)
Ensures that the argument is greater than or equal to the specified minimum value.
public static T ArgMustBeGreaterOrEqualsThan<T>(this T value, T min, string valueName = "") where T : IComparable
Parameters
valueTValue to validate.
minTMinimum allowed value.
valueNamestringName of the argument for diagnostic messages.
Returns
- T
The validated value.
Type Parameters
TType of the compared values.
Exceptions
- ArgumentOutOfRangeException
Thrown when the value is less than the allowed minimum.
ArgMustBeGreaterThan<T>(T, T, string)
Ensures that the argument is strictly greater than the specified minimum value.
public static T ArgMustBeGreaterThan<T>(this T value, T min, string valueName = "") where T : IComparable
Parameters
valueTValue to validate.
minTMinimum exclusive value.
valueNamestringName of the argument for diagnostic messages.
Returns
- T
The validated value.
Type Parameters
TType of the compared values.
Exceptions
- ArgumentOutOfRangeException
Thrown when the value is less than or equal to the allowed minimum.
ArgMustBeIn<T>(T, T[], string)
Ensures that the argument is present in the provided list of allowed values.
public static T ArgMustBeIn<T>(this T value, T[] targetValue, string valueName = "") where T : IComparable
Parameters
valueTValue to validate.
targetValueT[]Collection of allowed values.
valueNamestringName of the argument for diagnostic messages.
Returns
- T
The validated value.
Type Parameters
TType of the compared values.
Exceptions
- ArgumentOutOfRangeException
Thrown when the value is not present in the collection.
ArgMustBeLesserOrEqualsThan<T>(T, T, string)
Ensures that the argument is less than or equal to a maximum value.
public static T ArgMustBeLesserOrEqualsThan<T>(this T value, T max, string valueName = "") where T : IComparable
Parameters
valueTValue to validate.
maxTMaximum allowed value.
valueNamestringName of the argument for diagnostic messages.
Returns
- T
The validated value.
Type Parameters
TType of the compared values.
Exceptions
- ArgumentOutOfRangeException
Thrown when the value is greater than the allowed maximum.
ArgMustBeLesserThan<T>(T, T, string)
Ensures that the argument is strictly less than a maximum value.
public static T ArgMustBeLesserThan<T>(this T value, T max, string valueName = "") where T : IComparable
Parameters
valueTValue to validate.
maxTMaximum exclusive value.
valueNamestringName of the argument for diagnostic messages.
Returns
- T
The validated value.
Type Parameters
TType of the compared values.
Exceptions
- ArgumentOutOfRangeException
Thrown when the value is greater than or equal to the maximum.
ArgMustBeOfBounds(Array, (int? lBound, int? uBound)[], string)
Validates that an array argument matches the specified bounds for each dimension.
public static void ArgMustBeOfBounds(this Array value, (int? lBound, int? uBound)[] bounds, string valueName = "")
Parameters
valueArrayArray to validate.
bounds(int? lBound, int? uBound)[]Expected lower and upper bounds for each dimension.
valueNamestringName of the argument for diagnostic messages.
Exceptions
- ArgumentException
Thrown when any bound does not match the requested value.
ArgMustBeOfRank(Array, int, string)
Validates that an array argument has the specified rank.
public static void ArgMustBeOfRank(this Array value, int size, string valueName = "")
Parameters
valueArrayArray to validate.
sizeintExpected rank.
valueNamestringName of the argument for diagnostic messages.
Exceptions
- ArgumentException
Thrown when the array rank is not the expected value.
ArgMustBeOfSize<T>(IReadOnlyCollection<T>, int, string)
Validates that a collection argument contains exactly the specified number of elements.
public static void ArgMustBeOfSize<T>(this IReadOnlyCollection<T> value, int size, string valueName = "")
Parameters
valueIReadOnlyCollection<T>Collection to validate.
sizeintExpected number of elements.
valueNamestringName of the argument for diagnostic messages.
Type Parameters
TType of elements in the collection.
Exceptions
- ArgumentOutOfRangeException
Thrown when the collection length does not match the expected value.
ArgMustBeOfSizes(Array, int?[], string)
Validates that an array argument matches the specified dimension sizes.
public static void ArgMustBeOfSizes(this Array value, int?[] sizes, string valueName = "")
Parameters
valueArrayArray to validate.
sizesint?[]Expected sizes per dimension, or null for unconstrained dimensions.
valueNamestringName of the argument for diagnostic messages.
Exceptions
- ArgumentException
Thrown when the array shape does not match the requested sizes.
ArgMustBe<T>(T, Func<T, bool>, string, string)
Validates that a predicate evaluates to true for the provided value.
public static T ArgMustBe<T>(this T value, Func<T, bool> validationFunction, string message, string valueName = "")
Parameters
valueTValue to validate.
validationFunctionFunc<T, bool>Predicate that defines the validation rule.
messagestringError message used when the predicate fails.
valueNamestringName of the argument for diagnostic messages.
Returns
- T
The validated value.
Type Parameters
TType of the value.
Exceptions
- ArgumentNullException
Thrown when
validationFunctionis null.- ArgumentException
Thrown when the predicate evaluates to false.
ArgMustNotBeEmpty<T>(IReadOnlyCollection<T>, string)
Validates that a collection argument contains at least one element.
public static void ArgMustNotBeEmpty<T>(this IReadOnlyCollection<T> value, string valueName = "")
Parameters
valueIReadOnlyCollection<T>Collection to validate.
valueNamestringName of the argument for diagnostic messages.
Type Parameters
TType of elements in the collection.
Exceptions
- ArgumentOutOfRangeException
Thrown when the collection is empty.
ArgMustNotBe<T>(T, Func<T, bool>, string, string)
Validates that a predicate evaluates to false for the provided value.
public static T ArgMustNotBe<T>(this T value, Func<T, bool> validationFunction, string message, string valueName = "")
Parameters
valueTValue to validate.
validationFunctionFunc<T, bool>Predicate that defines the invalid condition.
messagestringError message used when the predicate succeeds.
valueNamestringName of the argument for diagnostic messages.
Returns
- T
The validated value.
Type Parameters
TType of the value.
Exceptions
- ArgumentNullException
Thrown when
validationFunctionis null.- ArgumentException
Thrown when the predicate evaluates to true.
ArgSizeMustBeMultipleOf<T>(IReadOnlyCollection<T>, int, string)
Ensures that the size of a collection argument is a multiple of the specified value.
public static void ArgSizeMustBeMultipleOf<T>(this IReadOnlyCollection<T> value, int multiple, string valueName = "")
Parameters
valueIReadOnlyCollection<T>Collection to validate.
multipleintExpected divisor of the collection length.
valueNamestringName of the argument for diagnostic messages.
Type Parameters
TType of elements in the collection.
Exceptions
- ArgumentOutOfRangeException
Thrown when the collection length is not a multiple of
multiple.
Arg<T>(T, string)
Creates an Arg<T>(T, string) validator for the provided value.
public static Arg<T> Arg<T>(this T value, string name = "")
Parameters
valueTValue to validate.
namestringName of the argument, automatically captured when possible.
Returns
- Arg<T>
A validator that can be used to chain additional checks.
Type Parameters
TType of the value to validate.
MustBeOfRank(Array, int)
Returns an exception describing a rank mismatch, or null when the rank matches.
public static Exception? MustBeOfRank(this Array value, int rank)
Parameters
Returns
- Exception
An Utils.Objects.ArrayDimensionException when the array rank does not equal
rank; null when the rank matches.
MustBeOfSize(Array, int?[])
Returns an exception describing the required dimension sizes for the provided array.
public static Exception MustBeOfSize(this Array value, int?[] sizes)
Parameters
valueArrayArray to inspect.
sizesint?[]Expected sizes per dimension, or null for unconstrained dimensions.
Returns
ValueMustNotBeNull<T>(T, string)
Ensures that a reference-type value is not null.
public static T ValueMustNotBeNull<T>(this T value, string valueName = "") where T : class
Parameters
valueTValue to validate.
valueNamestringName of the value for diagnostic messages.
Returns
- T
The original value when it is not null.
Type Parameters
TType of the value being checked.
Exceptions
- InvalidOperationException
Thrown when
valueis null.
ValueSizeMustBeMultipleOf<T>(IReadOnlyCollection<T>, int, string)
Ensures that a collection size is a multiple of the specified value.
public static void ValueSizeMustBeMultipleOf<T>(this IReadOnlyCollection<T> value, int multiple, string valueName = "")
Parameters
valueIReadOnlyCollection<T>Collection to validate.
multipleintExpected divisor of the collection length.
valueNamestringName of the argument for diagnostic messages.
Type Parameters
TType of elements in the collection.
Exceptions
- ArrayDimensionException
Thrown when the collection length is not a multiple of
multiple.
Variable<T>(T, string)
Creates a Variable<T>(T, string) validator for the provided value.
public static Variable<T> Variable<T>(this T value, string name = "")
Parameters
valueTValue to validate.
namestringName of the variable, automatically captured when possible.
Returns
- Variable<T>
A validator that can be used to chain additional checks.
Type Parameters
TType of the value to validate.