Interface Validation<E,T>

Type Parameters:
E - the type of values in the case of invalid
T - the type of values in the case of valid
All Superinterfaces:
Iterable<T>, Serializable, Value<T>
All Known Implementing Classes:
Validation.Invalid, Validation.Valid

public interface Validation<E,T> extends Value<T>, Serializable
An implementation similar to Scalaz's Validation control.

The Validation type is an applicative functor, not a Monad. While a Monad short-circuits on the first error, an applicative functor accumulates all errors, making it particularly useful for validation scenarios where all errors should be reported.


// Validation construction:

// Valid
Validation<String, Integer> valid = Validation.valid(5);

// Invalid
Validation<List<String>, Integer>invalid = Validation.invalid(List.of("error1", "error2"));

// Validation combination:

Validation<String, String> valid1 = Validation.valid("John");
Validation<String, Integer> valid2 = Validation.valid(5);
Validation<String, Option<String>> valid3 = Validation.valid(Option.of("123 Fake St."));
Function3<String, Integer, Option<String>, Person> f = ...;

Validation<List<String>, String> result =
    valid1.combine(valid2).ap((name, age) -> "Name: " + name + " Age: " + age);

Validation<List<String>, Person> result2 =
    valid1.combine(valid2).combine(valid3).ap(f);

// Another way to combine validations:
Validation<List<String>, Person> result3 =
    Validation.combine(valid1, valid2, valid3).ap(f);

