Class ValidationExtensions

Namespace
REslava.Result
Assembly
REslava.Result.dll

Provides consistent validation extension methods with standardized error messages. All methods throw ArgumentException (or ArgumentNullException) with consistent formatting. Use these to validate inputs and maintain consistent error messages across the library.

public static class ValidationExtensions
Inheritance
ValidationExtensions
Inherited Members

Fields

DefaultEmptyMessage

Default message for empty collections.

public const string DefaultEmptyMessage = "cannot be empty"

Field Value

string

DefaultKeyExistsMessage

Default message for duplicate keys in dictionaries.

public const string DefaultKeyExistsMessage = "already exists"

Field Value

string

DefaultKeyNotFoundMessage

Default message for missing keys in dictionaries.

public const string DefaultKeyNotFoundMessage = "does not exist"

Field Value

string

DefaultNullMessage

Default message for null arguments.

public const string DefaultNullMessage = "cannot be null"

Field Value

string

DefaultNullOrEmptyMessage

Default message for null or empty collections.

public const string DefaultNullOrEmptyMessage = "cannot be null or empty"

Field Value

string

DefaultNullOrWhitespaceMessage

Default message for null or whitespace strings.

public const string DefaultNullOrWhitespaceMessage = "cannot be null or whitespace"

Field Value

string

Methods

EnsureArrayNotNull<T>(T[]?, string, string?)

Ensures the array is not null, returning the validated array.

public static T[] EnsureArrayNotNull<T>(this T[]? value, string paramName, string? message = null)

Parameters

value T[]
paramName string
message string

Returns

T[]

Type Parameters

T

EnsureInRange(int, int, int, string, string?)

Ensures the value is within the specified range (inclusive).

public static int EnsureInRange(this int value, int min, int max, string paramName, string? message = null)

Parameters

value int
min int
max int
paramName string
message string

Returns

int

EnsureKeyExists<TKey, TValue>(IDictionary<TKey, TValue>, TKey, string, string?)

Ensures the key exists in the dictionary. Returns the dictionary for fluent chaining.

public static IDictionary<TKey, TValue> EnsureKeyExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, string paramName, string? message = null) where TKey : notnull

Parameters

dictionary IDictionary<TKey, TValue>
key TKey
paramName string
message string

Returns

IDictionary<TKey, TValue>

Type Parameters

TKey
TValue

EnsureKeyExists<TKey, TValue>(ImmutableDictionary<TKey, TValue>, TKey, string, string?)

Ensures the key exists in the immutable dictionary. Returns the dictionary for fluent chaining.

public static ImmutableDictionary<TKey, TValue> EnsureKeyExists<TKey, TValue>(this ImmutableDictionary<TKey, TValue> dictionary, TKey key, string paramName, string? message = null) where TKey : notnull

Parameters

dictionary ImmutableDictionary<TKey, TValue>
key TKey
paramName string
message string

Returns

ImmutableDictionary<TKey, TValue>

Type Parameters

TKey
TValue

EnsureKeyNotExists<TKey, TValue>(IDictionary<TKey, TValue>, TKey, string, string?)

Ensures the key does not already exist in the dictionary. Returns the dictionary for fluent chaining.

public static IDictionary<TKey, TValue> EnsureKeyNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, string paramName, string? message = null) where TKey : notnull

Parameters

dictionary IDictionary<TKey, TValue>
key TKey
paramName string
message string

Returns

IDictionary<TKey, TValue>

Type Parameters

TKey
TValue

Examples

tags.EnsureNotNull(nameof(tags))
    .EnsureKeyNotExists("UserId", nameof(tags));
tags.Add("UserId", 123);

EnsureKeyNotExists<TKey, TValue>(ImmutableDictionary<TKey, TValue>, TKey, string, string?)

Ensures the key does not already exist in the immutable dictionary. Returns the dictionary for fluent chaining.

public static ImmutableDictionary<TKey, TValue> EnsureKeyNotExists<TKey, TValue>(this ImmutableDictionary<TKey, TValue> dictionary, TKey key, string paramName, string? message = null) where TKey : notnull

Parameters

dictionary ImmutableDictionary<TKey, TValue>
key TKey
paramName string
message string

Returns

ImmutableDictionary<TKey, TValue>

Type Parameters

TKey
TValue

EnsureMatches(string, string, string, string?)

Ensures the string matches a pattern, allowing for fluent validation chains.

public static string EnsureMatches(this string value, string pattern, string paramName, string? message = null)

Parameters

value string
pattern string
paramName string
message string

Returns

string

Examples

email.EnsureNotNullOrWhiteSpace(nameof(email))
     .EnsureMatches(@"^[^@]+@[^@]+$", nameof(email), "Invalid email format");

EnsureMaxLength(string, int, string, string?)

Ensures the string length does not exceed the specified maximum.

public static string EnsureMaxLength(this string value, int maxLength, string paramName, string? message = null)

Parameters

value string
maxLength int
paramName string
message string

Returns

string

EnsureMinLength(string, int, string, string?)

Ensures the string length is at least the specified minimum.

public static string EnsureMinLength(this string value, int minLength, string paramName, string? message = null)

Parameters

value string
minLength int
paramName string
message string

Returns

