Apply a function (that returns a Validated
) in the valid case.
Apply a function (that returns a Validated
) in the valid case.
Otherwise return the original Validated
.
This allows "chained" validation: the output of one validation can be fed into another validation function.
This function is similar to flatMap
on Either
. It's not called flatMap
,
because by Cats convention, flatMap
is a monadic bind that is consistent
with ap
. This method is not consistent with ap (or other
Apply
-based methods), because it has "fail-fast" behavior as opposed to
accumulating validation failures.
From Apply: if both the function and this value are Valid, apply the function
Validated is a Bifunctor, this method applies one of the given functions.
Combine this Validated
with another Validated
, using the Semigroup
instances of the underlying E
and A
instances.
Combine this Validated
with another Validated
, using the Semigroup
instances of the underlying E
and A
instances. The resultant Validated
will be Valid
, if, and only if, both this Validated
instance and the
supplied Validated
instance are also Valid
.
Ensure that a successful result passes the given predicate,
falling back to an Invalid of onFailure
if the predicate
returns false.
Ensure that a successful result passes the given predicate,
falling back to an Invalid of onFailure
if the predicate
returns false.
For example:
scala> Validated.valid("").ensure(new IllegalArgumentException("Must not be empty"))(_.nonEmpty) res0: Validated[IllegalArgumentException, String] = Invalid(java.lang.IllegalArgumentException: Must not be empty)
Ensure that a successful result passes the given predicate,
falling back to the an Invalid of the result of onFailure
if the predicate
returns false.
Ensure that a successful result passes the given predicate,
falling back to the an Invalid of the result of onFailure
if the predicate
returns false.
For example:
scala> Validated.valid("ab").ensureOr(s => new IllegalArgumentException("Must be longer than 3, provided '" + s + "'"))(_.length > 3) res0: Validated[IllegalArgumentException, String] = Invalid(java.lang.IllegalArgumentException: Must be longer than 3, provided 'ab')
Is this Valid and matching the given predicate
If this
is valid return this
, otherwise if that
is valid return that
, otherwise combine the failures.
If this
is valid return this
, otherwise if that
is valid return that
, otherwise combine the failures.
This is similar to orElse except that here failures are accumulated.
apply the given function to the value with the given B when valid, otherwise return the given B
Lazily-apply the given function to the value with the given B when valid, otherwise return the given B.
Is this Invalid or matching the predicate
Run the side-effecting function on the value if it is Valid
Return the Valid value, or the default if Invalid
Apply a function to an Invalid value, returning a new Invalid value.
Apply a function to an Invalid value, returning a new Invalid value. Or, if the original valid was Valid, return it.
Apply a function to a Valid value, returning a new Valid value
Return this if it is Valid, or else fall back to the given default.
Return this if it is Valid, or else fall back to the given default. The functionality is similar to that of findValid except for failure accumulation, where here only the error on the right is preserved and the error on the left is ignored.
From Product
Converts the value to an Either[E, A]
Returns Valid values wrapped in Ior.Right, and None for Ior.Left values
Convert this value to a single element List if it is Valid, otherwise return an empty List
Returns Valid values wrapped in Some, and None for Invalid values
Lift the Invalid value into a NonEmptyChain.
Lift the Invalid value into a NonEmptyList.
When Valid, apply the function, marking the result as valid inside the Applicative's context, when Invalid, lift the Error into the Applicative's context
Return the Valid value, or the result of f if Invalid
Convert to an Either, apply a function, convert back.
Convert to an Either, apply a function, convert back. This is handy when you want to use the Monadic properties of the Either type.