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

    Validates the state of a value, recording failures without throwing an error.

    Type Parameters

    • T

      the type of the value

    Implements

    Index

    Constructors

    Properties

    _configuration: Configuration

    The validator configuration.

    context: Map<string, unknown>

    The contextual information of this validator.

    failures: ValidationFailure[]

    The list of validation failures.

    name: string

    The name of the value.

    The application configuration.

    The value being validated.

    Methods

    • Adds a validation failure and throws an error if the validator is configured to throw an error on failure.

      Parameters

      • message: string

        a message that explains what went wrong

      • errorBuilder: ErrorBuilder

        creates the error associated with this failure

      Returns void

    • Adds a RangeError validation failure and throws an error if the validator is configured to throw an error on failure.

      Parameters

      • message: string

        a message that explains what went wrong

      Returns void

    • Adds a TypeError validation failure and throws an error if the validator is configured to throw an error on failure.

      Parameters

      • message: string

        a message that explains what went wrong

      Returns void

    • 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

    • Parameters

      • namePrefix: string

        the string to prepend to the name if the name is null

      • name: null | string

        the name of the value

      • valuePrefix: string

        the string to prepend to the value if the name is null

      • value: unknown

        a value

      Returns string

      the prefixed name if it is defined; otherwise, the prefixed string representation of the value

    • Ensures that a name does not conflict with other variable names already in use by the validator.

      Parameters

      • name: string

        the name of the parameter

      • checkContext: boolean = true

        false to allow the name to be used even if it conflicts with an existing name in the validator context

      Returns JavascriptValidatorsImpl

      the internal validator of the name

      RangeError 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

    • 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