The Result of a validation, either successful or failed.

  • isSuccess => has a value (getValue())
  • !isSuccess => has an error(string) and throws Error when getValue() is called
  • To use the getValue() method, which returns the vaule of Result.ok(value), you need to check if the result was successful by calling isSuccess().

Type Parameters

  • T

Hierarchy

  • Result

Properties

error: string

💬 The error message that was given in Result.fail(error)

Methods

  • 💬 Chains multiple callbacks (which return Results) together (on this Result), where every previous value is forwarded to the next callback.

    • if one of the callback Results fails, the chain will return it
    • on success, the last callbacks Result will be returned

    Example

    // every validate*** returns a Result<number>:
    const veryValid: Result<number> = this.validateNumber(420).chain(
    (valid) => this.validateIntegerAndRound(valid),
    (valid) => this.validateInterval(valid, {min: 69})
    );

    Type Parameters

    • F extends ((value: T) => Result<T>)

    Parameters

    • Rest ...members: F[]

      to chain together

    Returns Result<T>

  • 💬 Converts the value of this Result to another Result, if this Result is successful.

    Returns this failed Result otherwise.

    Example

    // Result<number> => Result<Integer>
    this.validate(420).convertTo((valid) => new Integer(valid));

    Type Parameters

    • R

    Parameters

    • callback: ((valid: T) => R)

      for the conversion of the value

        • (valid: T): R
        • Parameters

          • valid: T

          Returns R

    Returns Result<R>

  • 💬 returns the value, but only if this method is called within an enclosure with isSuccess()

    Returns

    the value provided in Result.ok(value)

    Throws

    an Error if called on a failed Result!

    Returns T

  • 💬 Indicates if this Result was successful and converts it to reveal the getValue() method.

    Important:

    • To use the getValue() method, which returns the actual value, you need to check if the result was successful.
    • This is necessary to avoid trying to get the value from a result that failed.
    • (Result.fail('message').getValue() throws an Error because there is no value!)
    • For this reason, this method has a return value of something like is Result+getValue() - i.e. after checking, getValue() is "activated".

    Returns

    true if the Result is successful + activation of the getValue() method on the Result

    Example

    const result = Result.ok('test');
    result.getValue(); // ❌ TS: Property 'getValue' is protected and only accessible ...
    if (result.isSuccess()) {
    result.getValue(); // ✅ fine
    } else {
    result.getValue(); // ❌ same
    }

    Returns this is Result<T> & {
        getValue: (() => T);
    }

  • 💬 Calls the given callback if the Result fails.

    • provides the error for the callback
    • can be chained with the corresponding onSuccess() method

    Example

    Integer.validate(42) .onFail((error) => console.error(error)) .onSuccess((value) => console.log(Yes ${value} is an integer));

    Parameters

    • callback: ((error: string) => void)

      to be called if this Result fails

        • (error: string): void
        • Parameters

          • error: string

          Returns void

    Returns Omit<Result<T>, "onFail">

  • 💬 Calls the given callback if the Result is successful.

    • provides the value for the callback
    • can be chained with the corresponding onFail() method

    Example

    Integer.validate(42) .onSuccess((value) => console.log(Yes ${value} is an integer)); .onFail((error) => console.error(error))

    Parameters

    • callback: ((value: T) => void)

      to be called if this Result is successful

        • (value: T): void
        • Parameters

          • value: T

          Returns void

    Returns Omit<Result<T>, "onSuccess">

  • 💬 Combines multiple Results in a map, checks every Result and:

    • success: returns a single Result with a Record of all Results' values
    • failure: returns a single Result with the error of the first failed Result

    Example

    const book = Result.combine({
    title: Result.ok('MyBook'),
    author: Result.ok('My Self'),
    })

    book.getValue() // === {title: 'MyBook', author: 'My Self'}

    Type Parameters

    • ResultMap extends Record<string, Result<any>>

    Parameters

    • map: ResultMap

      a Record of multiple Results

    Returns Result<ValuesOf<ResultMap>>

  • 💬 Static constructor for a failed validation *

    Returns

    a failed Result

    Type Parameters

    • U

    Parameters

    • error: string

      reason why the validation failed

    Returns Result<U>

  • 💬 Static constructor for a successful validated value

    Returns

    a successful Result

    Type Parameters

    • U

    Parameters

    • Optional value: U

      of the successful validation

    Returns Result<U>

Generated using TypeDoc