Table of Contents

Validator Class

Definition

Namespace
Cuemon
Assemblies
Cuemon.Kernel.dll
Source
src/Cuemon.Kernel/Validator.cs

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: Validator.ThrowIf.InvalidJsonDocument().

Methods

Name Description
CheckParameter<TResult>(Func<TResult>)

Provides a convenient way to verify a desired state from the provided validator while returning a result that reflects this.

CheckParameter<T>(T, Action)

Provides a convenient way to verify a desired state from the provided validator while returning the specified argument unaltered.

ThrowIfContainsAny(string, char[], StringComparison, string, string)

Validates and throws an ArgumentOutOfRangeException if any of the characters occurs within the argument.

ThrowIfContainsInterface(Type, Type[], string, string)

Validates and throws an ArgumentOutOfRangeException if the specified argument is contained within at least one of the specified types.

ThrowIfContainsInterface<T>(string, string, params Type[])

Validates and throws an TypeArgumentOutOfRangeException if the specified T is contained within at least one of the specified types.

ThrowIfContainsInterface<T>(string, params Type[])

Validates and throws a TypeArgumentOutOfRangeException if the specified T is contained within at least one of the specified types.

ThrowIfContainsReservedKeyword(string, IEnumerable<string>, IEqualityComparer<string>, string, string)

Validates and throws a ArgumentReservedKeywordException if the specified argument is found in the sequence of reservedKeywords.

ThrowIfContainsReservedKeyword(string, IEnumerable<string>, string, string)

Validates and throws a ArgumentReservedKeywordException if the specified argument is found in the sequence of reservedKeywords.

ThrowIfContainsType(object, Type[], string, string)

Validates and throws an ArgumentOutOfRangeException if the specified argument is contained within at least one of the specified types.

ThrowIfContainsType(Type, Type[], string, string)

Validates and throws an ArgumentOutOfRangeException if the specified argument is contained within at least one of the specified types.

ThrowIfContainsType<T>(string, string, params Type[])

Validates and throws a TypeArgumentOutOfRangeException if the specified T is contained within at least one of the specified types.

ThrowIfContainsType<T>(string, params Type[])

Validates and throws a TypeArgumentOutOfRangeException if the specified T is contained within at least one of the specified types.

ThrowIfDifferent(string, string, string, string)

Validates and throws an ArgumentOutOfRangeException if there is a difference between second and first.

ThrowIfDisposed(bool, object, string)

Validates and throws an ObjectDisposedException if the specified condition is true.

ThrowIfDisposed(bool, Type, string)

Validates and throws an ObjectDisposedException if the specified condition is true.

ThrowIfEmailAddress(string, string, string)

Validates and throws an ArgumentException if the specified argument has the format of an email address.

ThrowIfEmpty(string, string, string)

Validates and throws an ArgumentException if the specified argument is empty.

ThrowIfEnumType(Type, string, string)

Validates and throws an ArgumentException if the specified argument represents an enumeration.

ThrowIfEnumType<TEnum>(string, string)

Validates and throws a TypeArgumentException if the specified TEnum represents an enumeration.

ThrowIfEnum<TEnum>(string, bool, string, string)

Validates and throws an ArgumentException if the specified argument represents an enumeration.

ThrowIfEqual<T>(T, T, string, IEqualityComparer<T>, string)

Validates and throws an ArgumentOutOfRangeException if the specified x object are equal to the y object.

ThrowIfFalse(bool, string, string, string)

Validates and throws an ArgumentException if the specified condition is false.

ThrowIfFalse(Func<bool>, string, string, string)

Validates and throws an ArgumentException if the specified predicate returns false.

ThrowIfGreaterThanOrEqual<T>(T, T, string, string)

Validates and throws an ArgumentOutOfRangeException if the specified x is greater than or equal to y.

ThrowIfGreaterThan<T>(T, T, string, string)

Validates and throws an ArgumentOutOfRangeException if the specified x is greater than y.

ThrowIfGuid(string, GuidFormats, string, string)

Validates and throws an ArgumentException if the specified argument has the format of a Guid.

ThrowIfHex(string, string, string)

Validates and throws an ArgumentException if the specified argument is hexadecimal.

ThrowIfInvalidConfigurator<TOptions>(Action<TOptions>, out TOptions, string, string)

Validates and throws an ArgumentException if the specified argument results in an instance of invalid options.

ThrowIfInvalidOptions<TOptions>(TOptions, string, string)

Validates and throws an ArgumentException if the specified argument are not in a valid state.

ThrowIfInvalidState(bool, string, string)

Validates and throws an InvalidOperationException if the specified condition is true.

ThrowIfLowerThanOrEqual<T>(T, T, string, string)

Validates and throws an ArgumentOutOfRangeException if the specified x is lower than or equal to y.

ThrowIfLowerThan<T>(T, T, string, string)

Validates and throws an ArgumentOutOfRangeException if the specified x is lower than y.

ThrowIfNotBase64String(string, string, string)

