Created
March 21, 2020 21:09
-
-
Save robhinds/260200135dff60afbedb33ec4d1b3213 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
val complex = ComplexElement( | |
value = List( | |
StringElement(value = "first element"), | |
StringElement(value = "second element") | |
) | |
) | |
import shapeless._ | |
sealed trait DefaultValidation extends Poly1 { | |
implicit def default[T] = at[T](x => true) | |
} | |
object validates extends DefaultValidation { | |
implicit def caseValidated[A](implicit v: ValidatorTypeClass[A]) = at[A](x => v.validate(x)) | |
} | |
object combine extends Poly2 { | |
implicit def caseValidation = at[Boolean, Boolean] (_ && _) | |
} | |
sealed trait ValidatorTypeClass[A] { | |
def validate(a: A): Boolean | |
} | |
object ValidatorTypeClass { | |
def validateElement[A](a: A)(implicit v: ValidatorTypeClass[A]) = v.validate(a) | |
implicit def stringElementValidator = new ValidatorTypeClass[String] { | |
override def validate(a: String): Boolean = true //validation logic for strings | |
} | |
implicit def numberElementValidator = new ValidatorTypeClass[Double] { | |
override def validate(a: Double): Boolean = true //validation logic for numbers | |
} | |
implicit def booleanElementValidator = new ValidatorTypeClass[Boolean] { | |
override def validate(a: Boolean): Boolean = true //validation logic for booleans | |
} | |
implicit def complexElementValidator = new ValidatorTypeClass[ComplexElement] { | |
override def validate(a: ComplexElement): Boolean = true | |
} | |
} | |
everything(validates)(combine)(complex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment