Top Description Inners Fields Methods
io.vavr.control

public Interface Validation<E, T>

extends Value<T>, Serializable
Known Direct Implementers
io.vavr.control.Validation.Valid, io.vavr.control.Validation.Invalid
Type Parameters
<E>
value type in the case of invalid
<T>
value type in the case of valid
Imports
io.vavr.*, io.vavr.collection.Seq, .Iterator, .List, java.io.Serializable, java.util.NoSuchElementException, .Objects, java.util.function.Consumer, .Function, .Predicate, .Supplier

An implementation similar to scalaz's Validation control.

The Validation type is different from a Monad type, it is an applicative functor. Whereas a Monad will short circuit after the first error, the applicative functor will continue on, accumulating ALL errors. This is especially helpful in cases such as validation, where you want to know all the validation errors that have occurred, not just the first one.


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 form of combining validations:

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

Author
Eric Nelson
See Also
Validation

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Validation.Builder<E, T1, T2>

public static class
Validation.Builder3<E, T1, T2, T3>

public static class
Validation.Builder4<E, T1, T2, T3, T4>

public static class
Validation.Builder5<E, T1, T2, T3, T4, T5>

public static class
Validation.Builder6<E, T1, T2, T3, T4, T5, T6>

public static class
Validation.Builder7<E, T1, T2, T3, T4, T5, T6, T7>

public static class
Validation.Builder8<E, T1, T2, T3, T4, T5, T6, T7, T8>

public static class
Validation.Invalid<
type of the error of this Validation
E
,
type of the value of this Validation
T
>

An invalid Validation

public static class
Validation.Valid<
type of the error of this Validation
E
,
type of the value of this Validation
T
>

A valid Validation

Field Summary

Modifier and TypeField and Description
public static final long

Method Summary

Modifier and TypeMethod and Description
public default <U> Validation<Seq<E>, U>
ap(Validation<Seq<E>, ? extends Function<? super T, ? extends U>> validation)

public default <
type of the mapping result if this is an invalid
E2
,
type of the mapping result if this is a valid
T2
>
Validation<E2, T2>

Returns:

