Last active
March 28, 2017 09:16
-
-
Save dgouyette/f45eadb755a2e4169e9e43b84b8337b0 to your computer and use it in GitHub Desktop.
This file contains 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
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