zio / zio-json

Fast, secure JSON library with tight ZIO integration.
https://zio.dev/zio-json
Apache License 2.0
407 stars 146 forks source link

Support error accumulation #771

Open Iltotore opened 1 year ago

Iltotore commented 1 year ago

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)
dylandoamaral commented 1 year ago

Actually there is an equivalent: https://zio.github.io/zio-prelude/docs/functionaldatatypes/validation.

wadouk commented 1 year ago

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

Iltotore commented 1 year ago

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.