Phantom Stories Library - v0.0.8
    Preparing search index...

    Class Passing<T>

    Represents a passing validation state with a value.

    Type Parameters

    • T

      The type of the data.

    Implements

    Index

    Constructors

    Properties

    type: Passing = ValidationTypes.Passing
    value: T

    Methods

    • Chains a function that returns a Validation, enabling monadic composition. For Passing, applies the function; for Failing, returns unchanged.

      Type Parameters

      • R

      Parameters

      • fn: (x: Passing<T>) => Validation<R>

        Function that takes a Passing and returns a new Validation.

      Returns Validation<R>

      The Validation returned by fn (for Passing) or the same instance (for Failing).

      const passing = Validation.Passing(42);
      const result = passing.chain(v => Validation.Passing(v.value * 2)); // Passing<84>
    • Combines this Validation with another, following semigroup rules. Returns Passing if both are Passing, otherwise returns a Failing with concatenated messages.

      Type Parameters

      • R

      Parameters

      • validation: Validation<R>

        Another Validation to combine with.

      Returns Validation<R>

      A new Validation combining the states.

      const passing = Validation.Passing(42);
      const failing = Validation.Failing(['error']);
      const result = passing.concat(failing); // Failing<['error']>
    • Transforms the Passing value using a function, preserving the Validation structure. Acts as a functor operation, similar to Array.map. Failing returns unchanged.

      Type Parameters

      • R

      Parameters

      • fn: (x: T) => R

        Function to transform the Passing value.

      Returns Passing<R>

      A new Validation with the transformed value (for Passing) or the same instance (for Failing).

      const passing = Validation.Passing(42);
      const result = passing.map(x => x + 1); // Passing<43>
    • Matches the Validation state with a pattern, returning a value. Enables declarative state handling, similar to a switch statement.

      Type Parameters

      • R

      Parameters

      Returns R

      The result of the matching function.

      const validation = Validation.Passing(42);
      const result = validation.matchWith({
      Passing: ({ value }) => value * 2,
      Failing: () => 0,
      }); // 84
    • Matches the Validation state with a partial pattern, performing side effects.

      Parameters

      • pattern: Partial<ValidationPattern<T, void>>

        Partial object with functions for some Validation states.

      Returns void

      Validation.Failing(['error']).matchWithPartial({
      Failing: ({ messages }) => console.log(messages),
      });