Skip to content

Instantly share code, notes, and snippets.

@erikrozendaal
Last active December 21, 2015 10:49
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 erikrozendaal/6294954 to your computer and use it in GitHub Desktop.
Save erikrozendaal/6294954 to your computer and use it in GitHub Desktop.
Scalaz equivalent to Hibernate's JSR-303 based @notempty annotation
def notNull[A](a: A): Validation[String, A] =
if (a != null) a.success else "may not be null".failure
def size[A <% Traversable[_]](min: Int = 0, max: Int = Int.MaxValue)(a: A): Validation[String, A] =
if (min to max contains a.size) a.success else s"size must be between $min and $max".failure
// Composing notNull and size, like https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/constraints/NotEmpty.java#L48
def notEmpty[A <% Traversable[_]](a: A): Validation[String, A] =
notNull(a) flatMap size(min = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment