Skip to content

Instantly share code, notes, and snippets.

@remeniuk
Created November 25, 2010 11:47
Show Gist options
  • Save remeniuk/715260 to your computer and use it in GitHub Desktop.
Save remeniuk/715260 to your computer and use it in GitHub Desktop.
//////////////// Scalaz Validation
sealed trait Name extends NewType[String]
object Name {
def apply(s: String): Validation[String, Name] = if (s.headOption.exists(_.isUpper))
(new Name {val value = s}).success
else
"Name must start with a capital letter".fail
}
sealed trait Age extends NewType[Int]
object Age {
def apply(a: Int): Validation[String, Age] = if (0 to 130 contains a)
(new Age {val value = a}).success
else
"Age must be in range".fail
}
case class Person(name: Name, age: Age)
//////////////// JEE 6 Bean Validation
case class Person(@NotNull @Pattern(regexp="[A-Z]\\w+") name: String, @Min(0) @Max(130) age: Int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment