system-f / validation

A data-type like Either but with an accumulating Applicative
Other
99 stars 28 forks source link

Help in creating a better example #34

Open rithmind opened 5 years ago

rithmind commented 5 years ago

I have created a type called Email and assosiated validations that collect EmailErrror I have created a type called Password and assosiated validations that collect PasswordError Now I want to create a type called Creds combining Email and Password and I want to accumulate both EmailErrror and PasswordError.

Is this possible? The code example is below here. https://stackoverflow.com/questions/55521575/how-to-compose-errors-using-data-validation

Thank you!

tonymorris commented 5 years ago

There are a few possible answers to your question.

Is this the right answer?


data CredsError = EmailCredsError EmailError | PasswordCredsError PasswordError
 deriving (Show)

validateCreds :: Creds -> Validation [CredsError] ()
validateCreds (Creds e p) = (\e' p' -> fmap EmailCredsError e' ++ fmap PasswordCredsError p') <$> validateEmail e <*> validatePassword p
rithmind commented 5 years ago

@tonymorris Thanks for the answer. Unfortunately, it didn't solve the question. Also, I was thinking of doing it in a generic way. Suppose, this Creds is part of even a bigger type such as Data Transaction = Creds Payload and we have errors related to Creds and Payload, mkTransactions should result in accumulation of all errors from Creds (i.e from Email and Password ) and Payload