Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support error accumulation #771

Open
Iltotore opened this issue Oct 27, 2022 · 3 comments
Open

Support error accumulation #771

Iltotore opened this issue Oct 27, 2022 · 3 comments

Comments

@Iltotore
Copy link

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
Copy link

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

@wadouk
Copy link

wadouk commented May 3, 2023

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

@Iltotore
Copy link
Author

Iltotore commented May 3, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants