Skip to content

Instantly share code, notes, and snippets.

@gigiigig
Created February 2, 2015 17:29
Show Gist options
  • Save gigiigig/79ffc9a551daab82f4bc to your computer and use it in GitHub Desktop.
Save gigiigig/79ffc9a551daab82f4bc to your computer and use it in GitHub Desktop.
Validation example
import scalaz._
import Scalaz._
import shapeless._
import shapeless.record._
import shapeless.ops.record._
import shapeless.syntax.singleton._
object console extends App {
case class FieldError(message: String)
trait Validable extends Product {
implicit val v:Validable = this
implicit class RichValidationNel(vn: ValidationNel[FieldError, Validable]) {
def and(r: ValidationNel[FieldError, Validable]) = (vn |@| r)((a, _) => a)
def +(r: ValidationNel[FieldError, Validable]) = and(r)
}
def validate: ValidationNel[FieldError, Validable]
}
case class Constrain[T](errorMessage: String)(f: T => Boolean) {
def apply(fieldName: String, fieldValue: T)(implicit obj: Validable): ValidationNel[FieldError, Validable] =
if (f(fieldValue)) {
obj.successNel[FieldError]
} else {
FieldError(s"field: $fieldName $errorMessage").failureNel[Validable]
}
}
object Constrain {
def stringLength(length: Int): Constrain[String] =
Constrain[String](s"should not be longer than `$length`")(_.length < length)
def intBetween(min: Int, max: Int): Constrain[Int] =
Constrain[Int](s"should be between min:`$min` and max:`$max`")(f => min < f && f < max)
}
case class Foo(i: Int, s: String) extends Validable {
def validate =
Constrain.stringLength(0)("s", s) and
Constrain.intBetween(1, 5)("i", i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment