Validation
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.
Type parameters
- E
-
the type of error value describing a validation failure for this
Validation
Attributes
- Source
- Validation.scala
- Graph
-
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass Any
- Known subtypes