Skip to content

Instantly share code, notes, and snippets.

@atamborrino
Created May 22, 2015 13:28
Show Gist options
  • Save atamborrino/bc92b7aa9dafd0365c1c to your computer and use it in GitHub Desktop.
Save atamborrino/bc92b7aa9dafd0365c1c to your computer and use it in GitHub Desktop.
validation.scala
def trying[O](f: String => O)(msg: String) =
Rule.fromMapping[String, O] { s =>
Try(f(s))
.map(Success[ValidationError, O](_))
.getOrElse(Failure[ValidationError, O](Seq(ValidationError(msg, s))))
}
val stringToDoubleR: Rule[String, Double] = trying(_.toDouble)("error.double")
val stringToLongR: Rule[String, Long] = trying(_.toLong)("error.long")
val stringToIntR: Rule[String, Int] = trying(_.toInt)("error.int")
def localDateR(formatter: DateTimeFormatter) = trying { str =>
LocalDate.parse(str, formatter)
}("error.localdate")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment