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

Any data type / structure for error accumulation? #230

Open
vreuter opened this issue Nov 22, 2024 · 1 comment
Open

Any data type / structure for error accumulation? #230

vreuter opened this issue Nov 22, 2024 · 1 comment

Comments

@vreuter
Copy link
Contributor

vreuter commented Nov 22, 2024

Is there any structure with which to accumulate errors from multiple Results? Say I have a dataclass with three fields and each needs validation, and I'd like to take input and either emit a Result.Ok with an instance of my dataclass, or a Result.Error with a collection of the (one or more) errors from the (failed) validation of the input.

Something from Scala might looks like...

case class Mine(a: A, b: B, c: C)

def safeA: String => Either[String, A]
def safeB: String => Either[String, B]
def safeC: String => Either[String, C]

def safeBuild(s1: String, s2: String, s2: String): cats.data.ValidatedNel[String, Mine] = 
  val aNel = safeA(s1).toValidatedNel
  val bNel = safeB(s2).toValidatedNel
  val cNel = safeC(s3).toValidatedNel
  (aNel, bNel, cNel).mapN(Mine.apply)

So I guess I'm looking for something like...

from dataclasses import dataclass

@dataclass
class Mine:
    a: int
    b: float
    c: str

def maybeA(s: str) -> Result[int, str]: ...
def maybeB(s: str) -> Result[float, str]: ...
def maybeC(s: str) -> Result[str, str]: ...

def maybeMine(s1: str, s2: str, s3: str) -> Result[Mine, list[str]]:
    ??? # how to combine the application of each component's safe parser?
@vreuter
Copy link
Contributor Author

vreuter commented Nov 22, 2024

Would be powered by #215

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

1 participant