Skip to content

Instantly share code, notes, and snippets.

@gabfssilva
Created October 11, 2017 18:18
Show Gist options
  • Save gabfssilva/e2a8b3838771b3a5aace350ce7a3c408 to your computer and use it in GitHub Desktop.
Save gabfssilva/e2a8b3838771b3a5aace350ce7a3c408 to your computer and use it in GitHub Desktop.
type classes
private object Validations {
trait NotEmpty[F[_], T] {
def validate(field: F[T]): Boolean
}
implicit def optionNotEmpty[T]: NotEmpty[Option, T] = field => field.isDefined
implicit def listNotEmpty[T]: NotEmpty[List, T] = field => field.nonEmpty
def notEmpty[F[_], T](field: F[T])(implicit ev: NotEmpty[F, T]): Boolean = ev.validate(field)
}
import Validations._
case class Template(url: Option[String],
expectedHttpStatus: List[Int]) {
require(notEmpty(url), "url cannot be empty")
require(notEmpty(expectedHttpStatus), "expectedHttpStatus cannot be empty")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment