Table of Contents

Class Validations

Namespace
Utils.Objects
Assembly
Utils.dll

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

value T

Value to validate.

valueName string

Name of the argument for diagnostic messages.

Returns

T

The validated value.

Type Parameters

T

Numeric 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

value T

Value to validate.

min T

Minimum inclusive value.

max T

Maximum inclusive value.

valueName string

Name of the argument for diagnostic messages.

Returns

T

The validated value.

Type Parameters

T

Type 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

value T

Value to validate.

targetValue T

Value that value must equal.

valueName string

Name of the argument for diagnostic messages.

Returns

T

The validated value.

Type Parameters

T

Type 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

value T

Value to validate.

min T

Minimum allowed value.

valueName string

Name of the argument for diagnostic messages.

Returns

T

The validated value.

Type Parameters

T

Type 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

value T

Value to validate.

min T

Minimum exclusive value.

valueName string

Name of the argument for diagnostic messages.

Returns

T

The validated value.

Type Parameters

T

Type 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

value T

Value to validate.

targetValue T[]

Collection of allowed values.

valueName string

Name of the argument for diagnostic messages.

Returns

T

The validated value.

Type Parameters

T

Type 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

value T

Value to validate.

max T

Maximum allowed value.

valueName string

Name of the argument for diagnostic messages.

Returns

T

The validated value.

Type Parameters

T

Type 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

value T

Value to validate.

max T

Maximum exclusive value.

valueName string

Name of the argument for diagnostic messages.

Returns

T

The validated value.

Type Parameters

T

Type 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

value Array

Array to validate.

bounds (int? lBound, int? uBound)[]

Expected lower and upper bounds for each dimension.

valueName string

Name 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

value Array

Array to validate.

size int

Expected rank.

valueName string

Name 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

value IReadOnlyCollection<T>

Collection to validate.

size int

Expected number of elements.

valueName string

Name of the argument for diagnostic messages.

Type Parameters

T

Type 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

value Array

Array to validate.

sizes int?[]

Expected sizes per dimension, or null for unconstrained dimensions.

valueName string

Name 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

value T

Value to validate.

validationFunction Func<T, bool>

Predicate that defines the validation rule.

message string

Error message used when the predicate fails.

valueName string

Name of the argument for diagnostic messages.

Returns

T

The validated value.

Type Parameters

T

Type of the value.

Exceptions

ArgumentNullException

Thrown when validationFunction is 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

value IReadOnlyCollection<T>

Collection to validate.

valueName string

Name of the argument for diagnostic messages.

Type Parameters

T

Type 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

value T

Value to validate.

validationFunction Func<T, bool>

Predicate that defines the invalid condition.

message string

Error message used when the predicate succeeds.

valueName string

Name of the argument for diagnostic messages.

Returns

T

The validated value.

Type Parameters

T

Type of the value.

Exceptions

ArgumentNullException

Thrown when validationFunction is 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

value IReadOnlyCollection<T>

Collection to validate.

multiple int

Expected divisor of the collection length.

valueName string

Name of the argument for diagnostic messages.

Type Parameters

T

Type 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

value T

Value to validate.

name string

Name of the argument, automatically captured when possible.

Returns

Arg<T>

A validator that can be used to chain additional checks.

Type Parameters

T

Type 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

value Array

Array to inspect.

rank int

Expected rank.

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

value Array

Array to inspect.

sizes int?[]

Expected sizes per dimension, or null for unconstrained dimensions.

Returns

Exception

An exception describing the size constraint when it is met; otherwise null.

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

value T

Value to validate.

valueName string

Name of the value for diagnostic messages.

Returns

T

The original value when it is not null.

Type Parameters

T

Type of the value being checked.

Exceptions

InvalidOperationException

Thrown when value is 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

value IReadOnlyCollection<T>

Collection to validate.

multiple int

Expected divisor of the collection length.

valueName string

Name of the argument for diagnostic messages.

Type Parameters

T

Type 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

value T

Value to validate.

name string

Name of the variable, automatically captured when possible.

Returns

Variable<T>

A validator that can be used to chain additional checks.

Type Parameters

T

Type of the value to validate.