string

EnsureNonNegative(int, string, string?)

Ensures the value is greater than or equal to zero.

public static int EnsureNonNegative(this int value, string paramName, string? message = null)

Parameters

value int
paramName string
message string

Returns

int

EnsureNotEmpty<T>(T[], string, string?)

Ensures the array is not empty, returning the validated array.

public static T[] EnsureNotEmpty<T>(this T[] value, string paramName, string? message = null)

Parameters

value T[]
paramName string
message string

Returns

T[]

Type Parameters

T

EnsureNotNullOrEmpty(string?, string, string?)

Ensures the string is not null or empty, returning the validated string.

public static string EnsureNotNullOrEmpty(this string? value, string paramName, string? message = null)

Parameters

value string
paramName string
message string

Returns

string

EnsureNotNullOrEmpty<T>(ICollection<T>?, string, string?)

Ensures the collection is not null or empty, returning the validated collection.

public static ICollection<T> EnsureNotNullOrEmpty<T>(this ICollection<T>? value, string paramName, string? message = null)

Parameters

value ICollection<T>
paramName string
message string

Returns

ICollection<T>

Type Parameters

T

EnsureNotNullOrEmpty<T>(IEnumerable<T>?, string, string?)

Ensures the enumerable is not null or empty, returning the validated enumerable. Note: This enumerates the collection to check if it's empty.

public static IEnumerable<T> EnsureNotNullOrEmpty<T>(this IEnumerable<T>? value, string paramName, string? message = null)

Parameters

value IEnumerable<T>
paramName string
message string

Returns

IEnumerable<T>

Type Parameters

T

EnsureNotNullOrEmpty<T>(T[]?, string, string?)

Ensures the array is not null or empty, returning the validated array.

public static T[] EnsureNotNullOrEmpty<T>(this T[]? value, string paramName, string? message = null)

Parameters

value T[]
paramName string
message string

Returns

T[]

Type Parameters

T

EnsureNotNullOrEmpty<TKey, TValue>(IDictionary<TKey, TValue>?, string, string?)

Ensures the dictionary is not null or empty, returning the validated dictionary.

public static IDictionary<TKey, TValue> EnsureNotNullOrEmpty<TKey, TValue>(this IDictionary<TKey, TValue>? dictionary, string paramName, string? message = null) where TKey : notnull

Parameters

dictionary IDictionary<TKey, TValue>
paramName string
message string

Returns

IDictionary<TKey, TValue>

Type Parameters

TKey
TValue

EnsureNotNullOrWhiteSpace(string?, string, string?)

Ensures the string is not null or whitespace, returning the validated string.

public static string EnsureNotNullOrWhiteSpace(this string? value, string paramName, string? message = null)

Parameters

value string

The string to validate.

paramName string

The parameter name (use nameof()).

message string

Optional custom message. If null, uses default message.

Returns

string

The validated string value.

Examples

public void SetEmail(string email)
{
    email = email.EnsureNotNullOrWhiteSpace(nameof(email));
    // email is guaranteed to be non-null and non-whitespace here
}

Exceptions

ArgumentException

Thrown when value is null or whitespace.

EnsureNotNull<TCollection>(TCollection?, string, string?)

Ensures the collection is not null, returning the validated collection.

public static TCollection EnsureNotNull<TCollection>(this TCollection? value, string paramName, string? message = null) where TCollection : class

Parameters

value TCollection
paramName string
message string

Returns

TCollection

Type Parameters

TCollection

EnsureNotNull<TKey, TValue>(IDictionary<TKey, TValue>?, string, string?)

Ensures the dictionary is not null, returning the validated dictionary.

public static IDictionary<TKey, TValue> EnsureNotNull<TKey, TValue>(this IDictionary<TKey, TValue>? dictionary, string paramName, string? message = null) where TKey : notnull

Parameters

dictionary IDictionary<TKey, TValue>
paramName string
message string

Returns

IDictionary<TKey, TValue>

Type Parameters

TKey
TValue

EnsurePositive(int, string, string?)

Ensures the value is greater than zero.

public static int EnsurePositive(this int value, string paramName, string? message = null)

Parameters

value int
paramName string
message string

Returns

int

EnsureValidDictionaryKey<TValue>(string?, IDictionary<string, TValue>, string, string?)

Validates a string key for mutable dictionary use.

public static string EnsureValidDictionaryKey<TValue>(this string? key, IDictionary<string, TValue> dictionary, string paramName, string? message = null)

Parameters

key string
dictionary IDictionary<string, TValue>
paramName string
message string

Returns

string

Type Parameters

TValue

EnsureValidDictionaryKey<TValue>(string?, ImmutableDictionary<string, TValue>, string, string?)

Validates a string key for dictionary use: ensures it's not null/whitespace and doesn't exist. Returns the validated key for use.

public static string EnsureValidDictionaryKey<TValue>(this string? key, ImmutableDictionary<string, TValue> dictionary, string paramName, string? message = null)

Parameters

key string
dictionary ImmutableDictionary<string, TValue>
paramName string
message string

Returns

string

Type Parameters

TValue

Examples

var validKey = key.EnsureValidDictionaryKey(Tags, nameof(key));
Tags = Tags.Add(validKey, value);