E
- value type in the case of invalidT
- value type in the case of validpublic interface Validation<E,T> extends Value<T>, Serializable
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);
Modifier and Type | Interface and Description |
---|---|
static class |
Validation.Builder<E,T1,T2> |
static class |
Validation.Builder3<E,T1,T2,T3> |
static class |
Validation.Builder4<E,T1,T2,T3,T4> |
static class |
Validation.Builder5<E,T1,T2,T3,T4,T5> |
static class |
Validation.Builder6<E,T1,T2,T3,T4,T5,T6> |
static class |
Validation.Builder7<E,T1,T2,T3,T4,T5,T6,T7> |
static class |
Validation.Builder8<E,T1,T2,T3,T4,T5,T6,T7,T8> |
static class |
Validation.Invalid<E,T>
An invalid Validation
|
static class |
Validation.Valid<E,T>
A valid Validation
|
Modifier and Type | Field and Description |
---|---|
static long |
serialVersionUID |
Modifier and Type | Method and Description |
---|---|
default <U> Validation<Seq<E>,U> |
ap(Validation<Seq<E>,? extends Function<? super T,? extends U>> validation) |
default <E2,T2> Validation<E2,T2> |
bimap(Function<? super E,? extends E2> errorMapper,
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.
|
static <E,T1,T2> Validation.Builder<E,T1,T2> |
combine(Validation<E,T1> validation1,
Validation<E,T2> validation2)
Combines two
Validation s into a Validation.Builder . |
static <E,T1,T2,T3> |
combine(Validation<E,T1> validation1,
Validation<E,T2> validation2,
Validation<E,T3> validation3)
Combines three
Validation s into a Validation.Builder3 . |
static <E,T1,T2,T3,T4> |
combine(Validation<E,T1> validation1,
Validation<E,T2> validation2,
Validation<E,T3> validation3,
Validation<E,T4> validation4)
Combines four
Validation s into a Validation.Builder4 . |
static <E,T1,T2,T3,T4,T5> |
combine(Validation<E,T1> validation1,
Validation<E,T2> validation2,
Validation<E,T3> validation3,
Validation<E,T4> validation4,
Validation<E,T5> validation5)
Combines five
Validation s into a Validation.Builder5 . |
static <E,T1,T2,T3,T4,T5,T6> |
combine(Validation<E,T1> validation1,
Validation<E,T2> validation2,
Validation<E,T3> validation3,
Validation<E,T4> validation4,
Validation<E,T5> validation5,
Validation<E,T6> validation6)
Combines six
Validation s into a Validation.Builder6 . |
static <E,T1,T2,T3,T4,T5,T6,T7> |
combine(Validation<E,T1> validation1,
Validation<E,T2> validation2,
Validation<E,T3> validation3,
Validation<E,T4> validation4,
Validation<E,T5> validation5,
Validation<E,T6> validation6,
Validation<E,T7> validation7)
Combines seven
Validation s into a Validation.Builder7 . |
static <E,T1,T2,T3,T4,T5,T6,T7,T8> |
combine(Validation<E,T1> validation1,
Validation<E,T2> validation2,
Validation<E,T3> validation3,
Validation<E,T4> validation4,
Validation<E,T5> validation5,
Validation<E,T6> validation6,
Validation<E,T7> validation7,
Validation<E,T8> validation8)
Combines eight
Validation s into a Validation.Builder8 . |
default <U> Validation.Builder<E,T,U> |
combine(Validation<E,U> validation)
Combines two
Validation s 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 . |
boolean |
equals(Object o)
Clarifies that values have a proper equals() method implemented.
|
default Option<Validation<E,T>> |
filter(Predicate<? super T> predicate) |
default <U> Validation<E,U> |
flatMap(Function<? super T,? extends Validation<E,? extends U>> mapper) |
default <U> U |
fold(Function<? super E,? extends U> ifInvalid,
Function<? super T,? extends U> ifValid)
Transforms this
Validation to a value of type U . |
default void |
forEach(Consumer<? super T> action)
Performs the given action for the value contained in
Valid , or does nothing
if this is an Invalid . |
static <E,T> Validation<E,T> |
fromEither(Either<E,T> either)
Creates a
Validation of an Either . |
static <T> Validation<Throwable,T> |
fromTry(Try<? extends T> t)
Creates a
Validation of an Try . |
T |
get()
Gets the value of this
Validation if is a Valid or throws if this is an Invalid . |
E |
getError()
Gets the error of this Validation if it is an
Invalid or throws if this is a Valid . |
default T |
getOrElseGet(Function<? super E,? extends T> other)
Gets the value if it is a Valid or an value calculated from the error.
|
int |
hashCode()
Clarifies that values have a proper hashCode() method implemented.
|
static <E,T> Validation<E,T> |
invalid(E error)
Creates an
Validation.Invalid that contains the given error . |
default boolean |
isAsync()
A
Validation 's value is computed synchronously. |
default boolean |
isEmpty()
Checks, this
Value is empty, i.e. |
boolean |
isInvalid()
Check whether this is of type
Invalid |
default boolean |
isLazy()
A
Validation 's value is computed eagerly. |
default boolean |
isSingleValued()
States whether this is a single-valued type.
|
boolean |
isValid()
Check whether this is of type
Valid |
default Iterator<T> |
iterator()
Returns a rich
io.vavr.collection.Iterator . |
default <U> Validation<E,U> |
map(Function<? super T,? extends U> f)
Maps the underlying value to a different component type.
|
default <U> Validation<U,T> |
mapError(Function<? super E,? extends U> f)
Applies a function f to the error of this Validation if this is an Invalid.
|
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. |
default Validation<E,T> |
orElse(Supplier<Validation<? extends E,? extends T>> supplier)
Returns this
Validation if it is valid, otherwise return the result of evaluating supplier. |
default Validation<E,T> |
orElse(Validation<? extends E,? extends T> other)
Returns this
Validation if it is valid, otherwise return the alternative. |
default Validation<E,T> |
peek(Consumer<? super T> action)
Performs the given
action on the first element if this is an eager implementation. |
static <E,T> Validation<Seq<E>,Seq<T>> |
sequence(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>> . |
default Validation<T,E> |
swap()
Flip the valid/invalid values for this Validation.
|
default Either<E,T> |
toEither()
Converts this Validation to an
Either . |
String |
toString()
Clarifies that values have a proper toString() method implemented.
|
static <E,T,U> Validation<Seq<E>,Seq<U>> |
traverse(Iterable<? extends T> values,
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>> . |
static <E,T> Validation<E,T> |
valid(T value)
Creates a
Validation.Valid that contains the given value . |
collect, collect, contains, corresponds, eq, exists, forAll, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, narrow, out, out, spliterator, stderr, stdout, stringPrefix, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
static final long serialVersionUID
static <E,T> Validation<E,T> valid(T value)
Validation.Valid
that contains the given value
.E
- type of the errorT
- type of the given value
value
- A valueValid(value)
static <E,T> Validation<E,T> invalid(E error)
Validation.Invalid
that contains the given error
.E
- type of the given error
T
- type of the valueerror
- An errorInvalid(error)
NullPointerException
- if error is nullstatic <E,T> Validation<E,T> fromEither(Either<E,T> either)
Validation
of an Either
.E
- error typeT
- value typeeither
- An Either
Valid(either.get())
if either is a Right, otherwise Invalid(either.getLeft())
.NullPointerException
- if either is nullstatic <T> Validation<Throwable,T> fromTry(Try<? extends T> t)
Validation
of an Try
.T
- type of the valid valuet
- A Try
Valid(t.get())
if t is a Success, otherwise Invalid(t.getCause())
.NullPointerException
- if t
is nullstatic <E,T> Validation<Seq<E>,Seq<T>> sequence(Iterable<? extends Validation<? extends Seq<? extends E>,? extends T>> values)
Validation
instances into a single Validation
by transforming an
Iterable<Validation<? extends T>>
into a Validation<Seq<T>>
.E
- value type in the case of invalidT
- value type in the case of validvalues
- An iterable of Validation instances.NullPointerException
- if values is nullstatic <E,T,U> Validation<Seq<E>,Seq<U>> traverse(Iterable<? extends T> values, Function<? super T,? extends Validation<? extends Seq<? extends E>,? extends U>> mapper)
Validation
by
transforming an Iterable<? extends T>
into a Validation<Seq<U>>
.
T
- The type of the given values.E
- The mapped error value type.U
- The mapped valid value type.values
- An Iterable
of values.mapper
- A mapper of values to ValidationsValidation
of a Seq
of results.NullPointerException
- if values or f is null.static <E,T> Validation<E,T> narrow(Validation<? extends E,? extends T> validation)
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.E
- type of errorT
- type of valid valuevalidation
- A Validation
.validation
instance as narrowed type Validation<E, T>
.static <E,T1,T2> Validation.Builder<E,T1,T2> combine(Validation<E,T1> validation1, Validation<E,T2> validation2)
Validation
s into a Validation.Builder
.E
- type of errorT1
- type of first valid valueT2
- type of second valid valuevalidation1
- first validationvalidation2
- second validationNullPointerException
- if validation1 or validation2 is nullstatic <E,T1,T2,T3> Validation.Builder3<E,T1,T2,T3> combine(Validation<E,T1> validation1, Validation<E,T2> validation2, Validation<E,T3> validation3)
Validation
s into a Validation.Builder3
.E
- type of errorT1
- type of first valid valueT2
- type of second valid valueT3
- type of third valid valuevalidation1
- first validationvalidation2
- second validationvalidation3
- third validationNullPointerException
- if validation1, validation2 or validation3 is nullstatic <E,T1,T2,T3,T4> Validation.Builder4<E,T1,T2,T3,T4> combine(Validation<E,T1> validation1, Validation<E,T2> validation2, Validation<E,T3> validation3, Validation<E,T4> validation4)
Validation
s into a Validation.Builder4
.E
- type of errorT1
- type of first valid valueT2
- type of second valid valueT3
- type of third valid valueT4
- type of fourth valid valuevalidation1
- first validationvalidation2
- second validationvalidation3
- third validationvalidation4
- fourth validationNullPointerException
- if validation1, validation2, validation3 or validation4 is nullstatic <E,T1,T2,T3,T4,T5> Validation.Builder5<E,T1,T2,T3,T4,T5> combine(Validation<E,T1> validation1, Validation<E,T2> validation2, Validation<E,T3> validation3, Validation<E,T4> validation4, Validation<E,T5> validation5)
Validation
s into a Validation.Builder5
.E
- type of errorT1
- type of first valid valueT2
- type of second valid valueT3
- type of third valid valueT4
- type of fourth valid valueT5
- type of fifth valid valuevalidation1
- first validationvalidation2
- second validationvalidation3
- third validationvalidation4
- fourth validationvalidation5
- fifth validationNullPointerException
- if validation1, validation2, validation3, validation4 or validation5 is nullstatic <E,T1,T2,T3,T4,T5,T6> Validation.Builder6<E,T1,T2,T3,T4,T5,T6> combine(Validation<E,T1> validation1, Validation<E,T2> validation2, Validation<E,T3> validation3, Validation<E,T4> validation4, Validation<E,T5> validation5, Validation<E,T6> validation6)
Validation
s into a Validation.Builder6
.E
- type of errorT1
- type of first valid valueT2
- type of second valid valueT3
- type of third valid valueT4
- type of fourth valid valueT5
- type of fifth valid valueT6
- type of sixth valid valuevalidation1
- first validationvalidation2
- second validationvalidation3
- third validationvalidation4
- fourth validationvalidation5
- fifth validationvalidation6
- sixth validationNullPointerException
- if validation1, validation2, validation3, validation4, validation5 or validation6 is nullstatic <E,T1,T2,T3,T4,T5,T6,T7> Validation.Builder7<E,T1,T2,T3,T4,T5,T6,T7> combine(Validation<E,T1> validation1, Validation<E,T2> validation2, Validation<E,T3> validation3, Validation<E,T4> validation4, Validation<E,T5> validation5, Validation<E,T6> validation6, Validation<E,T7> validation7)
Validation
s into a Validation.Builder7
.E
- type of errorT1
- type of first valid valueT2
- type of second valid valueT3
- type of third valid valueT4
- type of fourth valid valueT5
- type of fifth valid valueT6
- type of sixth valid valueT7
- type of seventh valid valuevalidation1
- first validationvalidation2
- second validationvalidation3
- third validationvalidation4
- fourth validationvalidation5
- fifth validationvalidation6
- sixth validationvalidation7
- seventh validationNullPointerException
- if validation1, validation2, validation3, validation4, validation5, validation6 or validation7 is nullstatic <E,T1,T2,T3,T4,T5,T6,T7,T8> Validation.Builder8<E,T1,T2,T3,T4,T5,T6,T7,T8> combine(Validation<E,T1> validation1, Validation<E,T2> validation2, Validation<E,T3> validation3, Validation<E,T4> validation4, Validation<E,T5> validation5, Validation<E,T6> validation6, Validation<E,T7> validation7, Validation<E,T8> validation8)
Validation
s into a Validation.Builder8
.E
- type of errorT1
- type of first valid valueT2
- type of second valid valueT3
- type of third valid valueT4
- type of fourth valid valueT5
- type of fifth valid valueT6
- type of sixth valid valueT7
- type of seventh valid valueT8
- type of eighth valid valuevalidation1
- first validationvalidation2
- second validationvalidation3
- third validationvalidation4
- fourth validationvalidation5
- fifth validationvalidation6
- sixth validationvalidation7
- seventh validationvalidation8
- eighth validationNullPointerException
- if validation1, validation2, validation3, validation4, validation5, validation6, validation7 or validation8 is nullboolean isValid()
Valid
boolean isInvalid()
Invalid
default Validation<E,T> orElse(Validation<? extends E,? extends T> other)
Validation
if it is valid, otherwise return the alternative.other
- An alternative Validation
Validation
if it is valid, otherwise return the alternative.default Validation<E,T> orElse(Supplier<Validation<? extends E,? extends T>> supplier)
Validation
if it is valid, otherwise return the result of evaluating supplier.supplier
- An alternative Validation
supplierValidation
if it is valid, otherwise return the result of evaluating supplier.default boolean isEmpty()
Value
Value
is empty, i.e. if the underlying value is absent.T get()
Validation
if is a Valid
or throws if this is an Invalid
.get
in interface Value<T>
Validation
NoSuchElementException
- if this is an Invalid
default T getOrElseGet(Function<? super E,? extends T> other)
other
- a function which converts an error to an alternative valueother
by applying the error.E getError()
Invalid
or throws if this is a Valid
.RuntimeException
- if this is a Valid
default Either<E,T> toEither()
Either
.Either.right(get())
if this is valid, otherwise Either.left(getError())
.boolean equals(Object o)
Value
int hashCode()
Value
See Object.hashCode().
String toString()
Value
See Object.toString().
default void forEach(Consumer<? super T> action)
Valid
, or does nothing
if this is an Invalid
.default <U> U fold(Function<? super E,? extends U> ifInvalid, Function<? super T,? extends U> ifValid)
Validation
to a value of type U
.
Example:
Validation<List<String>, String> valid = ...;<br>
int i = valid.fold(List::length, String::length);
U
- the fold result typeifInvalid
- an error mapperifValid
- an mapper for a valid valueifValid.apply(get())
if this is valid, otherwise ifInvalid.apply(getError())
.NullPointerException
- if one of the given mappers ifInvalid
or ifValid
is nulldefault Validation<T,E> swap()
default <U> Validation<E,U> map(Function<? super T,? extends U> f)
Value
default <E2,T2> Validation<E2,T2> bimap(Function<? super E,? extends E2> errorMapper, Function<? super T,? extends T2> valueMapper)
E2
- type of the mapping result if this is an invalidT2
- type of the mapping result if this is a validerrorMapper
- the invalid mapping operationvalueMapper
- the valid mapping operationNullPointerException
- if invalidMapper or validMapper is nulldefault <U> Validation<U,T> mapError(Function<? super E,? extends U> f)
U
- type of the error resulting from the mappingf
- a function that maps the error in this InvalidNullPointerException
- if mapping operation f is nulldefault <U> Validation<Seq<E>,U> ap(Validation<Seq<E>,? extends Function<? super T,? extends U>> validation)
default <U> Validation.Builder<E,T,U> combine(Validation<E,U> validation)
Validation
s 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
.U
- type of the value contained in validationvalidation
- the validation object to combine this withdefault <U> Validation<E,U> flatMap(Function<? super T,? extends Validation<E,? extends U>> mapper)
default Validation<E,T> peek(Consumer<? super T> action)
Value
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.default boolean isAsync()
Validation
's value is computed synchronously.default boolean isLazy()
Validation
's value is computed eagerly.default boolean isSingleValued()
Value
isSingleValued
in interface Value<T>
true
if this is single-valued, false
otherwise.Copyright © 2020. All Rights Reserved.