You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Accumulating error enables to get and return all occured errors during Json decoding:
importzio.json.*caseclassUser(name: String, age: Int)
givenJsonDecoder[User] =DeriveJsonDecoder.gen
"""{"name":5,"age":"Iltotore"}""".fromJsonAcc[User] //Hypothetic method//Invalid value for name//Invalid value for age
This is especially useful for form-like/API validation. This can be compared to Circe's decodeAccumulating or Cats' Validated.
As far as I know, there's no Validated equivalent for ZIO/ZIO Prelude but an Either[List[String], A] would be enough.
For example, I would like my above example to return Left(List(".name(...)", ".age(...)")).
In Circe, this is achieved by defining a second method in the Decoder (in ZIO-JSON, JsonDecoder) typeclass named decodeAccumulating which could be named decodeJsonAccumulating to feel more consistent with the existing decodeJson method.
To preserve retrocompatibility and because sometimes it's non sense to use error accumulation, the method can be defined like this by default:
This is only the default implementation to not force users to make an accumulative and non-accumulative implementation for their Decoder. AFAIK decodeJsonAccumulating is overriden in Circe's Decoder for objects with an implementation that actually makes use of error accumulation.
Accumulating error enables to get and return all occured errors during Json decoding:
This is especially useful for form-like/API validation. This can be compared to Circe's
decodeAccumulating
or Cats'Validated
.As far as I know, there's no
Validated
equivalent for ZIO/ZIO Prelude but anEither[List[String], A]
would be enough.For example, I would like my above example to return
Left(List(".name(...)", ".age(...)"))
.In Circe, this is achieved by defining a second method in the
Decoder
(in ZIO-JSON,JsonDecoder
) typeclass nameddecodeAccumulating
which could be nameddecodeJsonAccumulating
to feel more consistent with the existingdecodeJson
method.To preserve retrocompatibility and because sometimes it's non sense to use error accumulation, the method can be defined like this by default:
The text was updated successfully, but these errors were encountered: