Skip to content

Instantly share code, notes, and snippets.

@erikkaplun
Last active August 29, 2015 14:07
Show Gist options
  • Save erikkaplun/a6909452c58cb46ae7d5 to your computer and use it in GitHub Desktop.
Save erikkaplun/a6909452c58cb46ae7d5 to your computer and use it in GitHub Desktop.
import shapeless._
import poly._
import syntax.std.tuple._
type Va[+A] = Validation[String, A]
type Vali[+A, -B] = Kleisli[Va, A, B]
def Vali[A, B](fn: A => Va[B]): Vali[A, B] = Kleisli[Va, A, B](fn)
implicit val vaBinding = new Bind[Va] {
def map[A, B](fa: Va[A])(f: A => B): Va[B] = fa.map(f)
def bind[A, B](fa: Va[A])(f: A => Va[B]): Va[B] = fa.flatMap(f)
}
def nonEmpty[A] = (msg: String) => Vali { a: Option[A] =>
a.toSuccess(msg)
}
def validIso2CountryCode = (msg: String) => Vali { x: String =>
IsoCountryCodes2to3.get(x).toSuccess(msg)
}
val postalCode: Option[String] = request.param[String]("postalcode", allowEmpty = false)
val countryCode: Option[String] = request.param[String]("countrycode", allowEmpty = false)
val b = (
postalCode |> nonEmpty[String]("postal code missing"),
countryCode |> nonEmpty[String]("country code missing") >=> validIso2CountryCode("country code invalid")
)
b.map(_.toValidationNel).reduce(_ |@| _)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment