Skip to content

Commit

Permalink
Add tap and unit to ZValidation (#1299)
Browse files Browse the repository at this point in the history
  • Loading branch information
nox213 authored Apr 24, 2024
1 parent ab2d3c1 commit 3198020
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/shared/src/main/scala/zio/prelude/ZValidation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ sealed trait ZValidation[+W, +E, +A] { self =>
case Success(log, value) => Success(log, value)
}

/**
* Applies the provided validation function to the successful value of this `ZValidation` without altering the result.
* This method can be used to execute side effects or additional validations that do not transform the primary value
* but may modify the log or error. If this `ZValidation` is a failure, it remains unchanged.
*/
final def tap[W1 >: W, E1 >: E](f: A => ZValidation[W1, E1, Any]): ZValidation[W1, E1, A] =
self.flatMap(a => f(a).as(a))

/**
* Transforms this `ZValidation` to an `Either`, discarding the log.
*/
Expand Down Expand Up @@ -295,6 +303,14 @@ sealed trait ZValidation[+W, +E, +A] { self =>
ZIO.succeed(_)
)

/**
* Transforms the successful output of this `ZValidation` into a `Unit`, effectively discarding the original success value
* while preserving any accumulated warnings or errors. This can be useful when the outcome of the validation process is not
* needed, but the side effects (e.g., logging or error accumulation) are important.
*/
final def unit: ZValidation[W, E, Unit] =
self.as(())

/**
* A variant of `zipPar` that keeps only the left success value, but returns
* a failure with all errors if either this `ZValidation` or the specified
Expand Down

0 comments on commit 3198020

Please sign in to comment.