Skip to content

Instantly share code, notes, and snippets.

@dgouyette
Last active May 18, 2017 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgouyette/b2be561892725dd74c747f6ce175849a to your computer and use it in GitHub Desktop.
Save dgouyette/b2be561892725dd74c747f6ce175849a to your computer and use it in GitHub Desktop.
PathOrRepath
import pivot.Rules
import jto.validation._
Rules.maxLength(2).validate("messageWithoutPath")//Invalid(List((/,List(ValidationError(List(error.maxLength),WrappedArray(2))))))
val nomEmptyWithPAth = Rules.notEmpty.repath(_ => Path \"newPath")
nomEmptyWithPAth.validate("")// Invalid(List((/newPath,List(ValidationError(List(error.required),WrappedArray())))))
@dgouyette
Copy link
Author

Un coup de scala meta pour faire le repath

package validation

import scala.meta._


class RepathByName extends scala.annotation.StaticAnnotation {
  inline def apply(defn: Any): Any = meta {

    defn match {
      case q"..$mods val ${Pat.Var.Term(Term.Name(name))} : $tpe = $expr" =>
        val result = q"""val ${Pat.Var.Term(Term.Name(name))} : $tpe  = $expr repath(_ => jto.validation.Path \ ${Lit.String(name)})"""
        result
    }
  }
}

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