Created
December 19, 2014 12:50
-
-
Save Timshel/c984da64a7114433dd82 to your computer and use it in GitHub Desktop.
Validation API
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
package test | |
import play.api.data.mapping._ | |
object Test { | |
sealed trait Toto | |
case object toto1 extends Toto | |
case object toto2 extends Toto | |
case class Hey(first: String, toto: Toto) | |
implicit val totoR = Rule.fromMapping[String, Toto] { str => | |
Success(toto1) | |
} | |
def defaultR[T](default: T) = Rule.fromMapping[Option[T], T] { o => | |
Success(o.getOrElse(default)) | |
} | |
object Form { | |
import play.api.data.mapping.forms.{Rules, UrlFormEncoded} | |
import Rules._ | |
implicit val parsingRule = From[UrlFormEncoded] { __ => | |
( (__ \ "first").read[String] ~ | |
(__ \ "toto").read[Toto] | |
)(Hey.apply _) | |
} | |
} | |
object Json { | |
import play.api.data.mapping.json.Rules | |
import play.api.data.mapping.json._ | |
import play.api.libs.json._ | |
import Rules._ | |
implicit val parsingRule = From[JsValue] { __ => | |
( (__ \ "first").read[String] ~ | |
(__ \ "toto").read(totoR) | |
)(Hey.apply _) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment