Interface ArrayValidator<T, E>

Validates the state of an array.

interface ArrayValidator<T extends E[] | undefined | null, E> {
    and(validation: (validator: this) => void): this;
    contains(expected: E): this;
    contains(expected: E, name: string): this;
    containsAll(expected: Set<E>): this;
    containsAll(expected: E[]): this;
    containsAll(expected: Set<E>, name: string): this;
    containsAll(expected: E[], name: string): this;
    containsAny(expected: Set<E>): this;
    containsAny(expected: E[]): this;
    containsAny(expected: Set<E>, name: string): this;
    containsAny(expected: E[], name: string): this;
    containsExactly(expected: Set<E>): this;
    containsExactly(expected: E[]): this;
    containsExactly(expected: Set<E>, name: string): this;
    containsExactly(expected: E[], name: string): this;
    doesNotContain(unwanted: E): this;
    doesNotContain(unwanted: E, name: string): this;
    doesNotContainAll(unwanted: Set<E>): this;
    doesNotContainAll(unwanted: E[]): this;
    doesNotContainAll(unwanted: Set<E>, name: string): this;
    doesNotContainAll(unwanted: E[], name: string): this;
    doesNotContainAny(unwanted: Set<E>): this;
    doesNotContainAny(unwanted: E[]): this;
    doesNotContainAny(unwanted: Set<E>, name: string): this;
    doesNotContainAny(unwanted: E[], name: string): this;
    doesNotContainDuplicates(): this;
    doesNotContainExactly(unwanted: Set<E>): this;
    doesNotContainExactly(unwanted: E[]): this;
    doesNotContainExactly(unwanted: Set<E>, name: string): this;
    doesNotContainExactly(unwanted: E[], name: string): 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;
    isEmpty(): this;
    isEqualTo(expected: unknown): this;
    isEqualTo(expected: unknown, name: string): this;
    isInstanceOf<U extends object>(
        expected: ClassConstructor<U>,
    ): UnknownValidator<U>;
    isNotEmpty(): this;
    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>;
    isSorted(comparator: (first: unknown, second: unknown) => number): this;
    isType(expected: Type): this;
    isUndefined(): UnknownValidator<undefined>;
    length(): UnsignedNumberValidator;
    size(): UnsignedNumberValidator;
    validationFailed(): boolean;
    withContext(value: unknown, name: string): this;
}

