Skip to content

Instantly share code, notes, and snippets.

@erikrozendaal
Last active December 21, 2015 10:49

Revisions

  1. erikrozendaal revised this gist Aug 21, 2013. No changes.
  2. erikrozendaal revised this gist Aug 21, 2013. No changes.
  3. erikrozendaal revised this gist Aug 21, 2013. No changes.
  4. erikrozendaal created this gist Aug 21, 2013.
    9 changes: 9 additions & 0 deletions gistfile1.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    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)