Skip to content

Instantly share code, notes, and snippets.

@dgouyette
Created May 5, 2017 09:08
Show Gist options
  • Save dgouyette/2777b950dbf209acc5ef6881a821af41 to your computer and use it in GitHub Desktop.
Save dgouyette/2777b950dbf209acc5ef6881a821af41 to your computer and use it in GitHub Desktop.
import jto.validation._
sealed trait Color
object Color {
case object Red extends Color
case object Orange extends Color
case object Green extends Color
val colorR : Rule[String, Color] = Rule.fromMapping {
case "Red" => Valid(Red)
case "Orange" => Valid(Orange)
case "Green" => Valid(Green)
case other => Invalid(Seq(ValidationError("unhandled.value", other)))
}
}
Color.colorR.validate("aaa")//Invalid(List((/,List(ValidationError(List(unhandled.value),WrappedArray(aaa))))))
Color.colorR.validate("Red")//Valid(Red)
@jeantil
Copy link

jeantil commented May 9, 2017

ça vaudrait le coup de regarder dans contrib si il y a pas des trucs a réutiliser plutôt que de les redéfinir je pense en particulier a Class Or Trait ou a Hierarchy

genre https://github.com/scalameta/scalameta/blob/master/scalameta/contrib/shared/src/main/scala/scala/meta/contrib/TreeOps.scala#L58 pour hierarchy .. ?

@dgouyette
Copy link
Author

dgouyette commented May 10, 2017

Bonne idée. Maintenant que cela fonctionne, je vais essayer de simplifier mon code.
Merci pour l'info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment