Created
May 22, 2015 13:28
-
-
Save atamborrino/bc92b7aa9dafd0365c1c to your computer and use it in GitHub Desktop.
validation.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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