Validates and throws an ArgumentOutOfRangeException if the specified argument consist of anything besides a base-64 structure.

ThrowIfNotBinaryDigits(string, string, string)

Validates and throws an ArgumentOutOfRangeException if the specified argument consist of anything besides binary digits.

ThrowIfNotContainsAny(string, char[], StringComparison, string, string)

Validates and throws an ArgumentOutOfRangeException if any of the characters does not occur within the argument.

ThrowIfNotContainsInterface(Type, Type[], string, string)

Validates and throws an ArgumentOutOfRangeException if the specified argument is not contained within at least one of the specified types.

ThrowIfNotContainsInterface<T>(string, string, params Type[])

Validates and throws a TypeArgumentOutOfRangeException if the specified T is not contained within at least one of the specified types.

ThrowIfNotContainsInterface<T>(string, params Type[])

Validates and throws a TypeArgumentOutOfRangeException if the specified T is not contained within at least one of the specified types.

ThrowIfNotContainsType(object, Type[], string, string)

Validates and throws an ArgumentOutOfRangeException if the specified argument is not contained within at least one of the specified types.

ThrowIfNotContainsType(Type, Type[], string, string)

Validates and throws an ArgumentOutOfRangeException if the specified argument is not contained within at least one of the specified types.

ThrowIfNotContainsType<T>(string, string, params Type[])

Validates and throws a TypeArgumentOutOfRangeException if the specified T is not contained within at least one of the specified types.

ThrowIfNotContainsType<T>(string, params Type[])

Validates and throws a TypeArgumentOutOfRangeException if the specified T is not contained within at least one of the specified types.

ThrowIfNotDifferent(string, string, string, string)

Validates and throws an ArgumentOutOfRangeException if there is no difference between second and first.

ThrowIfNotEmailAddress(string, string, string)

Validates and throws an ArgumentException if the specified argument does not have the format of an email address.

ThrowIfNotEnumType(Type, string, string)

Validates and throws an ArgumentException if the specified argument does not represents an enumeration.

ThrowIfNotEnumType<TEnum>(string, string)

Validates and throws a TypeArgumentException if the specified TEnum does not represents an enumeration.

ThrowIfNotEnum<TEnum>(string, bool, string, string)

Validates and throws an ArgumentException if the specified argument does not represents an enumeration.

ThrowIfNotEqual<T>(T, T, string, IEqualityComparer<T>, string)

Validates and throws an ArgumentOutOfRangeException if the specified x object are not equal to the y object.

ThrowIfNotGuid(string, GuidFormats, string, string)

Validates and throws an ArgumentException if the specified argument does not have the format of a Guid.

ThrowIfNotHex(string, string, string)

Validates and throws an ArgumentException if the specified argument is not hexadecimal.

ThrowIfNotNumber(string, NumberStyles, IFormatProvider, string, string)

Validates and throws an ArgumentException if the specified argument is not a number.

ThrowIfNotSame<T>(T, T, string, string)

Validates and throws an ArgumentOutOfRangeException if the specified x object are not of the same instance as the y object.

ThrowIfNotUri(string, UriKind, string, string)

Validates and throws an ArgumentException if the specified argument does not have the format of a Uri.

ThrowIfNull(object, string, string)

Validates and throws an ArgumentNullException if the specified argument is null.

ThrowIfNullOrEmpty(string, string, string)

Validates and throws either an ArgumentNullException or ArgumentException if the specified argument is respectively null or empty.

ThrowIfNullOrWhitespace(string, string, string)

Validates and throws either an ArgumentNullException or ArgumentException if the specified argument is respectively null, empty or consist only of white-space characters.

ThrowIfNull<T>(IDecorator<T>, out T, string, string)

Validates and throws an ArgumentNullException if the specified argument is null.

ThrowIfNumber(string, NumberStyles, IFormatProvider, string, string)

Validates and throws an ArgumentException if the specified argument is a number.

ThrowIfSame<T>(T, T, string, string)

Validates and throws an ArgumentOutOfRangeException if the specified x object are of the same instance as the y object.

ThrowIfSequenceEmpty<T>(IEnumerable<T>, string, string)

Validates and throws an ArgumentException if the specified argument has no elements.

ThrowIfSequenceNullOrEmpty<T>(IEnumerable<T>, string, string)

Validates and throws either an ArgumentNullException or ArgumentException if the specified argument is respectively null or has no elements.

ThrowIfTrue(bool, string, string, string)

Validates and throws an ArgumentException if the specified condition is true.

ThrowIfTrue(Func<bool>, string, string, string)

Validates and throws an ArgumentException if the specified predicate returns true.

ThrowIfUri(string, UriKind, string, string)

Validates and throws an ArgumentException if the specified argument has the format of a Uri.

ThrowIfWhiteSpace(string, string, string)

Validates and throws an ArgumentException if the specified argument consist only of white-space characters.

ThrowWhen(Action<ExceptionCondition<ArgumentException>>)

Validates and throws an ArgumentException (or a derived counterpart) from the specified delegate condition.