ZIO Support
This module provides refinement methods using ZIO Prelude's Validation
.
Prelude's typeclass instances already work with IronType due to variance.
Dependency
SBT:
libraryDependencies += "io.github.iltotore" %% "iron-zio" % "version"
Mill:
ivy"io.github.iltotore::iron-zio:version"
Following examples' dependencies
SBT:
libraryDependencies += "dev.zio" %% "zio" % "2.0.5"
libraryDependencies += "dev.zio" %% "zio-prelude" % "1.0.0-RC16"
Mill:
ivy"dev.zio::zio:2.0.5"
ivy"dev.zio::zio-prelude:1.0.0-RC16"
Accumulative error handling
ZIO enables accumulative error handling via Validation. The ZIO module provides a refineValidation
method that uses this datatype to handle errors.
This method is similar to refineEither
and refineOption
defined in the core module.
The User example now looks like this:
import zio.prelude.Validation
import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*
import io.github.iltotore.iron.zio.*
type Username = DescribedAs[Alphanumeric, "Username should be alphanumeric"]
type Age = DescribedAs[Positive, "Age should be positive"]
case class User(name: String :| Username, age: Int :| Age)
def createUser(name: String, age: Int): Validation[String, User] =
Validation.validateWith(
name.refineValidation[Username],
age.refineValidation[Age]
)(User.apply)
createUser("Iltotore", 18) //Success(Chunk(),User(Iltotore,18))
createUser("Il_totore", 18) //Failure(Chunk(),NonEmptyChunk(Username should be alphanumeric))
createUser("Il_totore", -18) //Failure(Chunk(),NonEmptyChunk(Username should be alphanumeric, Age should be positive))
Companion object (RefinedTypeOps validation extension)
Companion object created with RefinedTypeOps
is being extended by set of functions.
Companion object
import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*
opaque type Temperature = Double :| Positive
object Temperature extends RefinedTypeOps[Temperature]
Imports
import io.github.iltotore.iron.zio.validation
validation
The example below returns ZValidation.Success
or ZValidation.Failure
.
import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*
opaque type Temperature = Double :| Positive
object Temperature extends RefinedTypeOps[Temperature]
Temperature.validation(x)
In this article