Skip to content

Instantly share code, notes, and snippets.

@Timshel
Created December 19, 2014 12:50
Show Gist options
  • Save Timshel/c984da64a7114433dd82 to your computer and use it in GitHub Desktop.
Save Timshel/c984da64a7114433dd82 to your computer and use it in GitHub Desktop.
Validation API
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