Type Parameters

  • T extends E[] | undefined | null

    the type of the value

  • E

    the type of elements in the collection

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

  • Ensures that the collection contains an element.

    Parameters

    • expected: E

      the element

    Returns this

    this

    TypeError if the value or expected are undefined or null

    RangeError if the collection does not contain expected

  • Ensures that the collection contains an element.

    Parameters

    • expected: E

      the element

    • name: string

      the name of the element

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection does not contain expected

  • Ensures that the collection contains all the elements in expected.

    Parameters

    • expected: Set<E>

      the desired elements

    Returns this

    this

    TypeError if the value or expected are undefined or null

    RangeError if the collection does not contain all the elements in expected

  • Ensures that the collection contains all the elements in expected.

    Parameters

    • expected: E[]

      the desired elements

    Returns this

    this

    TypeError if the value or expected are undefined or null

    RangeError if the collection does not contain all the elements in expected

  • Ensures that the collection contains all the elements in expected.

    Parameters

    • expected: Set<E>

      the desired elements

    • name: string

      the name of the expected collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection does not contain all elements in expected

  • Ensures that the collection contains all the elements in expected.

    Parameters

    • expected: E[]

      the desired elements

    • name: string

      the name of the expected collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection does not contain all elements in expected

  • Ensures that the collection contains any elements in expected.

    Parameters

    • expected: Set<E>

      the desired elements

    Returns this

    this

    TypeError if the value or expected are undefined or null

    RangeError if the collection does not contain any element in expected

  • Ensures that the collection contains any elements in expected.

    Parameters

    • expected: E[]

      the desired elements

    Returns this

    this

    TypeError if the value or expected are undefined or null

    RangeError if the collection does not contain any element in expected

  • Ensures that the collection contains at least one element in expected.

    Parameters

    • expected: Set<E>

      the desired elements

    • name: string

      the name of the expected collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection does not contain any element in expected

  • Ensures that the collection contains at least one element in expected.

    Parameters

    • expected: E[]

      the desired elements

    • name: string

      the name of the expected collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection does not contain any element in expected

  • Ensures that the collection consists of the same elements as expected, irrespective of their order.

    In contrast, isEqualTo() requires the same element ordering.

    Parameters

    • expected: Set<E>

      the desired elements

    Returns this

    this

    TypeError if the value or expected are undefined or null

    RangeError if:

    • the collection is missing any element in expected
    • the collection contains any element that is not in expected

  • Ensures that the collection consists of the same elements as expected, irrespective of their order.

    In contrast, isEqualTo() requires the same element ordering.

    Parameters

    • expected: E[]

      the desired elements

    Returns this

    this

    TypeError if the value or expected are undefined or null

    RangeError if:

    • the collection is missing any element in expected
    • the collection contains any element that is not in expected

  • Ensures that the collection consists of the same elements as expected, irrespective of their order.

    In contrast, isEqualTo() requires the same element ordering.

    Parameters

    • expected: Set<E>

      the desired elements

    • name: string

      the name of the expected collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection and expected contain different elements, irrespective of their order

  • Ensures that the collection consists of the same elements as expected, irrespective of their order.

    In contrast, isEqualTo() requires the same element ordering.

    Parameters

    • expected: E[]

      the desired elements

    • name: string

      the name of the expected collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection and expected contain different elements, irrespective of their order

  • Ensures that the collection does not contain unwanted.

    Parameters

    • unwanted: E

      the unwanted element

    Returns this

    this

    TypeError if the value or unwanted are undefined or null

    RangeError if the collection contains unwanted

  • Ensures that the collection does not contain unwanted.

    Parameters

    • unwanted: E

      the unwanted element

    • name: string

      the name of the element

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

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

  • Allows the collection to contain some, but not all, elements from a collection.

    Parameters

    • unwanted: Set<E>

      the unwanted elements

    Returns this

    this

    TypeError if the value or unwanted are undefined or null

    RangeError if the collection contains all the elements of unwanted

  • Allows the collection to contain some, but not all, elements from a collection.

    Parameters

    • unwanted: E[]

      the unwanted elements

    Returns this

    this

    TypeError if the value or unwanted are undefined or null

    RangeError if the collection contains all the elements of unwanted

  • Allows the collection to contain some, but not all, elements from a collection.

    Parameters

    • unwanted: Set<E>

      the unwanted elements

    • name: string

      the name of the unwanted collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection contains all the elements in unwanted

  • Allows the collection to contain some, but not all, elements from a collection.

    Parameters

    • unwanted: E[]

      the unwanted elements

    • name: string

      the name of the unwanted collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection contains all the elements in unwanted

  • Ensures that the collection does not contain any of the elements in unwanted.

    Parameters

    • unwanted: Set<E>

      the unwanted elements

    Returns this

    this

    TypeError if the value or unwanted are undefined or null

    RangeError if the collection contains any of the elements in unwanted

  • Ensures that the collection does not contain any of the elements in unwanted.

    Parameters

    • unwanted: E[]

      the unwanted elements

    Returns this

    this

    TypeError if the value or unwanted are undefined or null

    RangeError if the collection contains any of the elements in unwanted

  • Ensures that the collection does not contain any of the elements in unwanted.

    Parameters

    • unwanted: Set<E>

      the unwanted elements

    • name: string

      the name of the unwanted collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection contains any of the elements in unwanted

  • Ensures that the collection does not contain any of the elements in unwanted.

    Parameters

    • unwanted: E[]

      the unwanted elements

    • name: string

      the name of the unwanted collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection contains any of the elements in unwanted

  • Ensures that the collection and unwanted consist of different elements, irrespective of their order.

    Parameters

    • unwanted: Set<E>

      the unwanted elements

    Returns this

    this

    TypeError if the value or unwanted are undefined or null

    RangeError if the collection consists of the same elements as unwanted, irrespective of their order

  • Ensures that the collection and unwanted consist of different elements, irrespective of their order.

    Parameters

    • unwanted: E[]

      the unwanted elements

    Returns this

    this

    TypeError if the value or unwanted are undefined or null

    RangeError if the collection consists of the same elements as unwanted, irrespective of their order

  • Ensures that the collection and unwanted consist of different elements, irrespective of their order.

    Parameters

    • unwanted: Set<E>

      the unwanted elements

    • name: string

      the name of the unwanted collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection consists of the same elements as unwanted, irrespective of their order

  • Ensures that the collection and unwanted consist of different elements, irrespective of their order.

    Parameters

    • unwanted: E[]

      the unwanted elements

    • name: string

      the name of the unwanted collection

    Returns this

    this

    TypeError if the value or any of the arguments are undefined or null

    RangeError if:

    • name is empty
    • name contains whitespace
    • name is already in use by the value being validated or the validator context
    • the collection consists of the same elements as unwanted, irrespective of their order

  • 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 array is sorted.

    Parameters

    • comparator: (first: unknown, second: unknown) => number

      a function that returns a negative number if first should come before second, zero or NaN if the two values are equal, or a positive number if first should come after second.

    Returns this

    this

    TypeError if the value or comparator are undefined or null

    RangeError if the collection is not sorted

  • 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