an instance of Validation<U,R>
bimap
(Function<? super E, ? extends E2>
the invalid mapping operation
errorMapper
,
Function<? super T, ? extends T2>
the valid mapping operation
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.

public static <
type of error
E
,
type of first valid value
T1
,
type of second valid value
T2
>
Validation.Builder<E, T1, T2>

Returns:

an instance of Builder<E,T1,T2>
combine
(Validation<E, T1>
first validation
validation1
,
Validation<E, T2>
second validation
validation2
)

Combines two Validations into a Builder.

public static <
type of error
E
,
type of first valid value
T1
,
type of second valid value
T2
,
type of third valid value
T3
>
Validation.Builder3<E, T1, T2, T3>

Returns:

an instance of Builder3<E,T1,T2,T3>
combine
(Validation<E, T1>
first validation
validation1
,
Validation<E, T2>
second validation
validation2
,
Validation<E, T3>
third validation
validation3
)

Combines three Validations into a Builder3.

public static <
type of error
E
,
type of first valid value
T1
,
type of second valid value
T2
,
type of third valid value
T3
,
type of fourth valid value
T4
>
Validation.Builder4<E, T1, T2, T3, T4>

Returns:

an instance of Builder3<E,T1,T2,T3,T4>
combine
(Validation<E, T1>
first validation
validation1
,
Validation<E, T2>
second validation
validation2
,
Validation<E, T3>
third validation
validation3
,
Validation<E, T4>
fourth validation
validation4
)

Combines four Validations into a Builder4.

public static <
type of error
E
,
type of first valid value
T1
,
type of second valid value
T2
,
type of third valid value
T3
,
type of fourth valid value
T4
,
type of fifth valid value
T5
>
Validation.Builder5<E, T1, T2, T3, T4, T5>

Returns:

an instance of Builder3<E,T1,T2,T3,T4,T5>
combine
(Validation<E, T1>
first validation
validation1
,
Validation<E, T2>
second validation
validation2
,
Validation<E, T3>
third validation
validation3
,
Validation<E, T4>
fourth validation
validation4
,
Validation<E, T5>
fifth validation
validation5
)

Combines five Validations into a Builder5.

public static <
type of error
E
,
type of first valid value
T1
,
type of second valid value
T2
,
type of third valid value
T3
,
type of fourth valid value
T4
,
type of fifth valid value
T5
,
type of sixth valid value
T6
>
Validation.Builder6<E, T1, T2, T3, T4, T5, T6>

Returns:

an instance of Builder3<E,T1,T2,T3,T4,T5,T6>
combine
(Validation<E, T1>
first validation
validation1
,
Validation<E, T2>
second validation
validation2
,
Validation<E, T3>
third validation
validation3
,
Validation<E, T4>
fourth validation
validation4
,
Validation<E, T5>
fifth validation
validation5
,
Validation<E, T6>
sixth validation
validation6
)

Combines six Validations into a Builder6.

public static <
type of error
E
,
type of first valid value
T1
,
type of second valid value
T2
,
type of third valid value
T3
,
type of fourth valid value
T4
,
type of fifth valid value
T5
,
type of sixth valid value
T6
,
type of seventh valid value
T7
>
Validation.Builder7<E, T1, T2, T3, T4, T5, T6, T7>

Returns:

an instance of Builder3<E,T1,T2,T3,T4,T5,T6,T7>
combine
(Validation<E, T1>
first validation
validation1
,
Validation<E, T2>
second validation
validation2
,
Validation<E, T3>
third validation
validation3
,
Validation<E, T4>
fourth validation
validation4
,
Validation<E, T5>
fifth validation
validation5
,
Validation<E, T6>
sixth validation
validation6
,
Validation<E, T7>
seventh validation
validation7
)

Combines seven Validations into a Builder7.

public static <
type of error
E
,
type of first valid value
T1
,
type of second valid value
T2
,
type of third valid value
T3
,
type of fourth valid value
T4
,
type of fifth valid value
T5
,
type of sixth valid value
T6
,
type of seventh valid value
T7
,
type of eighth valid value
T8
>
Validation.Builder8<E, T1, T2, T3, T4, T5, T6, T7, T8>

Returns:

an instance of Builder3<E,T1,T2,T3,T4,T5,T6,T7,T8>
combine
(Validation<E, T1>
first validation
validation1
,
Validation<E, T2>
second validation
validation2
,
Validation<E, T3>
third validation
validation3
,
Validation<E, T4>
fourth validation
validation4
,
Validation<E, T5>
fifth validation
validation5
,
Validation<E, T6>
sixth validation
validation6
,
Validation<E, T7>
seventh validation
validation7
,
Validation<E, T8>
eighth validation
validation8
)

Combines eight Validations into a Builder8.

public default <
type of the value contained in validation
U
>
Validation.Builder<E, T, U>

Returns:

an instance of Builder
combine
(Validation<E, U>
the validation object to combine this with
validation
)

Combines two Validations to form a Builder, which can then be used to perform further combines, or apply a function to it in order to transform the Builder into a Validation.

public boolean
equals(Object
An object
o
)

Redeclares io.vavr.Value.equals.

Clarifies that values have a proper equals() method implemented.

public default Option<Validation<E, T>>
filter(Predicate<? super T> predicate)

public default <U> Validation<E, U>
flatMap(Function<? super T, ? extends Validation<E, ? extends U>> mapper)

public default <
the fold result type
U
>
U

Returns:

ifValid.apply(get()) if this is valid, otherwise ifInvalid.apply(getError()).
fold
(Function<? super E, ? extends U>
an error mapper
ifInvalid
,
Function<? super T, ? extends U>
an mapper for a valid value
ifValid
)

Transforms this Validation to a value of type U.

public default void
forEach(Consumer<? super T>
the action to be performed on the contained value
action
)

Overrides default io.vavr.Value.forEach.

Performs the given action for the value contained in Valid, or does nothing if this is an Invalid.

public static <
error type
E
,
value type
T
>
Validation<E, T>

Returns:

A Valid(either.get()) if either is a Right, otherwise Invalid(either.getLeft()).
fromEither
(Either<E, T>
An Either
either
)

Creates a Validation of an Either.

public static <
type of the valid value
T
>
Validation<Throwable, T>

Returns:

A Valid(t.get()) if t is a Success, otherwise Invalid(t.getCause()).
fromTry
(Try<? extends T>
A Try
t
)

Creates a Validation of an Try.

public T

Returns:

The value of this Validation
get
()

Redeclares io.vavr.Value.get.

Gets the value of this Validation if is a Valid or throws if this is an Invalid.

public E

Returns:

The error, if present
getError
()

Gets the error of this Validation if it is an Invalid or throws if this is a Valid.

public default T

Returns:

the value, if the underlying Validation is a Valid, or else the alternative value provided by other by applying the error.
getOrElseGet
(Function<? super E, ? extends T>
a function which converts an error to an alternative value
other
)

Gets the value if it is a Valid or an value calculated from the error.

public int
hashCode()

Redeclares io.vavr.Value.hashCode.

Clarifies that values have a proper hashCode() method implemented.

public static <
type of the given error
E
,
type of the value
T
>
Validation<E, T>

Returns:

Invalid(error)
invalid
(E
An error
error
)

Creates an Invalid that contains the given error.

public default boolean

Returns:

false
isAsync
()

Implements io.vavr.Value.isAsync.

A Validation's value is computed synchronously.

public default boolean
isEmpty()

Implements io.vavr.Value.isEmpty.

Checks, this Value is empty, i.e. if the underlying value is absent.

public boolean

Returns:

true if is an Invalid, false if is a Valid
isInvalid
()

Check whether this is of type Invalid

public default boolean

Returns:

false
isLazy
()

Implements io.vavr.Value.isLazy.

A Validation's value is computed eagerly.

public default boolean
isSingleValued()

Implements io.vavr.Value.isSingleValued.

States whether this is a single-valued type.

public boolean

Returns:

true if is a Valid, false if is an Invalid
isValid
()

Check whether this is of type Valid

public default Iterator<T>
iterator()

Implements io.vavr.Value.iterator.

Returns a rich io.vavr.collection.Iterator.

public default <U> Validation<E, U>
map(Function<? super T, ? extends U>
A mapper
f
)

Implements io.vavr.Value.map.

Maps the underlying value to a different component type.

public default <
type of the error resulting from the mapping
U
>
Validation<U, T>

Returns:

an instance of Validation<U,T>
mapError
(Function<? super E, ? extends U>
a function that maps the error in this Invalid
f
)

Applies a function f to the error of this Validation if this is an Invalid.

public static <
type of error
E
,
type of valid value
T
>
Validation<E, T>

Returns:

the given validation instance as narrowed type Validation<E, T>.
narrow
(Validation<? extends E, ? extends T>
A Validation.
validation
)

Narrows a widened Validation<?

public default Validation<E, T>

Returns:

this Validation if it is valid, otherwise return the alternative.
orElse
(Validation<? extends E, ? extends T>
An alternative Validation
other
)

Returns this Validation if it is valid, otherwise return the alternative.

public default Validation<E, T>

Returns:

this Validation if it is valid, otherwise return the result of evaluating supplier.
orElse
(Supplier<Validation<? extends E, ? extends T>>
An alternative Validation supplier
supplier
)

Returns this Validation if it is valid, otherwise return the result of evaluating supplier.

public default Validation<E, T>
peek(Consumer<? super T>
The action that will be performed on the element(s).
action
)

Implements io.vavr.Value.peek.

Performs the given action on the first element if this is an eager implementation.

public static <
value type in the case of invalid
E
,
value type in the case of valid
T
>
Validation<Seq<E>, Seq<T>>

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.
sequence
(Iterable<? extends Validation<? extends Seq<? extends E>, ? extends T>>
An iterable of Validation instances.
values
)

Reduces many Validation instances into a single Validation by transforming an Iterable<Validation<?

public default Validation<T, E>

Returns:

a flipped instance of Validation
swap
()

Flip the valid/invalid values for this Validation.

public default Either<E, T>

Returns:

Either.right(get()) if this is valid, otherwise Either.left(getError()).
toEither
()

Converts this Validation to an Either.

public String
toString()

Redeclares io.vavr.Value.toString.

Clarifies that values have a proper toString() method implemented.

public static <
The mapped error value type.
E
,
The type of the given values.
T
,
The mapped valid value type.
U
>
Validation<Seq<E>, Seq<U>>

Returns:

A Validation of a Seq of results.
traverse
(Iterable<? extends T>
An Iterable of values.
values
,
Function<? super T, ? extends Validation<? extends Seq<? extends E>, ? extends U>>
A mapper of values to Validations
mapper
)

Maps the values of an iterable to a sequence of mapped values into a single Validation by transforming an Iterable<?

public static <
type of the error
E
,
type of the given value
T
>
Validation<E, T>

Returns:

Valid(value)
valid
(T
A value
value
)

Creates a Valid that contains the given value.

Inherited from io.vavr.Value:
collectcollectcontainscorrespondseqexistsforAllgetOrElsegetOrElsegetOrElseThrowgetOrElseTrygetOrNullnarrowoutoutspliteratorstderrstdoutstringPrefixtoArraytoCharSeqtoCompletableFuturetoEithertoEithertoInvalidtoInvalidtoJavaArraytoJavaArraytoJavaArraytoJavaCollectiontoJavaListtoJavaListtoJavaMaptoJavaMaptoJavaMaptoJavaOptionaltoJavaParallelStreamtoJavaSettoJavaSettoJavaStreamtoLefttoLefttoLinkedMaptoLinkedMaptoLinkedSettoListtoMaptoMaptoOptiontoPriorityQueuetoPriorityQueuetoQueuetoRighttoRighttoSettoSortedMaptoSortedMaptoSortedMaptoSortedMaptoSortedSettoSortedSettoStreamtoTreetoTreetoTrytoTrytoValidtoValidtoValidationtoValidationtoVector