API Reference
Complete documentation for all Pragmatic.Ensure methods.
Null Checks
Section titled “Null Checks”ThrowIfNull
Section titled “ThrowIfNull”Throws if a reference type or nullable value type is null.
Ensure.ThrowIfNull(user); // Reference typeEnsure.ThrowIfNull(nullableInt); // Nullable value type| Parameter | Type | Exception |
|---|---|---|
value | T? where T : class | ArgumentNullException |
value | T? where T : struct | ArgumentNullException |
IsNotNull
Section titled “IsNotNull”Returns true if the value is not null. Supports null-state analysis via [NotNullWhen(true)].
if (Ensure.IsNotNull(user)){ // 'user' is known to be non-null here Console.WriteLine(user.Name);}String Checks
Section titled “String Checks”ThrowIfNullOrEmpty / ThrowIfNullOrWhiteSpace
Section titled “ThrowIfNullOrEmpty / ThrowIfNullOrWhiteSpace”Ensure.ThrowIfNullOrEmpty(name); // Null or ""Ensure.ThrowIfNullOrWhiteSpace(name); // Null, "", or whitespace only| Method | Throws On |
|---|---|
ThrowIfNullOrEmpty | null, "" |
ThrowIfNullOrWhiteSpace | null, "", " " |
Length Validation
Section titled “Length Validation”Ensure.ThrowIfLongerThan(name, 100); // Max lengthEnsure.ThrowIfShorterThan(name, 2); // Min lengthEnsure.ThrowIfLengthOutOfRange(name, 2, 100); // BothFormat Validation
Section titled “Format Validation”Ensure.ThrowIfNotEmail(email); // RFC 5322 email formatEnsure.ThrowIfNotUrl(url); // Valid URL (HTTP/HTTPS)Ensure.ThrowIfNotUrl(url, httpsOnly: true); // HTTPS onlyEnsure.ThrowIfNotPhone(phone); // E.164 phone formatEnsure.ThrowIfNotCreditCard(card); // Luhn algorithmEnsure.ThrowIfNotMatch(value, @"^\d{5}$"); // Custom regexIs* String Methods
Section titled “Is* String Methods”Ensure.IsNotNullOrEmpty(value) // true if not null/emptyEnsure.IsNotNullOrWhiteSpace(value) // true if not null/empty/whitespaceEnsure.IsLengthInRange(value, min, max)Ensure.IsEmail(value)Ensure.IsUrl(value)Ensure.IsPhone(value)Ensure.IsCreditCard(value)Ensure.IsMatch(value, pattern)Numeric Checks
Section titled “Numeric Checks”All numeric methods use generic constraints for maximum flexibility.
Range Validation
Section titled “Range Validation”Ensure.ThrowIfNegative(value); // value < 0Ensure.ThrowIfNegativeOrZero(value); // value <= 0Ensure.ThrowIfZero(value); // value == 0Ensure.ThrowIfPositive(value); // value > 0Ensure.ThrowIfOutOfRange(value, 0, 100); // value outside [0, 100]Ensure.ThrowIfGreaterThan(value, max); // value > maxEnsure.ThrowIfLessThan(value, min); // value < minWorks with: int, long, decimal, double, float, and any INumber<T> type.
Is* Numeric Methods
Section titled “Is* Numeric Methods”Ensure.IsPositive(value) // value > 0Ensure.IsNegative(value) // value < 0Ensure.IsZero(value) // value == 0Ensure.IsNotZero(value) // value != 0Ensure.IsNotNegative(value) // value >= 0Ensure.IsInRange(value, min, max)Ensure.IsAtLeastMin(value, min)Ensure.IsAtMostMax(value, max)Collection Checks
Section titled “Collection Checks”ThrowIfEmpty / ThrowIfNullOrEmpty
Section titled “ThrowIfEmpty / ThrowIfNullOrEmpty”Ensure.ThrowIfEmpty(items); // Null or no itemsEnsure.ThrowIfNullOrEmpty(items); // Alias for ThrowIfEmptyOptimized overloads for:
IEnumerable<T>- UsesAny()ICollection<T>- UsesCountIReadOnlyCollection<T>- UsesCountIList<T>- UsesCountT[]- UsesLength
IsNotNullOrEmpty
Section titled “IsNotNullOrEmpty”if (Ensure.IsNotNullOrEmpty(items)){ foreach (var item in items) { // Safe to enumerate }}Guid Checks
Section titled “Guid Checks”Ensure.ThrowIfEmpty(id); // Guid.EmptyEnsure.IsNotEmpty(id); // true if not Guid.EmptyDateTime Checks
Section titled “DateTime Checks”Ensure.ThrowIfDefault(date); // default(DateTime)Ensure.ThrowIfInPast(date); // Before DateTime.NowEnsure.ThrowIfInFuture(date); // After DateTime.Now
Ensure.IsNotDefault(date);Ensure.IsPast(date);Ensure.IsFuture(date);Enum Checks
Section titled “Enum Checks”Ensure.ThrowIfUndefined(status); // Not a defined enum valueEnsure.IsDefined(status); // true if definedBoolean Conditions
Section titled “Boolean Conditions”For custom conditions not covered by specific methods:
Ensure.ThrowIfTrue(user.IsDeleted, "User is deleted");Ensure.ThrowIfFalse(user.IsActive, "User is inactive");Equality Checks
Section titled “Equality Checks”Ensure.AreEqual(expected, actual) // true if equalEnsure.AreNotEqual(value1, value2) // true if not equalException Types
Section titled “Exception Types”| Method Pattern | Exception Type |
|---|---|
ThrowIfNull* | ArgumentNullException |
ThrowIfEmpty (collections) | ArgumentNullException or ArgumentException |
ThrowIf*Range* | ArgumentOutOfRangeException |
ThrowIfNegative* | ArgumentOutOfRangeException |
ThrowIfPositive | ArgumentOutOfRangeException |
ThrowIfZero | ArgumentOutOfRangeException |
Other ThrowIf* | ArgumentException |
Check Methods (Pragmatic.Ensure.Result)
Section titled “Check Methods (Pragmatic.Ensure.Result)”The Pragmatic.Ensure.Result package provides validation methods that return VoidResult<TError> instead of throwing. Use for domain validation where failure is expected.
Installation
Section titled “Installation”dotnet add package Pragmatic.Ensure.ResultNull Checks
Section titled “Null Checks”Check.NotNull<T, TError>(T? value, TError error)Check.NotNull<T, TError>(T? value, Func<TError> errorFactory)// Supports both T : class and T? where T : structString Checks
Section titled “String Checks”Check.NotNullOrEmpty<TError>(string? value, TError error)Check.NotNullOrWhiteSpace<TError>(string? value, TError error)Check.LengthInRange<TError>(string? value, int min, int max, TError error)Check.Email<TError>(string? value, TError error)Check.Url<TError>(string? value, TError error, bool requireHttps = false)Check.Phone<TError>(string? value, TError error)Check.Match<TError>(string? value, string pattern, TError error)Numeric Checks
Section titled “Numeric Checks”Check.Positive<T, TError>(T value, TError error) // where T : INumber<T>Check.NotNegative<T, TError>(T value, TError error) // where T : struct, IComparable<T>Check.NotZero<T, TError>(T value, TError error) // where T : struct, IComparable<T>Check.InRange<T, TError>(T value, T min, T max, TError error)Check.AtLeast<T, TError>(T value, T minimum, TError error)Check.AtMost<T, TError>(T value, T maximum, TError error)Collection Checks
Section titled “Collection Checks”Check.NotNullOrEmpty<T, TError>(IEnumerable<T>? value, TError error)Check.NotNullOrEmpty<T, TError>(ICollection<T>? value, TError error)Check.NotNullOrEmpty<T, TError>(IReadOnlyCollection<T>? value, TError error)Check.NotNullOrEmpty<T, TError>(T[]? value, TError error)Other Checks
Section titled “Other Checks”// GuidCheck.NotEmpty<TError>(Guid value, TError error)
// EnumCheck.Defined<TEnum, TError>(TEnum value, TError error)
// Boolean conditionsCheck.That<TError>(bool condition, TError error)Check.That<TError>(bool condition, Func<TError> errorFactory)Check.Not<TError>(bool condition, TError error)
// DateTimeCheck.InPast<TError>(DateTime value, TError error)Check.InFuture<TError>(DateTime value, TError error)Check.InPast<TError>(DateTimeOffset value, TError error)Check.InFuture<TError>(DateTimeOffset value, TError error)Check.NotDefault<TError>(DateTime value, TError error)Check.NotDefault<TError>(DateTimeOffset value, TError error)
// EqualityCheck.Equal<T, TError>(T? value1, T? value2, TError error)Check.NotEqual<T, TError>(T? value1, T? value2, TError error)Usage Example
Section titled “Usage Example”using Pragmatic.Ensure.Result;
// Composable validation chainvar result = Check.NotNull(user, new NotFoundError("User not found")) .Bind(_ => Check.NotNullOrEmpty(user.Email, new ValidationError("Email required"))) .Bind(_ => Check.Email(user.Email, new ValidationError("Invalid email format")));
if (result.IsFailure){ return result.Error;}