Skip to content

Instantly share code, notes, and snippets.

@dgouyette
Last active March 28, 2017 09:16
Show Gist options
  • Save dgouyette/f45eadb755a2e4169e9e43b84b8337b0 to your computer and use it in GitHub Desktop.
Save dgouyette/f45eadb755a2e4169e9e43b84b8337b0 to your computer and use it in GitHub Desktop.
import jto.validation.{Rule, VA}
import shapeless.Witness
import shapeless.labelled.{FieldType, field}
trait Validator {
this: String =>
val rules: Rule[String, String]
def validate: VA[String] = rules.validate(this)
}
object EmailValidator{
this: String =>
val rules: Rule[String, String] = Rules.email |+| Rules.notEmpty
}
type Email = FieldType[EmailValidator.type , String]
implicit def email(value: String): FieldType[EmailValidator.type , String] = field[EmailValidator.type ](value)
def getFieldName[K, V](value: FieldType[K, V])(implicit witness: Witness.Aux[K]): K = witness.value
def getFieldValue[K, V](value: FieldType[K, V]): V = value
case class Row(email: Email) {
}
val invalidRow = Row("")
getFieldValue(invalidRow.email)
getFieldName(invalidRow.email) match {
case EmailValidator => EmailValidator.rules.validate(invalidRow.email)
case _ => EmailValidator.rules.validate("")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment