Validator Class
Definition
Provides a generic way to validate different types of arguments passed to members.
public sealed class Validator
- Inheritance
-
Validator
Examples
The following example demonstrates how to use the Validator class to guard against null, empty, and invalid arguments using precondition checks.
using System;
using System.Collections.Generic;
using System.Linq;
using Cuemon;
namespace Contoso.Routing;
public sealed class ValidatorExample
{
public static void Run()
{
string endpoint = NormalizeEndpoint("https://api.cuemon.net", new[] { "v1", "health" });
Console.WriteLine(endpoint);
}
private static string NormalizeEndpoint(string endpoint, IEnumerable<string> segments)
{
Validator.ThrowIfNullOrWhitespace(endpoint);
Validator.ThrowIfSequenceNullOrEmpty(segments);
Validator.ThrowIfNotUri(endpoint, UriKind.Absolute);
return string.Join("/", new[] { endpoint.TrimEnd('/') }.Concat(segments));
}
}
Properties
| Name | Description |
|---|---|
| ThrowIf | Gets the singleton instance of the Validator functionality allowing for extensions methods like: |
Methods
| Name | Description |
|---|---|
| CheckParameter<TResult>(Func<TResult>) | Provides a convenient way to verify a desired state from the provided |
| CheckParameter<T>(T, Action) | Provides a convenient way to verify a desired state from the provided |
| ThrowIfContainsAny(string, char[], StringComparison, string, string) | Validates and throws an ArgumentOutOfRangeException if any of the |
| ThrowIfContainsInterface(Type, Type[], string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfContainsInterface<T>(string, string, params Type[]) | Validates and throws an TypeArgumentOutOfRangeException if the specified |
| ThrowIfContainsInterface<T>(string, params Type[]) | Validates and throws a TypeArgumentOutOfRangeException if the specified |
| ThrowIfContainsReservedKeyword(string, IEnumerable<string>, IEqualityComparer<string>, string, string) | Validates and throws a ArgumentReservedKeywordException if the specified |
| ThrowIfContainsReservedKeyword(string, IEnumerable<string>, string, string) | Validates and throws a ArgumentReservedKeywordException if the specified |
| ThrowIfContainsType(object, Type[], string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfContainsType(Type, Type[], string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfContainsType<T>(string, string, params Type[]) | Validates and throws a TypeArgumentOutOfRangeException if the specified |
| ThrowIfContainsType<T>(string, params Type[]) | Validates and throws a TypeArgumentOutOfRangeException if the specified |
| ThrowIfDifferent(string, string, string, string) | Validates and throws an ArgumentOutOfRangeException if there is a difference between |
| ThrowIfDisposed(bool, object, string) | Validates and throws an ObjectDisposedException if the specified |
| ThrowIfDisposed(bool, Type, string) | Validates and throws an ObjectDisposedException if the specified |
| ThrowIfEmailAddress(string, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfEmpty(string, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfEnumType(Type, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfEnumType<TEnum>(string, string) | Validates and throws a TypeArgumentException if the specified |
| ThrowIfEnum<TEnum>(string, bool, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfEqual<T>(T, T, string, IEqualityComparer<T>, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfFalse(bool, string, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfFalse(Func<bool>, string, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfGreaterThanOrEqual<T>(T, T, string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfGreaterThan<T>(T, T, string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfGuid(string, GuidFormats, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfHex(string, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfInvalidConfigurator<TOptions>(Action<TOptions>, out TOptions, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfInvalidOptions<TOptions>(TOptions, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfInvalidState(bool, string, string) | Validates and throws an InvalidOperationException if the specified |
| ThrowIfLowerThanOrEqual<T>(T, T, string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfLowerThan<T>(T, T, string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfNotBase64String(string, string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfNotBinaryDigits(string, string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfNotContainsAny(string, char[], StringComparison, string, string) | Validates and throws an ArgumentOutOfRangeException if any of the |
| ThrowIfNotContainsInterface(Type, Type[], string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfNotContainsInterface<T>(string, string, params Type[]) | Validates and throws a TypeArgumentOutOfRangeException if the specified |
| ThrowIfNotContainsInterface<T>(string, params Type[]) | Validates and throws a TypeArgumentOutOfRangeException if the specified |
| ThrowIfNotContainsType(object, Type[], string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfNotContainsType(Type, Type[], string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfNotContainsType<T>(string, string, params Type[]) | Validates and throws a TypeArgumentOutOfRangeException if the specified |
| ThrowIfNotContainsType<T>(string, params Type[]) | Validates and throws a TypeArgumentOutOfRangeException if the specified |
| ThrowIfNotDifferent(string, string, string, string) | Validates and throws an ArgumentOutOfRangeException if there is no difference between |
| ThrowIfNotEmailAddress(string, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfNotEnumType(Type, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfNotEnumType<TEnum>(string, string) | Validates and throws a TypeArgumentException if the specified |
| ThrowIfNotEnum<TEnum>(string, bool, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfNotEqual<T>(T, T, string, IEqualityComparer<T>, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfNotGuid(string, GuidFormats, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfNotHex(string, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfNotNumber(string, NumberStyles, IFormatProvider, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfNotSame<T>(T, T, string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfNotUri(string, UriKind, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfNull(object, string, string) | Validates and throws an ArgumentNullException if the specified |
| ThrowIfNullOrEmpty(string, string, string) | Validates and throws either an ArgumentNullException or ArgumentException if the specified |
| ThrowIfNullOrWhitespace(string, string, string) | Validates and throws either an ArgumentNullException or ArgumentException if the specified |
| ThrowIfNull<T>(IDecorator<T>, out T, string, string) | Validates and throws an ArgumentNullException if the specified |
| ThrowIfNumber(string, NumberStyles, IFormatProvider, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfSame<T>(T, T, string, string) | Validates and throws an ArgumentOutOfRangeException if the specified |
| ThrowIfSequenceEmpty<T>(IEnumerable<T>, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfSequenceNullOrEmpty<T>(IEnumerable<T>, string, string) | Validates and throws either an ArgumentNullException or ArgumentException if the specified |
| ThrowIfTrue(bool, string, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfTrue(Func<bool>, string, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfUri(string, UriKind, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowIfWhiteSpace(string, string, string) | Validates and throws an ArgumentException if the specified |
| ThrowWhen(Action<ExceptionCondition<ArgumentException>>) | Validates and throws an ArgumentException (or a derived counterpart) from the specified delegate |