See Also:
  • Field Details

    • serialVersionUID

      static final long serialVersionUID
      The serial version UID for serialization.
      See Also:
  • Method Details

    • valid

      static <E,T> Validation<E,T> valid(T value)
      Creates a Validation.Valid that contains the given value.
      Type Parameters:
      E - type of the error
      T - type of the given value
      Parameters:
      value - A value
      Returns:
      Valid(value)
    • invalid

      static <E,T> Validation<E,T> invalid(E error)
      Creates an Validation.Invalid that contains the given error.
      Type Parameters:
      E - type of the given error
      T - type of the value
      Parameters:
      error - An error
      Returns:
      Invalid(error)
      Throws:
      NullPointerException - if error is null
    • fromEither

      static <E,T> Validation<E,T> fromEither(@NonNull Either<E,T> either)
      Creates a Validation of an Either.
      Type Parameters:
      E - error type
      T - value type
      Parameters:
      either - An Either
      Returns:
      A Valid(either.get()) if either is a Right, otherwise Invalid(either.getLeft()).
      Throws:
      NullPointerException - if either is null
    • fromTry

      static <T> Validation<Throwable, T> fromTry(@NonNull Try<? extends T> t)
      Creates a Validation of an Try.
      Type Parameters:
      T - type of the valid value
      Parameters:
      t - A Try
      Returns:
      A Valid(t.get()) if t is a Success, otherwise Invalid(t.getCause()).
      Throws:
      NullPointerException - if t is null
    • sequence

      static <E,T> Validation<Seq<E>,Seq<T>> sequence(@NonNull Iterable<? extends Validation<? extends Seq<? extends E>, ? extends T>> values)
      Reduces many Validation instances into a single Validation by transforming an Iterable<Validation<? extends T>> into a Validation<Seq<T>>.
      Type Parameters:
      E - value type in the case of invalid
      T - value type in the case of valid
      Parameters:
      values - An iterable of Validation instances.
      Returns:
      A valid Validation of a sequence of values if all Validation instances are valid or an invalid Validation containing an accumulated List of errors.
      Throws:
      NullPointerException - if values is null
    • traverse

      static <E,T,U> Validation<Seq<E>,Seq<U>> traverse(@NonNull Iterable<? extends T> values, @NonNull Function<? super T, ? extends Validation<? extends Seq<? extends E>, ? extends U>> mapper)
      Maps the values of an iterable to a sequence of mapped values into a single Validation by transforming an Iterable<? extends T> into a Validation<Seq<U>>.
      Type Parameters:
      E - The mapped error value type.
      T - The type of the given values.
      U - The mapped valid value type.
      Parameters:
      values - An Iterable of values.
      mapper - A mapper of values to Validations
      Returns:
      A Validation of a Seq of results.
      Throws:
      NullPointerException - if values or f is null.
    • narrow

      static <E,T> Validation<E,T> narrow(Validation<? extends E, ? extends T> validation)
      Narrows a widened Validation<? extends E, ? extends T> to Validation<E, T> by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.
      Type Parameters:
      E - type of error
      T - type of valid value
      Parameters:
      validation - A Validation.
      Returns:
      the given validation instance as narrowed type Validation<E, T>.
    • cond

      static <E,T> Validation<E,T> cond(boolean test, @NonNull Supplier<? extends T> valid, @NonNull Supplier<? extends E> error)
      Decides which Validation<E, T> to return, depending on the test value - if it's true, the result will be a Validation.Valid, if it's false - the result will be a Validation.Invalid
      Type Parameters:
      E - Type of error
      T - Type of valid value
      Parameters:
      test - A boolean value to evaluate
      valid - A Supplier<? extends T> supplier of valid value, called if test is true
      error - A Supplier<? extends E> supplier of error, called if test is false
      Returns:
      Validation<E, T> with valid value or error, depending on the test condition evaluation
      Throws:
      NullPointerException - if any of the arguments is null
    • cond

      static <E,T> Validation<E,T> cond(boolean test, @NonNull T valid, @NonNull E error)
      Decides which Validation<E, T> to return, depending on the test value - if it's true, the result will be a Validation.Valid, if it's false - the result will be a Validation.Invalid
      Type Parameters:
      E - Type of error
      T - Type of valid value
      Parameters:
      test - A boolean value to evaluate
      valid - A T valid value, called if test is true
      error - An E error value, called if test is false
      Returns:
      Validation<E, T> with valid value or error, depending on the test condition evaluation
      Throws:
      NullPointerException - if any of the arguments is null
    • combine

      static <E,T1,T2> Validation.Builder<E,T1,T2> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2)
      Combines two Validations into a Validation.Builder.
      Type Parameters:
      E - type of error
      T1 - type of first valid value
      T2 - type of second valid value
      Parameters:
      validation1 - first validation
      validation2 - second validation
      Returns:
      an instance of Builder<E,T1,T2>
      Throws:
      NullPointerException - if validation1 or validation2 is null
    • combine

      static <E,T1,T2,T3> Validation.Builder3<E,T1,T2,T3> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3)
      Combines three Validations into a Validation.Builder3.
      Type Parameters:
      E - type of error
      T1 - type of first valid value
      T2 - type of second valid value
      T3 - type of third valid value
      Parameters:
      validation1 - first validation
      validation2 - second validation
      validation3 - third validation
      Returns:
      an instance of Builder3<E,T1,T2,T3>
      Throws:
      NullPointerException - if validation1, validation2 or validation3 is null
    • combine

      static <E,T1,T2,T3,T4> Validation.Builder4<E,T1,T2,T3,T4> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4)
      Combines four Validations into a Validation.Builder4.
      Type Parameters:
      E - type of error
      T1 - type of first valid value
      T2 - type of second valid value
      T3 - type of third valid value
      T4 - type of fourth valid value
      Parameters:
      validation1 - first validation
      validation2 - second validation
      validation3 - third validation
      validation4 - fourth validation
      Returns:
      an instance of Builder3<E,T1,T2,T3,T4>
      Throws:
      NullPointerException - if validation1, validation2, validation3 or validation4 is null
    • combine

      static <E,T1,T2,T3,T4,T5> Validation.Builder5<E,T1,T2,T3,T4,T5> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5)
      Combines five Validations into a Validation.Builder5.
      Type Parameters:
      E - type of error
      T1 - type of first valid value
      T2 - type of second valid value
      T3 - type of third valid value
      T4 - type of fourth valid value
      T5 - type of fifth valid value
      Parameters:
      validation1 - first validation
      validation2 - second validation
      validation3 - third validation
      validation4 - fourth validation
      validation5 - fifth validation
      Returns:
      an instance of Builder3<E,T1,T2,T3,T4,T5>
      Throws:
      NullPointerException - if validation1, validation2, validation3, validation4 or validation5 is null
    • combine

      static <E,T1,T2,T3,T4,T5,T6> Validation.Builder6<E,T1,T2,T3,T4,T5,T6> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5, @NonNull Validation<E,T6> validation6)
      Combines six Validations into a Validation.Builder6.
      Type Parameters:
      E - type of error
      T1 - type of first valid value
      T2 - type of second valid value
      T3 - type of third valid value
      T4 - type of fourth valid value
      T5 - type of fifth valid value
      T6 - type of sixth valid value
      Parameters:
      validation1 - first validation
      validation2 - second validation
      validation3 - third validation
      validation4 - fourth validation
      validation5 - fifth validation
      validation6 - sixth validation
      Returns:
      an instance of Builder3<E,T1,T2,T3,T4,T5,T6>
      Throws:
      NullPointerException - if validation1, validation2, validation3, validation4, validation5 or validation6 is null
    • combine

      static <E,T1,T2,T3,T4,T5,T6,T7> Validation.Builder7<E,T1,T2,T3,T4,T5,T6,T7> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5, @NonNull Validation<E,T6> validation6, @NonNull Validation<E,T7> validation7)
      Combines seven Validations into a Validation.Builder7.
      Type Parameters:
      E - type of error
      T1 - type of first valid value
      T2 - type of second valid value
      T3 - type of third valid value
      T4 - type of fourth valid value
      T5 - type of fifth valid value
      T6 - type of sixth valid value
      T7 - type of seventh valid value
      Parameters:
      validation1 - first validation
      validation2 - second validation
      validation3 - third validation
      validation4 - fourth validation
      validation5 - fifth validation
      validation6 - sixth validation
      validation7 - seventh validation
      Returns:
      an instance of Builder3<E,T1,T2,T3,T4,T5,T6,T7>
      Throws:
      NullPointerException - if validation1, validation2, validation3, validation4, validation5, validation6 or validation7 is null
    • combine

      static <E,T1,T2,T3,T4,T5,T6,T7,T8> Validation.Builder8<E,T1,T2,T3,T4,T5,T6,T7,T8> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5, @NonNull Validation<E,T6> validation6, @NonNull Validation<E,T7> validation7, @NonNull Validation<E,T8> validation8)
      Combines eight Validations into a Validation.Builder8.
      Type Parameters:
      E - type of error
      T1 - type of first valid value
      T2 - type of second valid value
      T3 - type of third valid value
      T4 - type of fourth valid value
      T5 - type of fifth valid value
      T6 - type of sixth valid value
      T7 - type of seventh valid value
      T8 - type of eighth valid value
      Parameters:
      validation1 - first validation
      validation2 - second validation
      validation3 - third validation
      validation4 - fourth validation
      validation5 - fifth validation
      validation6 - sixth validation
      validation7 - seventh validation
      validation8 - eighth validation
      Returns:
      an instance of Builder3<E,T1,T2,T3,T4,T5,T6,T7,T8>
      Throws:
      NullPointerException - if validation1, validation2, validation3, validation4, validation5, validation6, validation7 or validation8 is null
    • isValid

      boolean isValid()
      Check whether this is of type Valid
      Returns:
      true if is a Valid, false if is an Invalid
    • isInvalid

      boolean isInvalid()
      Check whether this is of type Invalid
      Returns:
      true if is an Invalid, false if is a Valid
    • orElse

      default Validation<E,T> orElse(@NonNull Validation<? extends E, ? extends T> other)
      Returns this Validation if it is valid, otherwise return the alternative.
      Parameters:
      other - An alternative Validation
      Returns:
      this Validation if it is valid, otherwise return the alternative.
    • orElse

      default Validation<E,T> orElse(@NonNull Supplier<Validation<? extends E, ? extends T>> supplier)
      Returns this Validation if it is valid, otherwise return the result of evaluating supplier.
      Parameters:
      supplier - An alternative Validation supplier
      Returns:
      this Validation if it is valid, otherwise return the result of evaluating supplier.
    • isEmpty

      default boolean isEmpty()
      Description copied from interface: Value
      Checks, this Value is empty, i.e. if the underlying value is absent.
      Specified by:
      isEmpty in interface Value<E>
      Returns:
      false, if no underlying value is present, true otherwise.
    • get

      T get()
      Gets the value of this Validation if is a Valid or throws if this is an Invalid.
      Specified by:
      get in interface Value<E>
      Returns:
      The value of this Validation
      Throws:
      NoSuchElementException - if this is an Invalid
    • getOrElseGet

      default T getOrElseGet(@NonNull Function<? super E, ? extends T> other)
      Gets the value if it is a Valid or an value calculated from the error.
      Parameters:
      other - a function which converts an error to an alternative value
      Returns:
      the value, if the underlying Validation is a Valid, or else the alternative value provided by other by applying the error.
    • getError

      E getError()
      Gets the error of this Validation if it is an Invalid or throws if this is a Valid.
      Returns:
      The error, if present
      Throws:
      RuntimeException - if this is a Valid
    • toEither

      default Either<E,T> toEither()
      Converts this Validation to an Either.
      Returns:
      Either.right(get()) if this is valid, otherwise Either.left(getError()).
    • equals

      boolean equals(Object o)
      Description copied from interface: Value
      Clarifies that values have a proper equals() method implemented.

      See Object.equals(Object).

      Specified by:
      equals in interface Value<E>
      Overrides:
      equals in class Object
      Parameters:
      o - An object
      Returns:
      true, if this equals o, false otherwise
    • hashCode

      int hashCode()
      Description copied from interface: Value
      Clarifies that values have a proper hashCode() method implemented.

      See Object.hashCode().

      Specified by:
      hashCode in interface Value<E>
      Overrides:
      hashCode in class Object
      Returns:
      The hashcode of this object
    • toString

      String toString()
      Description copied from interface: Value
      Clarifies that values have a proper toString() method implemented.

      See Object.toString().

      Specified by:
      toString in interface Value<E>
      Overrides:
      toString in class Object
      Returns:
      A String representation of this object
    • forEach

      default void forEach(@NonNull Consumer<? super T> action)
      Performs the given action for the value contained in Valid, or does nothing if this is an Invalid.
      Specified by:
      forEach in interface Iterable<E>
      Specified by:
      forEach in interface Value<E>
      Parameters:
      action - the action to be performed on the contained value
      Throws:
      NullPointerException - if action is null
    • fold

      default <U> U fold(@NonNull Function<? super E, ? extends U> ifInvalid, @NonNull Function<? super T, ? extends U> ifValid)
      Transforms this Validation to a value of type U.

      Example:

      Validation<List<String>, String> valid = ...;<br>
      int i = valid.fold(List::length, String::length);
      
      Type Parameters:
      U - the fold result type
      Parameters:
      ifInvalid - an error mapper
      ifValid - an mapper for a valid value
      Returns:
      ifValid.apply(get()) if this is valid, otherwise ifInvalid.apply(getError()).
      Throws:
      NullPointerException - if one of the given mappers ifInvalid or ifValid is null
    • swap

      default Validation<T,E> swap()
      Flip the valid/invalid values for this Validation. If this is a Valid<E,T>, returns Invalid<T,E>. Or if this is an Invalid<E,T>, return a Valid<T,E>.
      Returns:
      a flipped instance of Validation
    • map

      default <U> Validation<E,U> map(@NonNull Function<? super T, ? extends U> f)
      Description copied from interface: Value
      Maps the underlying value to a different component type.
      Specified by:
      map in interface Value<E>
      Type Parameters:
      U - The new component type
      Parameters:
      f - A mapper
      Returns:
      A new value
    • bimap

      default <E2,T2> Validation<E2,T2> bimap(@NonNull Function<? super E, ? extends E2> errorMapper, @NonNull Function<? super T, ? extends T2> valueMapper)
      Whereas map only performs a mapping on a valid Validation, and mapError performs a mapping on an invalid Validation, bimap allows you to provide mapping actions for both, and will give you the result based on what type of Validation this is. Without this, you would have to do something like: validation.map(...).mapError(...);
      Type Parameters:
      E2 - type of the mapping result if this is an invalid
      T2 - type of the mapping result if this is a valid
      Parameters:
      errorMapper - the invalid mapping operation
      valueMapper - the valid mapping operation
      Returns:
      an instance of Validation<U,R>
      Throws:
      NullPointerException - if invalidMapper or validMapper is null
    • mapError

      default <U> Validation<U,T> mapError(@NonNull Function<? super E, ? extends U> f)
      Applies a function f to the error of this Validation if this is an Invalid. Otherwise does nothing if this is a Valid.
      Type Parameters:
      U - type of the error resulting from the mapping
      Parameters:
      f - a function that maps the error in this Invalid
      Returns:
      an instance of Validation<U,T>
      Throws:
      NullPointerException - if mapping operation f is null
    • ap

      default <U> Validation<Seq<E>,U> ap(@NonNull Validation<Seq<E>, ? extends Function<? super T, ? extends U>> validation)
      Applies a validation containing a function to this validation's value, combining errors if both are invalid. This is the applicative functor's ap operation for Validation.
      Type Parameters:
      U - type of the result of applying the function
      Parameters:
      validation - the validation containing the function to apply
      Returns:
      a valid Validation with the result if both are valid, otherwise an invalid Validation with accumulated errors
      Throws:
      NullPointerException - if validation is null
    • combine

      default <U> Validation.Builder<E,T,U> combine(Validation<E,U> validation)
      Combines two Validations to form a Validation.Builder, which can then be used to perform further combines, or apply a function to it in order to transform the Validation.Builder into a Validation.
      Type Parameters:
      U - type of the value contained in validation
      Parameters:
      validation - the validation object to combine this with
      Returns:
      an instance of Builder
    • filter

      default Option<Validation<E,T>> filter(@NonNull Predicate<? super T> predicate)
      Filters this Validation by testing a predicate on the value. If this is an Invalid or if the predicate matches, returns Some of this Validation, otherwise returns None.
      Parameters:
      predicate - A predicate to test the value
      Returns:
      Some(this) if this is an Invalid or the predicate matches, otherwise None
      Throws:
      NullPointerException - if predicate is null
    • flatMap

      default <U> Validation<E,U> flatMap(@NonNull Function<? super T, ? extends Validation<E, ? extends U>> mapper)
      FlatMaps the value of this Validation if it is valid, otherwise returns this Invalid.
      Type Parameters:
      U - type of the returned Validation value
      Parameters:
      mapper - the mapper function to apply to the value
      Returns:
      a new Validation
      Throws:
      NullPointerException - if mapper is null
    • peek

      default Validation<E,T> peek(@NonNull Consumer<? super T> action)
      Description copied from interface: Value
      Performs the given action on the first element if this is an eager implementation. Performs the given action on all elements (the first immediately, successive deferred), if this is a lazy implementation.
      Specified by:
      peek in interface Value<E>
      Parameters:
      action - The action that will be performed on the element(s).
      Returns:
      this instance
    • isAsync

      default boolean isAsync()
      A Validation's value is computed synchronously.
      Specified by:
      isAsync in interface Value<E>
      Returns:
      false
    • isLazy

      default boolean isLazy()
      A Validation's value is computed eagerly.
      Specified by:
      isLazy in interface Value<E>
      Returns:
      false
    • isSingleValued

      default boolean isSingleValued()
      Description copied from interface: Value
      States whether this is a single-valued type.
      Specified by:
      isSingleValued in interface Value<E>
      Returns:
      true if this is single-valued, false otherwise.
    • iterator

      default @NonNull Iterator<T> iterator()
      Description copied from interface: Value
      Returns a rich io.vavr.collection.Iterator.
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Value<E>
      Returns:
      A new Iterator