Interface MapValidator<T, K, V>

Validates the state of a Map.

interface MapValidator<T extends Map<K, V> | undefined | null, K, V> {
    and(validation: (validator: this) => void): this;
    elseGetFailures(): ValidationFailures;
    elseThrow(): boolean;
    entries(): ArrayValidator<[K, V][], [K, V]>;
    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>;
    isType(expected: Type): this;
    isUndefined(): UnknownValidator<undefined>;
    keys(): ArrayValidator<K[], K>;
    size(): UnsignedNumberValidator;
    validationFailed(): boolean;
    values(): ArrayValidator<V[], V>;
    withContext(value: unknown, name: string): this;
}

Type Parameters

  • T extends Map<K, V> | undefined | null

    the type of the value

  • K

    the type of keys in the map

  • V

    the type of values in the map

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 empty.

    Returns this

    this

    TypeError if the value is undefined or null

    RangeError if value is not empty

  • Ensures that the value is not empty.

    Returns this

    this

    TypeError if the value is undefined or null

    RangeError if value is empty

  • 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