Interface NumberValidator<T>

Validates the state of a number.

interface NumberValidator<T extends number | 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;
    isBetween(minimumInclusive: number, maximumExclusive: number): this;
    isBetween(
        minimum: number,
        minimumInclusive: boolean,
        maximum: number,
        maximumInclusive: boolean,
    ): this;
    isEqualTo(expected: unknown): this;
    isEqualTo(expected: unknown, name: string): this;
    isFinite(): this;
    isGreaterThan(minimumExclusive: number): this;
    isGreaterThan(minimumExclusive: number, name: string): this;
    isGreaterThanOrEqualTo(minimumInclusive: number): this;
    isGreaterThanOrEqualTo(minimumInclusive: number, name: string): this;
    isInfinite(): this;
    isInstanceOf<U extends object>(
        expected: ClassConstructor<U>,
    ): UnknownValidator<U>;
    isLessThan(maximumExclusive: number): this;
    isLessThan(maximumExclusive: number, name: string): this;
    isLessThanOrEqualTo(maximumInclusive: number): this;
    isLessThanOrEqualTo(maximumInclusive: number, name: string): this;
    isMultipleOf(factor: number): this;
    isMultipleOf(factor: number, name: string): this;
    isNegative(): this;
    isNotEqualTo(unwanted: unknown): this;
    isNotEqualTo(unwanted: unknown, name: string): this;
    isNotInstanceOf<U extends object>(unwanted: ClassConstructor<U>): this;
    isNotMultipleOf(factor: number): this;
    isNotMultipleOf(factor: number, name: string): this;
    isNotNegative(): this;
    isNotNull(): UnknownValidator<NonNullable<T>>;
    isNotNumber(): this;
    isNotPositive(): this;
    isNotUndefined(): UnknownValidator<Exclude<T, undefined>>;
    isNotZero(): this;
    isNull(): UnknownValidator<null>;
    isNumber(): this;
    isPositive(): this;
    isType(expected: Type): this;
    isUndefined(): UnknownValidator<undefined>;
    isZero(): this;
    validationFailed(): boolean;
    withContext(value: unknown, name: string): this;
}

Type Parameters

  • T extends number | 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 within a range.

    Parameters

    • minimumInclusive: number

      the lower bound of the range (inclusive)

    • maximumExclusive: number

      the upper bound of the range (exclusive)

    Returns this

    this

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

    RangeError if minimumInclusive is greater than maximumExclusive. If the value is less than minimumInclusive. If the value is greater than or equal to maximumExclusive.

  • Ensures that the value is within a range.

    Parameters

    • minimum: number

      the lower bound of the range

    • minimumInclusive: boolean

      true if the lower bound of the range is inclusive

    • maximum: number

      the upper bound of the range

    • maximumInclusive: boolean

      true if the upper bound of the range is inclusive

    Returns this

    this

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

    RangeError if minimum is greater than maximum. If minimumInclusive is true, and the value is less than minimum. If minimumInclusive is false, and the value is less than or equal to minimum. If maximumInclusive is true and the value is greater than maximum. If maximumInclusive is false, and the value is greater than or equal to maximum.

  • Ensures that the value is greater than a lower bound.

    Parameters

    • minimumExclusive: number

      the exclusive lower bound

    Returns this

    this

    TypeError if the value or minimumExclusive are undefined or null

    RangeError if the value is less than minimumExclusive

  • Ensures that the value is greater than a lower bound.

    Parameters

    • minimumExclusive: number

      the exclusive lower bound

    • name: string

      the name of the lower bound

    Returns this

    this

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

    RangeError if name contains whitespace or is empty. If the value is less than minimumExclusive.

  • Ensures that the value is greater than or equal to a minimum value.

    Parameters

    • minimumInclusive: number

      the minimum value

    Returns this

    this

    TypeError if the value or minimumInclusive are undefined or null

    RangeError if the value is less than minimumInclusive

  • Ensures that the value is greater than or equal a minimum value.

    Parameters

    • minimumInclusive: number

      the minimum value

    • name: string

      the name of the minimum value

    Returns this

    this

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

    RangeError if name contains whitespace or is empty. If the value is less than minimumInclusive.

  • Ensures that the value is less than an upper bound.

    Parameters

    • maximumExclusive: number

      the exclusive upper bound

    Returns this

    this

    TypeError if the value or maximumExclusive are undefined or null

    RangeError if the value is greater than or equal to maximumExclusive

  • Ensures that the value is less than an upper bound.

    Parameters

    • maximumExclusive: number

      the exclusive upper bound

    • name: string

      the name of the upper bound

    Returns this

    this

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

    RangeError if name contains a leading, trailing whitespace or is empty. If the value is greater than or equal to maximumExclusive.

  • Ensures that the value is less than or equal to a maximum value.

    Parameters

    • maximumInclusive: number

      the inclusive upper value

    Returns this

    this

    TypeError if the value or maximumInclusive are undefined or null

    RangeError if the value is greater than maximumInclusive

  • Ensures that the value is less than or equal to a maximum value.

    Parameters

    • maximumInclusive: number

      the maximum value

    • name: string

      the name of the maximum value

    Returns this

    this

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

    RangeError if name contains whitespace or is empty. If the value is greater than maximumInclusive.

  • Ensures that the value is a multiple of factor.

    Parameters

    • factor: number

      the number being multiplied

    Returns this

    this

    TypeError if the value or factor are undefined or null

    RangeError if the value is not a multiple of factor

  • Ensures that the value is a multiple of factor.

    Parameters

    • factor: number

      the number being multiplied

    • name: string

      the name of the factor

    Returns this

    this

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

    RangeError if name contains whitespace or is empty. If the value is not a multiple of factor.

  • Ensures that the value is not a multiple of factor.

    Parameters

    • factor: number

      the number being multiplied

    Returns this

    this

    TypeError if the value or factor are undefined or null

    RangeError if the value is a multiple of factor

  • Ensures that the value is not a multiple of factor.

    Parameters

    • factor: number

      the number being multiplied

    • name: string

      the name of the factor

    Returns this

    this

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

    RangeError if name contains whitespace or is empty. If the value is a multiple of factor.

  • Ensures that the value is the result of a mathematically undefined numerical operation (such as division by zero) or don't have a representation in real numbers (such as the square-root of -1).

    Returns this

    this

    NullPointerError if the value is undefined or null

    RangeError if value is a well-defined number

  • 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