Ands this Validation with another, passed, Validation.
Ands this Validation with another, passed, Validation.
The result of and-ing two Validations is:
| Expression | Result |
|---|---|
Pass && Pass | Pass |
Pass && Fail(right) | Fail(right) |
Fail(left) && Pass | Fail(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)
the other validation to and with this one
the result of anding this Validation with the other, passed, Validation
Represents the result of a validation, either the object
Passif the validation succeeded, else an instance ofFailcontaining an error value describing the validation failure.
BecauseValidations are used to filterOrs inforexpressions orfiltermethod calls. For example, consider these methods:isRoundandisDivBy3take anIntand return aValidation[ErrorMessage], you can use them in filters inforexpressions involvingOrs of typeIntOrErrorMessage. Here's an example:Validations can also be used to accumulate error usingwhen, a method that's made available by traitAccumulationon accumualtingOrs (Ors whoseBadtype is anEvery[T]). Here are some examples: Note: You can think ofValidationas an “Optionwith attitude,” wherePassis aNonethat indicates validation success andFailis aSomewhose value describes the validation failure.the type of error value describing a validation failure for this
Validation