@cowwoc/requirements - v4.0.12
    Preparing search index...

    Interface StringValidator<T>

    Validates the state of a string.

    interface StringValidator<T extends string | undefined | null> {
        and(validation: (validator: this) => void): this;
        contains(expected: string): this;
        doesNotContain(unwanted: string): this;
        doesNotContainWhitespace(): this;
        doesNotEndWith(suffix: string): this;
        doesNotStartWith(prefix: string): this;
        elseGetFailures(): ValidationFailures;
        elseThrow(): boolean;
        endsWith(suffix: string): this;
        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>;
        isTrimmed(): this;
        isType(expected: Type): this;
        isUndefined(): UnknownValidator<undefined>;
        length(): UnsignedNumberValidator;
        matches(regex: RegExp): this;
        startsWith(prefix: string): this;
        validationFailed(): boolean;
        withContext(value: unknown, name: string): this;
    }

    Type Parameters

    • T extends string | undefined | null

    Hierarchy (View Summary)

    Index

    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 value contains some substring.

      Parameters

      • expected: string

        the string that the value must contain

      Returns this

      this

      TypeError if the value or expected are undefined or null

      RangeError if the value does not contain expected

    • Ensures that the value does not contain some substring.

      Parameters

      • unwanted: string

        the string that the value may not contain

      Returns this

      this

      TypeError if the value or unwanted are undefined or null

      RangeError if the value contains unwanted

    • Ensures that the value does not contain whitespace characters.

      Returns this

      this

      NullPointerException if the value is undefined or null

      IllegalArgumentException if the value contains whitespace characters

    • Ensures that the value does not end with some suffix.

      Parameters

      • suffix: string

        the value that the string may not end with

      Returns this

      this

      TypeError if the value or suffix are undefined or null

      RangeError if the value ends with suffix

    • Ensures that the value does not start with some prefix.

      Parameters

      • prefix: string

        the value that the string may not start with

      Returns this

      this

      TypeError if the value or prefix are undefined or null

      RangeError if the value starts with prefix

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

    • Ensures that the value ends with some suffix.

      Parameters

      • suffix: string

        the value that the string must end with

      Returns this

      this

      TypeError if the value or suffix are undefined or null

      RangeError if the value does not end with suffix

    • 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 the value is not empty

    • Ensures that the value is not empty.

      Returns this

      this

      TypeError if the value is undefined or null

      RangeError if the value is empty

    • Ensures that the value does not contain leading or trailing whitespace.

      Returns this

      this

      TypeError if the value is undefined or null

      RangeError if the value contains leading or trailing whitespace

      isEmpty

    • Ensures that the value matches a regular expression.

      Parameters

      • regex: RegExp

        the regular expression

      Returns this

      this

      TypeError if the value is undefined or null

      RangeError if the value does not match regex

    • Ensures that the value starts with some prefix.

      Parameters

      • prefix: string

        the value that the string must start with

      Returns this

      this

      TypeError if the value or prefix are undefined or null

      RangeError if the value does not start with prefix

    • 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