Skip to content
Brian Marick edited this page Sep 22, 2017 · 2 revisions

Full source of my solution

Each of the validations trims the string, parses it, throws away valid integers or floats that exceed boundaries, and then creates a ValidatedString. That looks like a job for pipeline:

createVia : (String -> Result err a)  -- string parser
         -> (a -> Bool)               -- parses, but is it in bounds?
         -> (String -> ValidatedString a)
createVia parser boundsChecker string =
  string
    |> String.trim
    |> parser
    |> Result.toMaybe
    |> Maybe.filter boundsChecker
    |> ValidatedString.make string

Then each validator can be defined concisely:

minutes : String -> ValidatedString Int
minutes =
  createVia
    String.toInt
    (\i -> i >= 0 && i < 60)

Clone this wiki locally