FSharp.Domain.Validation icon indicating copy to clipboard operation
FSharp.Domain.Validation copied to clipboard

Possible to parse from another type than the wrapped one?

Open cmeeren opened this issue 4 years ago • 1 comments

My IDs are usually int (for DB performance reasons). The APIs I create follow JSON:API, which requires that all IDs are represented as strings. My single-case DUs for IDs of course wrap int, which preserves the most type information. I then have fromInt functions that always succeed, and parse functions (used by API code) that accept a string and may fail:

type MyId = private MyId of int

module MyId =

  let fromInt = MyId

  let parse = validInt >> Result.map MyId

Based on the information in the readme, I can't figure out if this library supports this use case, where you parse from another type than the wrapped type. Is it, and if so, how?

cmeeren avatar Jan 30 '21 11:01 cmeeren

I like this idea, it could be handled with the validation definition returning a discriminated union instead of a list of errors. I'm not sure however that it would be possible to keep the same type inference magic going on as it was pretty hard to pull off as it is. I'll leave this under investigation for when I have the time to look at it. I believe this could also be achieved with a sister library if I put the interface definitions in their own package which I'm planning on doing before v1.

lfr avatar Jan 31 '21 09:01 lfr