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 | Class and Description |
|---|---|
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | Validation.
type of the error of this Validation E, type of the value of this Validation T>An invalid Validation |
| public static class | Validation.
type of the error of this Validation E, type of the value of this Validation T>A valid Validation |
| Modifier and Type | Field and Description |
|---|---|
| public static final long |
| Modifier and Type | Method and Description |
|---|---|
| public default <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 | Returns: an instance of Validation<U,R>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. | Returns: an instance of Builder<E,T1,T2>first validation validation1, Validation<E, T2> second validation validation2)Combines two |
| 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. | Returns: an instance of Builder3<E,T1,T2,T3>first validation validation1, Validation<E, T2> second validation validation2, Validation<E, T3> third validation validation3)Combines three |
| 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. | Returns: an instance of Builder3<E,T1,T2,T3,T4>first validation validation1, Validation<E, T2> second validation validation2, Validation<E, T3> third validation validation3, Validation<E, T4> fourth validation validation4)Combines four |
| 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. | Returns: an instance of Builder3<E,T1,T2,T3,T4,T5>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 |
| 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. | Returns: an instance of Builder3<E,T1,T2,T3,T4,T5,T6>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 |
| 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. | Returns: an instance of Builder3<E,T1,T2,T3,T4,T5,T6,T7>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 |
| 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. | Returns: an instance of Builder3<E,T1,T2,T3,T4,T5,T6,T7,T8>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 |
| public default < type of the value contained in validation U> Validation. | Returns: an instance of Builderthe validation object to combine this with validation)Combines two |
| public boolean | equals(Object
An object o)Redeclares io. Clarifies that values have a proper equals() method implemented. |
| public default Option | |
| public default <U> Validation | |
| public default < the fold result type U> U | |
| public default void | forEach(Consumer<? super T>
the action to be performed on the contained value action)Overrides default io. Performs the given action for the value contained in |
| public static < error type E, value type T> Validation | Returns: AValid(either.get()) if either is a Right, otherwise Invalid(either.getLeft()).An either)EitherCreates a |
| public static < type of the valid value T> Validation | |
| public T | Returns: The value of thisValidationRedeclares io. Gets the value of this |
| public E | Returns: The error, if presentGets the error of this Validation if it is an |
| public default T | Returns: the value, if the underlying Validation is a Valid, or else the alternative value provided byother by applying the error.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. Clarifies that values have a proper hashCode() method implemented. |
| public static < type of the given E, errortype of the value T> Validation | |
| public default boolean | Returns: falseImplements io. A |
| public default boolean | isEmpty()
Implements io. Checks, this |
| public boolean | |
| public default boolean | |
| public default boolean | isSingleValued()
Implements io. States whether this is a single-valued type. |
| public boolean | |
| public default Iterator | |
| public default <U> Validation | map(Function<? super T, ? extends U>
A mapper f)Implements io. Maps the underlying value to a different component type. |
| public default < type of the error resulting from the mapping U> Validation | |
| public static < type of error E, type of valid value T> Validation | Returns: the givenvalidation instance as narrowed type Validation<E, T>.A validation)Validation.Narrows a widened |
| public default Validation | Returns: thisValidation if it is valid, otherwise return the alternative.An alternative other)ValidationReturns this |
| public default Validation | Returns: thisValidation if it is valid, otherwise return the result of evaluating supplier.An alternative supplier)Validation supplierReturns this |
| public default Validation | peek(Consumer<? super T>
The action that will be performed on the element(s). action)Implements io. Performs the given |
| public static < value type in the case of invalid E, value type in the case of valid T> Validation | 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.An iterable of Validation instances. values)Reduces many |
| public default Validation | |
| public default Either | |
| public String | toString()
Redeclares io. 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 | traverse(Iterable<? extends T>
An values, Function<? super T, ? extends Validation<? extends Seq<? extends E>, ? extends U>> Iterable of values.A mapper of values to Validations mapper)Maps the values of an iterable to a sequence of mapped values into a single |
| public static < type of the error E, type of the given T> Validationvalue |