system-f / validation

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

Add power to validate and ensure #32

Open FintanH opened 5 years ago

FintanH commented 5 years ago

It would be really useful if the validate and ensure used (a -> Maybe b) instead of (a -> Bool). This would allow the user to output an updated, validated value as a result.

For example, my current use case is that I'm validating an Integer to a Natural, ensuring that the Integer is (n >= 0).

So I could do:

positive :: Integer -> Validation Errors Natural
positive n = 
  validate (pure $ NegativeVal n) (if n <= then Just $ fromInteger n else Nothing) n

What do you think? :)

FintanH commented 5 years ago

Granted, I could just do an fmap at the end of the computation too

tonymorris commented 5 years ago

Agree. +1