zio/zio-json

Support error accumulation

Iltotore opened this issue · 3 comments

Accumulating error enables to get and return all occured errors during Json decoding:

import zio.json.*

case class User(name: String, age: Int)

given JsonDecoder[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:

def decodeJsonAccumulating(value: A): Either[List[String], A] = decodeJson(value).left.map(List.apply)

validation could be the implementation but with the decoder that fail on first will not help the case

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.