t

org.scalactic

Validation

sealed trait Validation[+E] extends Product with Serializable

Represents the result of a validation, either the object Pass if the validation succeeded, else an instance of Fail containing an error value describing the validation failure.

Validations are used to filter Ors in for expressions or filter method calls. For example, consider these methods:

import org.scalactic._

def isRound(i: Int): Validation[ErrorMessage] = if (i % 10 == 0) Pass else Fail(i + " was not a round number")
def isDivBy3(i: Int): Validation[ErrorMessage] = if (i % 3 == 0) Pass else Fail(i + " was not divisible by 3")

Because isRound and isDivBy3 take an Int and return a Validation[ErrorMessage], you can use them in filters in for expressions involving Ors of type Int Or ErrorMessage. Here's an example:

for (i <- Good(3) if isRound(i) && isDivBy3(i)) yield i
// Result: Bad(3 was not a round number)

Validations can also be used to accumulate error using when, a method that's made available by trait Accumulation on accumualting Ors (Ors whose Bad type is an Every[T]). Here are some examples:

import Accumulation._

for (i <- Good(3) when (isRound, isDivBy3)) yield i // Result: Bad(One(3 was not a round number))
for (i <- Good(4) when (isRound, isDivBy3)) yield i // Result: Bad(Many(4 was not a round number, 4 was not divisible by 3))

Note: You can think of Validation as an “Option with attitude,” where Pass is a None that indicates validation success and Fail is a Some whose value describes the validation failure.

E

the type of error value describing a validation failure for this Validation

Source
Validation.scala
Linear Supertypes
Serializable, Product, Equals, AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Validation
  2. Serializable
  3. Product
  4. Equals
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Abstract Value Members

  1. abstract def &&[F >: E](other: => Validation[F]): Validation[F]

    Ands this Validation with another, passed, Validation.

    Ands this Validation with another, passed, Validation.

    The result of and-ing two Validations is:

    ExpressionResult
    Pass && PassPass
    Pass && Fail(right)Fail(right)
    Fail(left) && PassFail(left)
    Fail(left) && Fail(right)Fail(left)

    As you can see in the above table, no attempt is made by && to accumulate errors, which in turn means that no constraint is placed on the E type (it need not be an Every). Instead, && short circuits and returns the first Fail it encounters. This makes it useful in filters in for expressions involving Ors. Here's an example:

    import org.scalactic._
    
    def isRound(i: Int): Validation[ErrorMessage] = if (i % 10 != 0) Fail(i + " was not a round number") else Pass
    def isDivBy3(i: Int): Validation[ErrorMessage] = if (i % 3 != 0) Fail(i + " was not divisible by 3") else Pass
    for (i <- Good(3) if isRound(i) && isDivBy3(i)) yield i // Result: Bad(3 was not a round number)

    other

    the other validation to and with this one

    returns

    the result of anding this Validation with the other, passed, Validation

  2. abstract def canEqual(that: Any): Boolean
    Definition Classes
    Equals
  3. abstract def productArity: Int
    Definition Classes
    Product
  4. abstract def productElement(n: Int): Any
    Definition Classes
    Product

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  9. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  12. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  13. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  14. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  15. def productElementName(n: Int): String
    Definition Classes
    Product
  16. def productElementNames: Iterator[String]
    Definition Classes
    Product
  17. def productIterator: Iterator[Any]
    Definition Classes
    Product
  18. def productPrefix: String
    Definition Classes
    Product
  19. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  20. def toString(): String
    Definition Classes
    AnyRef → Any
  21. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  22. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  23. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from AnyRef

Inherited from Any

Ungrouped