Interface BooleanValidator<T>

Validates the state of a boolean.

interface BooleanValidator<T extends boolean | undefined | null> {
    and(validation: (validator: this) => void): this;
    elseGetFailures(): ValidationFailures;
    elseThrow(): boolean;
    getContext(): Map<string, unknown>;
    getContextAsString(): string;
    getName(): string;
    getValue(): T;
    getValueOrDefault(defaultValue: T): T;
    getValueOrDefault(defaultValue: null | T): null | T;
    isEqualTo(expected: unknown): this;
    isEqualTo(expected: unknown, name: string): this;
    isFalse(): this;
    isInstanceOf<U extends object>(
        expected: ClassConstructor<U>,
    ): UnknownValidator<U>;
    isNotEqualTo(unwanted: unknown): this;
    isNotEqualTo(unwanted: unknown, name: string): this;
    isNotInstanceOf<U extends object>(unwanted: ClassConstructor<U>): this;
    isNotNull(): UnknownValidator<NonNullable<T>>;
    isNotUndefined(): UnknownValidator<Exclude<T, undefined>>;
    isNull(): UnknownValidator<null>;
    isTrue(): this;
    isType(expected: Type): this;
    isUndefined(): UnknownValidator<undefined>;
    validationFailed(): boolean;
    withContext(value: unknown, name: string): this;
}

Type Parameters

  • T extends boolean | undefined | null

    the type of the value

Hierarchy (View Summary)

Methods

  • Facilitates the validation of related properties. For example,

    ```ts requireThat(nameToFrequency, "nameToFrequency"). and(m => m.size().isPositive()). and(m => m.keySet().contains("John")); ```

    Any changes made during the validation process will impact this validator.

    Parameters

    • validation: (validator: this) => void

      the nested validation

    Returns this

    this

    TypeError if validation is undefined or null

  • Throws an error if a validation failed; otherwise, returns true.

    Returns boolean

    true if the validation passed

    RangeError if a method precondition, class invariant, or method postcondition was violated

    MultipleFailuresError if more than one validation failed. This error contains an array of the failures.

  • Returns the contextual information for upcoming validations carried out by this validator. The contextual information is a map of key-value pairs that can provide more details about validation failures. For example, if the message is "Password may not be empty" and the map contains the key-value pair {"username": "john.smith"}, the error message would be:

    Password may not be empty
    username: john.smith

    Returns Map<string, unknown>

    an unmodifiable map from each entry's name to its value

    Validators.getContext

  • Ensures that the value is false.

    Returns this

    this

    TypeError if the value is undefined or null

    RangeError if the value is true

  • Ensures that the value is true.

    Returns this

    this

    TypeError if the value is undefined or null

    RangeError if the value is false

  • Sets the contextual information for upcoming validations.

    This method adds contextual information to error messages. The contextual information is stored as key-value pairs in a map. Values set by this method override any values that are set using Validators.withContext.

    There is no way to remove contextual information from a validator. Thread-level contextual information is removed automatically.

    Parameters

    • value: unknown

      the value of the entry

    • name: string

      the name of an entry

    Returns this

    this

    TypeError if name is undefined or null

    RangeError if name:

    • contains whitespace
    • is empty
    • is already in use by the value being validated or the validator context