Class ValidationExtensions
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
DefaultKeyExistsMessage
Default message for duplicate keys in dictionaries.
public const string DefaultKeyExistsMessage = "already exists"
Field Value
DefaultKeyNotFoundMessage
Default message for missing keys in dictionaries.
public const string DefaultKeyNotFoundMessage = "does not exist"
Field Value
DefaultNullMessage
Default message for null arguments.
public const string DefaultNullMessage = "cannot be null"
Field Value
DefaultNullOrEmptyMessage
Default message for null or empty collections.
public const string DefaultNullOrEmptyMessage = "cannot be null or empty"
Field Value
DefaultNullOrWhitespaceMessage
Default message for null or whitespace strings.
public const string DefaultNullOrWhitespaceMessage = "cannot be null or whitespace"
Field Value
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
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
Returns
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
dictionaryIDictionary<TKey, TValue>keyTKeyparamNamestringmessagestring
Returns
- IDictionary<TKey, TValue>
Type Parameters
TKeyTValue
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
dictionaryImmutableDictionary<TKey, TValue>keyTKeyparamNamestringmessagestring
Returns
- ImmutableDictionary<TKey, TValue>
Type Parameters
TKeyTValue
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
dictionaryIDictionary<TKey, TValue>keyTKeyparamNamestringmessagestring
Returns
- IDictionary<TKey, TValue>
Type Parameters
TKeyTValue
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
dictionaryImmutableDictionary<TKey, TValue>keyTKeyparamNamestringmessagestring
Returns
- ImmutableDictionary<TKey, TValue>
Type Parameters
TKeyTValue
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
Returns
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
Returns
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
Returns
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
Returns
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
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
Returns
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
valueICollection<T>paramNamestringmessagestring
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
valueIEnumerable<T>paramNamestringmessagestring
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
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
dictionaryIDictionary<TKey, TValue>paramNamestringmessagestring
Returns
- IDictionary<TKey, TValue>
Type Parameters
TKeyTValue
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
valuestringThe string to validate.
paramNamestringThe parameter name (use nameof()).
messagestringOptional 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
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
dictionaryIDictionary<TKey, TValue>paramNamestringmessagestring
Returns
- IDictionary<TKey, TValue>
Type Parameters
TKeyTValue
EnsurePositive(int, string, string?)
Ensures the value is greater than zero.
public static int EnsurePositive(this int value, string paramName, string? message = null)
Parameters
Returns
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
keystringdictionaryIDictionary<string, TValue>paramNamestringmessagestring
Returns
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
keystringdictionaryImmutableDictionary<string, TValue>paramNamestringmessagestring
Returns
Type Parameters
TValue
Examples
var validKey = key.EnsureValidDictionaryKey(Tags, nameof(key));
Tags = Tags.Add(validKey, value);