FSharp.Domain.Validation
FSharp.Domain.Validation copied to clipboard
What about transforming values during parsing?
I have certain constrained types that, through their parsing, also transform the input. Two examples:
- For most simple one-line string inputs, I don't reject control characters, I just strip them out. This is usually the first step in the parsing (before validation).
- For phone numbers, I use a 3rd party library to simultaneously validate and format them correctly. This is usually done after verifying that the string is not null or empty (e.g. after some validation).
This is trivial with custom code, e.g.:
type PhoneNumber = PhoneNumber of string
module PhoneNumber =
let parse =
trim // trim: string -> string
>> notEmpty // notEmpty: string -> Result<string, ParseError>
>> Result.bind validPhoneNumber // validPhoneNumber: string -> Result<string, ParseError>
>> Result.map PhoneNumber
Based on the information in the readme, which demonstrates a list of validation functions, I can't immediately see how to accomplish having some validation steps also transform the value. Is there a simple/canonical way to achieve this with this library?
Right now type definitions don't support custom input processing, so you have to use Block.verbatim next to your own processing function, which can still be defined with the type itself since you can add any member to it as long as it implements the interface. If #9 gets implemented I'm pretty sure this would have to be part of the package.