Object

catslib

ValidatedSection

Related Doc: package catslib

Permalink

object ValidatedSection extends FlatSpec with Matchers with Section

Imagine you are filling out a web form to signup for an account. You input your username and password and submit. Response comes back saying your username can't have dashes in it, so you make some changes and resubmit. Can't have special characters either. Change, resubmit. Passwords need to have at least one capital letter. Change, resubmit. Password needs to have at least one number.

Or perhaps you're reading from a configuration file. One could imagine the configuration library you're using returns a scala.util.Try, or maybe a scala.util.Either (or cats.data.Xor). Your parsing may look something like:

case class ConnectionParams(url: String, port: Int)

for {
url  <- config[String]("url")
port <- config[Int]("port")
} yield ConnectionParams(url, port)

You run your program and it says key "url" not found, turns out the key was "endpoint". So you change your code and re-run. Now it says the "port" key was not a well-formed integer.

It would be nice to have all of these errors be reported simultaneously. That the username can't have dashes can be validated separately from it not having special characters, as well as from the password needing to have certain requirements. A misspelled (or missing) field in a config can be validated separately from another field not being well-formed.

Enter Validated.

Parallel validation

Our goal is to report any and all errors across independent bits of data. For instance, when we ask for several pieces of configuration, each configuration field can be validated separately from one another. How then do we enforce that the data we are working with is independent? We ask for both of them up front.

As our running example, we will look at config parsing. Our config will be represented by a Map[String, String]. Parsing will be handled by a Read type class - we provide instances just for String and Int for brevity.

trait Read[A] {
def read(s: String): Option[A]
}

object Read {
def apply[A](implicit A: Read[A]): Read[A] = A

implicit val stringRead: Read[String] =
 new Read[String] { def read(s: String): Option[String] = Some(s) }

implicit val intRead: Read[Int] =
 new Read[Int] {
   def read(s: String): Option[Int] =
     if (s.matches("-?[0-9]+")) Some(s.toInt)
     else None
 }
}

Then we enumerate our errors - when asking for a config value, one of two things can go wrong: the field is missing, or it is not well-formed with regards to the expected type.

sealed abstract class ConfigError
final case class MissingConfig(field: String) extends ConfigError
final case class ParseError(field: String) extends ConfigError

We need a data type that can represent either a successful value (a parsed configuration), or an error. It'd look like in the following example, which cats provides in cats.data.Validated.

sealed abstract class Validated[+E, +A]

object Validated {
final case class Valid[+A](a: A) extends Validated[Nothing, A]
final case class Invalid[+E](e: E) extends Validated[E, Nothing]
}

Now we are ready to write our parser.

import cats.data.Validated
import cats.data.Validated.{Invalid, Valid}

case class Config(map: Map[String, String]) {
def parse[A : Read](key: String): Validated[ConfigError, A] =
 map.get(key) match {
   case None        => Invalid(MissingConfig(key))
   case Some(value) =>
     Read[A].read(value) match {
       case None    => Invalid(ParseError(key))
       case Some(a) => Valid(a)
     }
 }
}

Everything is in place to write the parallel validator. Recall that we can only do parallel validation if each piece is independent. How do we enforce the data is independent? By asking for all of it up front. Let's start with two pieces of data.

def parallelValidate[E, A, B, C](v1: Validated[E, A], v2: Validated[E, B])(f: (A, B) => C): Validated[E, C] =
(v1, v2) match {
 case (Valid(a), Valid(b))       => Valid(f(a, b))
 case (Valid(_), i@Invalid(_))   => i
 case (i@Invalid(_), Valid(_))   => i
 case (Invalid(e1), Invalid(e2)) => ???
}

We've run into a problem. In the case where both have errors, we want to report both. But we have no way of combining the two errors into one error! Perhaps we can put both errors into a List, but that seems needlessly specific - clients may want to define their own way of combining errors.

How then do we abstract over a binary operation? The Semigroup type class captures this idea.

import cats.Semigroup

def parallelValidate[E : Semigroup, A, B, C](v1: Validated[E, A], v2: Validated[E, B])(f: (A, B) => C): Validated[E, C] =
(v1, v2) match {
 case (Valid(a), Valid(b))       => Valid(f(a, b))
 case (Valid(_), i@Invalid(_))   => i
 case (i@Invalid(_), Valid(_))   => i
 case (Invalid(e1), Invalid(e2)) => Invalid(Semigroup[E].combine(e1, e2))
}

Perfect! But.. going back to our example, we don't have a way to combine ConfigErrors. But as clients, we can change our Validated values where the error can be combined, say, a List[ConfigError]. It is more common however to use a NonEmptyList[ConfigError] - the NonEmptyList statically guarantees we have at least one value, which aligns with the fact that if we have an Invalid, then we most certainly have at least one error. This technique is so common there is a convenient method on Validated called toValidatedNel that turns any Validated[E, A] value to a Validated[NonEmptyList[E], A]. Additionally, the type alias ValidatedNel[E, A] is provided.

Time to parse.

import cats.SemigroupK
import cats.data.NonEmptyList
import cats.std.list._

implicit val nelSemigroup: Semigroup[NonEmptyList[ConfigError]] =
SemigroupK[NonEmptyList].algebra[ConfigError]

implicit val readString: Read[String] = Read.stringRead
implicit val readInt: Read[Int] = Read.intRead
Linear Supertypes
Section, Matchers, Explicitly, MatcherWords, Tolerance, FlatSpec, FlatSpecLike, Documenting, Alerting, Notifying, Informing, CanVerb, MustVerb, ShouldVerb, TestRegistration, Suite, Serializable, AbstractSuite, Assertions, TripleEquals, TripleEqualsSupport, AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. ValidatedSection
  2. Section
  3. Matchers
  4. Explicitly
  5. MatcherWords
  6. Tolerance
  7. FlatSpec
  8. FlatSpecLike
  9. Documenting
  10. Alerting
  11. Notifying
  12. Informing
  13. CanVerb
  14. MustVerb
  15. ShouldVerb
  16. TestRegistration
  17. Suite
  18. Serializable
  19. AbstractSuite
  20. Assertions
  21. TripleEquals
  22. TripleEqualsSupport
  23. AnyRef
  24. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Type Members

  1. final class AWord extends AnyRef

    Permalink
    Definition Classes
    Matchers
  2. final class AnWord extends AnyRef

    Permalink
    Definition Classes
    Matchers
  3. sealed class AnyShouldWrapper[T] extends AnyRef

    Permalink
    Definition Classes
    Matchers
  4. class AssertionsHelper extends AnyRef

    Permalink
    Definition Classes
    Assertions
  5. final class BehaviorWord extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  6. class CheckingEqualizer[L] extends AnyRef

    Permalink
    Definition Classes
    TripleEqualsSupport
  7. class DecidedByEquality[A] extends Equality[A]

    Permalink
    Definition Classes
    Explicitly
  8. class DecidedWord extends AnyRef

    Permalink
    Definition Classes
    Explicitly
  9. class DeterminedByEquivalence[T] extends Equivalence[T]

    Permalink
    Definition Classes
    Explicitly
  10. class DeterminedWord extends AnyRef

    Permalink
    Definition Classes
    Explicitly
  11. class Equalizer[L] extends AnyRef

    Permalink
    Definition Classes
    TripleEqualsSupport
  12. final class HavePropertyMatcherGenerator extends AnyRef

    Permalink
    Definition Classes
    Matchers
  13. final class IgnoreVerbString extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  14. final class IgnoreVerbStringTaggedAs extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  15. final class IgnoreWord extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  16. final class InAndIgnoreMethods extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  17. final class InAndIgnoreMethodsAfterTaggedAs extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  18. final class ItVerbString extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  19. final class ItVerbStringTaggedAs extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  20. final class ItWord extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  21. final class KeyWord extends AnyRef

    Permalink
    Definition Classes
    Matchers
  22. class LegacyCheckingEqualizer[L] extends AnyRef

    Permalink
    Definition Classes
    TripleEqualsSupport
  23. class LegacyEqualizer[L] extends AnyRef

    Permalink
    Definition Classes
    TripleEqualsSupport
  24. trait NoArgTest extends () ⇒ Outcome with TestData

    Permalink
    Attributes
    protected
    Definition Classes
    Suite
  25. final class PlusOrMinusWrapper[T] extends AnyRef

    Permalink
    Definition Classes
    Tolerance
  26. final class RegexWord extends AnyRef

    Permalink
    Definition Classes
    Matchers
  27. final class RegexWrapper extends AnyRef

    Permalink
    Definition Classes
    Matchers
  28. class ResultOfBeWordForAny[T] extends AnyRef

    Permalink
    Definition Classes
    Matchers
  29. sealed class ResultOfBeWordForCollectedAny[T] extends AnyRef

    Permalink
    Definition Classes
    Matchers
  30. final class ResultOfBeWordForCollectedArray[T] extends ResultOfBeWordForCollectedAny[Array[T]]

    Permalink
    Definition Classes
    Matchers
  31. final class ResultOfCollectedAny[T] extends AnyRef

    Permalink
    Definition Classes
    Matchers
  32. final class ResultOfContainWordForCollectedAny[T] extends AnyRef

    Permalink
    Definition Classes
    Matchers
  33. final class ResultOfEndWithWordForCollectedString extends AnyRef

    Permalink
    Definition Classes
    Matchers
  34. final class ResultOfEndWithWordForString extends AnyRef

    Permalink
    Definition Classes
    Matchers
  35. final class ResultOfEvaluatingApplication extends AnyRef

    Permalink
    Definition Classes
    Matchers
  36. final class ResultOfFullyMatchWordForCollectedString extends AnyRef

    Permalink
    Definition Classes
    Matchers
  37. final class ResultOfFullyMatchWordForString extends AnyRef

    Permalink
    Definition Classes
    Matchers
  38. final class ResultOfHaveWordForCollectedExtent[A] extends AnyRef

    Permalink
    Definition Classes
    Matchers
  39. final class ResultOfHaveWordForExtent[A] extends AnyRef

    Permalink
    Definition Classes
    Matchers
  40. final class ResultOfIncludeWordForCollectedString extends AnyRef

    Permalink
    Definition Classes
    Matchers
  41. final class ResultOfIncludeWordForString extends AnyRef

    Permalink
    Definition Classes
    Matchers
  42. sealed class ResultOfNotWordForCollectedAny[T] extends AnyRef

    Permalink
    Definition Classes
    Matchers
  43. final class ResultOfProduceInvocation[T] extends AnyRef

    Permalink
    Definition Classes
    Matchers
  44. final class ResultOfStartWithWordForCollectedString extends AnyRef

    Permalink
    Definition Classes
    Matchers
  45. final class ResultOfStartWithWordForString extends AnyRef

    Permalink
    Definition Classes
    Matchers
  46. trait StringCanWrapperForVerb extends AnyRef

    Permalink
    Definition Classes
    CanVerb
  47. trait StringMustWrapperForVerb extends AnyRef

    Permalink
    Definition Classes
    MustVerb
  48. final class StringShouldWrapper extends AnyShouldWrapper[String] with org.scalatest.Matchers.StringShouldWrapperForVerb

    Permalink
    Definition Classes
    Matchers
  49. trait StringShouldWrapperForVerb extends AnyRef

    Permalink
    Definition Classes
    ShouldVerb
  50. class TheAfterWord extends AnyRef

    Permalink
    Definition Classes
    Explicitly
  51. final class TheSameInstanceAsPhrase extends AnyRef

    Permalink
    Definition Classes
    Matchers
  52. final class TheyVerbString extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  53. final class TheyVerbStringTaggedAs extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  54. final class TheyWord extends AnyRef

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  55. final class ValueWord extends AnyRef

    Permalink
    Definition Classes
    Matchers

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. def !==[T](right: Spread[T]): TripleEqualsInvocationOnSpread[T]

    Permalink
    Definition Classes
    TripleEqualsSupport
  3. def !==(right: Null): TripleEqualsInvocation[Null]

    Permalink
    Definition Classes
    TripleEqualsSupport
  4. def !==[T](right: T): TripleEqualsInvocation[T]

    Permalink
    Definition Classes
    TripleEqualsSupport
  5. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  6. def <[T](right: T)(implicit arg0: Ordering[T]): ResultOfLessThanComparison[T]

    Permalink
    Definition Classes
    Matchers
  7. def <=[T](right: T)(implicit arg0: Ordering[T]): ResultOfLessThanOrEqualToComparison[T]

    Permalink
    Definition Classes
    Matchers
  8. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  9. def ===[T](right: Spread[T]): TripleEqualsInvocationOnSpread[T]

    Permalink
    Definition Classes
    TripleEqualsSupport
  10. def ===(right: Null): TripleEqualsInvocation[Null]

    Permalink
    Definition Classes
    TripleEqualsSupport
  11. def ===[T](right: T): TripleEqualsInvocation[T]

    Permalink
    Definition Classes
    TripleEqualsSupport
  12. def >[T](right: T)(implicit arg0: Ordering[T]): ResultOfGreaterThanComparison[T]

    Permalink
    Definition Classes
    Matchers
  13. def >=[T](right: T)(implicit arg0: Ordering[T]): ResultOfGreaterThanOrEqualToComparison[T]

    Permalink
    Definition Classes
    Matchers
  14. def a[T](implicit arg0: Manifest[T]): ResultOfATypeInvocation[T]

    Permalink
    Definition Classes
    Matchers
  15. val a: AWord

    Permalink
    Definition Classes
    Matchers
  16. val after: TheAfterWord

    Permalink
    Definition Classes
    Explicitly
  17. def alert: Alerter

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike → Alerting
  18. def all(xs: String)(implicit collecting: Collecting[Char, String]): ResultOfCollectedAny[Char]

    Permalink
    Definition Classes
    Matchers
  19. def all[K, V, JMAP[k, v] <: Map[k, v]](xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]]): ResultOfCollectedAny[Entry[K, V]]

    Permalink
    Definition Classes
    Matchers
  20. def all[E, C[_]](xs: C[E])(implicit collecting: Collecting[E, C[E]]): ResultOfCollectedAny[E]

    Permalink
    Definition Classes
    Matchers
  21. def allOf(firstEle: Any, secondEle: Any, remainingEles: Any*): ResultOfAllOfApplication

    Permalink
    Definition Classes
    Matchers
  22. def an[T](implicit arg0: Manifest[T]): ResultOfAnTypeInvocation[T]

    Permalink
    Definition Classes
    Matchers
  23. val an: AnWord

    Permalink
    Definition Classes
    Matchers
  24. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  25. macro def assert(condition: Boolean, clue: Any): Unit

    Permalink
    Definition Classes
    Assertions
  26. macro def assert(condition: Boolean): Unit

    Permalink
    Definition Classes
    Assertions
  27. macro def assertCompiles(code: String): Unit

    Permalink
    Definition Classes
    Assertions
  28. macro def assertDoesNotCompile(code: String): Unit

    Permalink
    Definition Classes
    Assertions
  29. def assertResult(expected: Any)(actual: Any): Unit

    Permalink
    Definition Classes
    Assertions
  30. def assertResult(expected: Any, clue: Any)(actual: Any): Unit

    Permalink
    Definition Classes
    Assertions
  31. macro def assertTypeError(code: String): Unit

    Permalink
    Definition Classes
    Assertions
  32. val assertionsHelper: AssertionsHelper

    Permalink
    Definition Classes
    Assertions
  33. macro def assume(condition: Boolean, clue: Any): Unit

    Permalink
    Definition Classes
    Assertions
  34. macro def assume(condition: Boolean): Unit

    Permalink
    Definition Classes
    Assertions
  35. def atLeast(num: Int, xs: String)(implicit collecting: Collecting[Char, String]): ResultOfCollectedAny[Char]

    Permalink
    Definition Classes
    Matchers
  36. def atLeast[K, V, JMAP[k, v] <: Map[k, v]](num: Int, xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]]): ResultOfCollectedAny[Entry[K, V]]

    Permalink
    Definition Classes
    Matchers
  37. def atLeast[E, C[_]](num: Int, xs: C[E])(implicit collecting: Collecting[E, C[E]]): ResultOfCollectedAny[E]

    Permalink
    Definition Classes
    Matchers
  38. def atLeastOneOf(firstEle: Any, secondEle: Any, remainingEles: Any*): ResultOfAtLeastOneOfApplication

    Permalink
    Definition Classes
    Matchers
  39. def atMost(num: Int, xs: String)(implicit collecting: Collecting[Char, String]): ResultOfCollectedAny[Char]

    Permalink
    Definition Classes
    Matchers
  40. def atMost[K, V, JMAP[k, v] <: Map[k, v]](num: Int, xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]]): ResultOfCollectedAny[Entry[K, V]]

    Permalink
    Definition Classes
    Matchers
  41. def atMost[E, C[_]](num: Int, xs: C[E])(implicit collecting: Collecting[E, C[E]]): ResultOfCollectedAny[E]

    Permalink
    Definition Classes
    Matchers
  42. def atMostOneOf(firstEle: Any, secondEle: Any, remainingEles: Any*): ResultOfAtMostOneOfApplication

    Permalink
    Definition Classes
    Matchers
  43. val be: BeWord

    Permalink
    Definition Classes
    MatcherWords
  44. val behave: BehaveWord

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  45. val behavior: BehaviorWord

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  46. def between(from: Int, upTo: Int, xs: String)(implicit collecting: Collecting[Char, String]): ResultOfCollectedAny[Char]

    Permalink
    Definition Classes
    Matchers
  47. def between[K, V, JMAP[k, v] <: Map[k, v]](from: Int, upTo: Int, xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]]): ResultOfCollectedAny[Entry[K, V]]

    Permalink
    Definition Classes
    Matchers
  48. def between[E, C[_]](from: Int, upTo: Int, xs: C[E])(implicit collecting: Collecting[E, C[E]]): ResultOfCollectedAny[E]

    Permalink
    Definition Classes
    Matchers
  49. def cancel(cause: Throwable): Nothing

    Permalink
    Definition Classes
    Assertions
  50. def cancel(message: String, cause: Throwable): Nothing

    Permalink
    Definition Classes
    Assertions
  51. def cancel(message: String): Nothing

    Permalink
    Definition Classes
    Assertions
  52. def cancel(): Nothing

    Permalink
    Definition Classes
    Assertions
  53. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  54. val compile: CompileWord

    Permalink
    Definition Classes
    MatcherWords
  55. val contain: ContainWord

    Permalink
    Definition Classes
    MatcherWords
  56. def conversionCheckedConstraint[A, B](implicit equivalenceOfA: Equivalence[A], cnv: (B) ⇒ A): Constraint[A, B]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  57. def convertEquivalenceToAToBConstraint[A, B](equivalenceOfB: Equivalence[B])(implicit ev: <:<[A, B]): Constraint[A, B]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  58. def convertEquivalenceToAToBConversionConstraint[A, B](equivalenceOfB: Equivalence[B])(implicit ev: (A) ⇒ B): Constraint[A, B]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  59. def convertEquivalenceToBToAConstraint[A, B](equivalenceOfA: Equivalence[A])(implicit ev: <:<[B, A]): Constraint[A, B]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  60. def convertEquivalenceToBToAConversionConstraint[A, B](equivalenceOfA: Equivalence[A])(implicit ev: (B) ⇒ A): Constraint[A, B]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  61. implicit def convertNumericToPlusOrMinusWrapper[T](pivot: T)(implicit arg0: Numeric[T]): PlusOrMinusWrapper[T]

    Permalink
    Definition Classes
    Tolerance
  62. implicit def convertSymbolToHavePropertyMatcherGenerator(symbol: Symbol): HavePropertyMatcherGenerator

    Permalink
    Definition Classes
    Matchers
  63. implicit def convertToAnyShouldWrapper[T](o: T): AnyShouldWrapper[T]

    Permalink
    Definition Classes
    Matchers
  64. def convertToCheckingEqualizer[T](left: T): CheckingEqualizer[T]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  65. implicit def convertToEqualizer[T](left: T): Equalizer[T]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  66. implicit def convertToInAndIgnoreMethods(resultOfStringPassedToVerb: ResultOfStringPassedToVerb): InAndIgnoreMethods

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  67. implicit def convertToInAndIgnoreMethodsAfterTaggedAs(resultOfTaggedAsInvocation: ResultOfTaggedAsInvocation): InAndIgnoreMethodsAfterTaggedAs

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  68. def convertToLegacyCheckingEqualizer[T](left: T): LegacyCheckingEqualizer[T]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  69. def convertToLegacyEqualizer[T](left: T): LegacyEqualizer[T]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  70. implicit def convertToRegexWrapper(o: Regex): RegexWrapper

    Permalink
    Definition Classes
    Matchers
  71. implicit def convertToStringCanWrapper(o: String): StringCanWrapperForVerb

    Permalink
    Definition Classes
    CanVerb
  72. implicit def convertToStringMustWrapper(o: String): StringMustWrapperForVerb

    Permalink
    Definition Classes
    MustVerb
  73. implicit def convertToStringShouldWrapper(o: String): StringShouldWrapper

    Permalink
    Definition Classes
    Matchers → ShouldVerb
  74. val decided: DecidedWord

    Permalink
    Definition Classes
    Explicitly
  75. def defaultEquality[A]: Equality[A]

    Permalink
    Definition Classes
    TripleEqualsSupport
  76. val defined: DefinedWord

    Permalink
    Definition Classes
    MatcherWords
  77. def definedAt[T](right: T): ResultOfDefinedAt[T]

    Permalink
    Definition Classes
    Matchers
  78. val determined: DeterminedWord

    Permalink
    Definition Classes
    Explicitly
  79. val empty: EmptyWord

    Permalink
    Definition Classes
    MatcherWords
  80. val endWith: EndWithWord

    Permalink
    Definition Classes
    MatcherWords
  81. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  82. def equal(o: Null): Matcher[AnyRef]

    Permalink
    Definition Classes
    Matchers
  83. def equal[T](spread: Spread[T]): Matcher[T]

    Permalink
    Definition Classes
    Matchers
  84. def equal(right: Any): MatcherFactory1[Any, Equality]

    Permalink
    Definition Classes
    MatcherWords
  85. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  86. def every(xs: String)(implicit collecting: Collecting[Char, String]): ResultOfCollectedAny[Char]

    Permalink
    Definition Classes
    Matchers
  87. def every[K, V, JMAP[k, v] <: Map[k, v]](xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]]): ResultOfCollectedAny[Entry[K, V]]

    Permalink
    Definition Classes
    Matchers
  88. def every[E, C[_]](xs: C[E])(implicit collecting: Collecting[E, C[E]]): ResultOfCollectedAny[E]

    Permalink
    Definition Classes
    Matchers
  89. def exactly(num: Int, xs: String)(implicit collecting: Collecting[Char, String]): ResultOfCollectedAny[Char]

    Permalink
    Definition Classes
    Matchers
  90. def exactly[K, V, JMAP[k, v] <: Map[k, v]](num: Int, xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]]): ResultOfCollectedAny[Entry[K, V]]

    Permalink
    Definition Classes
    Matchers
  91. def exactly[E, C[_]](num: Int, xs: C[E])(implicit collecting: Collecting[E, C[E]]): ResultOfCollectedAny[E]

    Permalink
    Definition Classes
    Matchers
  92. final def execute: Unit

    Permalink
    Definition Classes
    Suite
  93. final def execute(testName: String, configMap: ConfigMap, color: Boolean, durations: Boolean, shortstacks: Boolean, fullstacks: Boolean, stats: Boolean): Unit

    Permalink
    Definition Classes
    Suite
  94. val exist: ExistWord

    Permalink
    Definition Classes
    MatcherWords
  95. def expectedTestCount(filter: Filter): Int

    Permalink
    Definition Classes
    Suite → AbstractSuite
  96. def fail(cause: Throwable): Nothing

    Permalink
    Definition Classes
    Assertions
  97. def fail(message: String, cause: Throwable): Nothing

    Permalink
    Definition Classes
    Assertions
  98. def fail(message: String): Nothing

    Permalink
    Definition Classes
    Assertions
  99. def fail(): Nothing

    Permalink
    Definition Classes
    Assertions
  100. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  101. val fullyMatch: FullyMatchWord

    Permalink
    Definition Classes
    MatcherWords
  102. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  103. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  104. val have: HaveWord

    Permalink
    Definition Classes
    MatcherWords
  105. val ignore: IgnoreWord

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  106. def inOrder(firstEle: Any, secondEle: Any, remainingEles: Any*): ResultOfInOrderApplication

    Permalink
    Definition Classes
    Matchers
  107. def inOrderOnly[T](firstEle: Any, secondEle: Any, remainingEles: Any*): ResultOfInOrderOnlyApplication

    Permalink
    Definition Classes
    Matchers
  108. val include: IncludeWord

    Permalink
    Definition Classes
    MatcherWords
  109. def info: Informer

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike → Informing
  110. def intercept[T <: AnyRef](f: ⇒ Any)(implicit manifest: Manifest[T]): T

    Permalink
    Definition Classes
    Assertions
  111. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  112. val it: ItWord

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  113. val key: KeyWord

    Permalink
    Definition Classes
    Matchers
  114. val length: LengthWord

    Permalink
    Definition Classes
    MatcherWords
  115. def lowPriorityConversionCheckedConstraint[A, B](implicit equivalenceOfB: Equivalence[B], cnv: (A) ⇒ B): Constraint[A, B]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  116. def lowPriorityTypeCheckedConstraint[A, B](implicit equivalenceOfB: Equivalence[B], ev: <:<[A, B]): Constraint[A, B]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  117. def markup: Documenter

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike → Documenting
  118. val matchPattern: MatchPatternWord

    Permalink
    Definition Classes
    MatcherWords
  119. def message(expectedMessage: String): ResultOfMessageWordApplication

    Permalink
    Definition Classes
    Matchers
  120. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  121. def nestedSuites: IndexedSeq[Suite]

    Permalink
    Definition Classes
    Suite → AbstractSuite
  122. def no(xs: String)(implicit collecting: Collecting[Char, String]): ResultOfCollectedAny[Char]

    Permalink
    Definition Classes
    Matchers
  123. def no[K, V, JMAP[k, v] <: Map[k, v]](xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]]): ResultOfCollectedAny[Entry[K, V]]

    Permalink
    Definition Classes
    Matchers
  124. def no[E, C[_]](xs: C[E])(implicit collecting: Collecting[E, C[E]]): ResultOfCollectedAny[E]

    Permalink
    Definition Classes
    Matchers
  125. def noErrors(res0: Boolean, res1: String, res2: Int): Unit

    Permalink

    When no errors are present in the configuration, we get a ConnectionParams wrapped in a Valid instance.

  126. val noException: NoExceptionWord

    Permalink
    Definition Classes
    MatcherWords
  127. def noneOf(firstEle: Any, secondEle: Any, remainingEles: Any*): ResultOfNoneOfApplication

    Permalink
    Definition Classes
    Matchers
  128. val not: NotWord

    Permalink
    Definition Classes
    MatcherWords
  129. def note: Notifier

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike → Notifying
  130. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  131. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  132. def of[T](implicit ev: Manifest[T]): ResultOfOfTypeInvocation[T]

    Permalink
    Definition Classes
    Matchers
  133. def oneOf(firstEle: Any, secondEle: Any, remainingEles: Any*): ResultOfOneOfApplication

    Permalink
    Definition Classes
    Matchers
  134. def only(xs: Any*): ResultOfOnlyApplication

    Permalink
    Definition Classes
    Matchers
  135. def pending: PendingNothing

    Permalink
    Definition Classes
    Suite
  136. def pendingUntilFixed(f: ⇒ Unit): Unit

    Permalink
    Definition Classes
    Suite
  137. def produce[T](implicit arg0: Manifest[T]): ResultOfProduceInvocation[T]

    Permalink
    Definition Classes
    Matchers
  138. val readable: ReadableWord

    Permalink
    Definition Classes
    MatcherWords
  139. val regex: RegexWord

    Permalink
    Definition Classes
    Matchers
  140. final def registerIgnoredTest(testText: String, testTags: Tag*)(testFun: ⇒ Unit): Unit

    Permalink
    Definition Classes
    FlatSpecLike → TestRegistration
  141. final def registerTest(testText: String, testTags: Tag*)(testFun: ⇒ Unit): Unit

    Permalink
    Definition Classes
    FlatSpecLike → TestRegistration
  142. def rerunner: Option[String]

    Permalink
    Definition Classes
    Suite → AbstractSuite
  143. def run(testName: Option[String], args: Args): Status

    Permalink
    Definition Classes
    FlatSpecLike → Suite → AbstractSuite
  144. final def run(testName: Option[String], reporter: Reporter, stopper: Stopper, filter: Filter, configMap: Map[String, Any], distributor: Option[Distributor], tracker: Tracker): Status

    Permalink
    Definition Classes
    AbstractSuite
  145. def runNestedSuites(args: Args): Status

    Permalink
    Attributes
    protected
    Definition Classes
    Suite → AbstractSuite
  146. def runTest(testName: String, args: Args): Status

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike → Suite → AbstractSuite
  147. def runTests(testName: Option[String], args: Args): Status

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike → Suite → AbstractSuite
  148. def sequentialValidation(res0: Boolean, res1: Boolean): Unit

    Permalink

    Our parallelValidate function looks awfully like the Apply#map2 function.

    Apply

    Our parallelValidate function looks awfully like the Apply#map2 function.

    def map2[F[_], A, B, C](fa: F[A], fb: F[B])(f: (A, B) => C): F[C]

    Which can be defined in terms of Apply#ap and Apply#map, the very functions needed to create an Apply instance.

    Can we perhaps define an Apply instance for Validated? Better yet, can we define an Applicative instance?

    import cats.Applicative
    
    implicit def validatedApplicative[E : Semigroup]: Applicative[Validated[E, ?]] =
    new Applicative[Validated[E, ?]] {
     def ap[A, B](f: Validated[E, A => B])(fa: Validated[E, A]): Validated[E, B] =
       (fa, f) match {
         case (Valid(a), Valid(fab)) => Valid(fab(a))
         case (i@Invalid(_), Valid(_)) => i
         case (Valid(_), i@Invalid(_)) => i
         case (Invalid(e1), Invalid(e2)) => Invalid(Semigroup[E].combine(e1, e2))
       }
    
     def pure[A](x: A): Validated[E, A] = Validated.valid(x)
     def map[A, B](fa: Validated[E, A])(f: A => B): Validated[E, B] = fa.map(f)
     def product[A, B](fa: Validated[E, A], fb: Validated[E, B]): Validated[E, (A, B)] =
       ap(fa.map(a => (b: B) => (a, b)))(fb)
    }

    Awesome! And now we also get access to all the goodness of Applicative, which includes map{2-22}, as well as the Cartesian syntax |@|.

    We can now easily ask for several bits of configuration and get any and all errors returned back.

    import cats.Apply
    import cats.data.ValidatedNel
    
    implicit val nelSemigroup: Semigroup[NonEmptyList[ConfigError]] =
    SemigroupK[NonEmptyList].algebra[ConfigError]
    
    val config = Config(Map(("name", "cat"), ("age", "not a number"), ("houseNumber", "1234"), ("lane", "feline street")))
    
    case class Address(houseNumber: Int, street: String)
    case class Person(name: String, age: Int, address: Address)

    Thus.

    val personFromConfig: ValidatedNel[ConfigError, Person] =
    Apply[ValidatedNel[ConfigError, ?]].map4(config.parse[String]("name").toValidatedNel,
                                            config.parse[Int]("age").toValidatedNel,
                                            config.parse[Int]("house_number").toValidatedNel,
                                            config.parse[String]("street").toValidatedNel) {
     case (name, age, houseNumber, street) => Person(name, age, Address(houseNumber, street))

    We can now rewrite validations in terms of Apply.

    Of flatMaps and Xors

    Option has flatMap, Xor has flatMap, where's Validated's? Let's try to implement it - better yet, let's implement the Monad type class.

    import cats.Monad
    
    implicit def validatedMonad[E]: Monad[Validated[E, ?]] =
    new Monad[Validated[E, ?]] {
     def flatMap[A, B](fa: Validated[E, A])(f: A => Validated[E, B]): Validated[E, B] =
       fa match {
         case Valid(a)     => f(a)
         case i@Invalid(_) => i
       }
    
     def pure[A](x: A): Validated[E, A] = Valid(x)
    }

    Note that all Monad instances are also Applicative instances, where ap is defined as

    trait Monad[F[_]] {
    def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B]
    def pure[A](x: A): F[A]
    
    def map[A, B](fa: F[A])(f: A => B): F[B] =
     flatMap(fa)(f.andThen(pure))
    
    def ap[A, B](fa: F[A])(f: F[A => B]): F[B] =
     flatMap(fa)(a => map(f)(fab => fab(a)))
    }

    However, the ap behavior defined in terms of flatMap does not behave the same as that of our ap defined above. Observe:

    val v = validatedMonad.tuple2(Validated.invalidNel[String, Int]("oops"), Validated.invalidNel[String, Double]("uh oh"))

    This one short circuits! Therefore, if we were to define a Monad (or FlatMap) instance for Validated we would have to override ap to get the behavior we want. But then the behavior of flatMap would be inconsistent with that of ap, not good. Therefore, Validated has only an Applicative instance.

    Sequential Validation

    If you do want error accumulation but occasionally run into places where you sequential validation is needed, then Validated provides a couple methods that may be helpful.

    andThen

    The andThen method is similar to flatMap (such as Xor.flatMap). In the cause of success, it passes the valid value into a function that returns a new Validated instance.

    val houseNumber = config.parse[Int]("house_number").andThen{ n =>
    if (n >= 0) Validated.valid(n)
    else Validated.invalid(ParseError("house_number"))
    }
  149. implicit val shorthandSharedTestRegistrationFunction: (String) ⇒ BehaveWord

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  150. implicit val shorthandTestRegistrationFunction: (String, String, String) ⇒ ResultOfStringPassedToVerb

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  151. val size: SizeWord

    Permalink
    Definition Classes
    MatcherWords
  152. def someErrors(res0: Boolean, res1: Boolean): Unit

    Permalink

    But what happens when having one or more errors? They are accumulated in a NonEmptyList wrapped in a Invalid instance.

  153. val sorted: SortedWord

    Permalink
    Definition Classes
    MatcherWords
  154. val startWith: StartWithWord

    Permalink
    Definition Classes
    MatcherWords
  155. final val styleName: String

    Permalink
    Definition Classes
    FlatSpecLike → Suite → AbstractSuite
  156. def suiteId: String

    Permalink
    Definition Classes
    Suite
  157. def suiteName: String

    Permalink
    Definition Classes
    Suite
  158. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  159. def tags: Map[String, Set[String]]

    Permalink
    Definition Classes
    FlatSpecLike → Suite → AbstractSuite
  160. def testDataFor(testName: String, theConfigMap: ConfigMap): TestData

    Permalink
    Definition Classes
    FlatSpecLike → Suite
  161. def testNames: Set[String]

    Permalink
    Definition Classes
    FlatSpecLike → Suite → AbstractSuite
  162. def the[T](implicit arg0: Manifest[T]): ResultOfTheTypeInvocation[T]

    Permalink
    Definition Classes
    Matchers
  163. def theSameElementsAs(xs: GenTraversable[_]): ResultOfTheSameElementsAsApplication

    Permalink
    Definition Classes
    Matchers
  164. def theSameElementsInOrderAs(xs: GenTraversable[_]): ResultOfTheSameElementsInOrderAsApplication

    Permalink
    Definition Classes
    Matchers
  165. val theSameInstanceAs: TheSameInstanceAsPhrase

    Permalink
    Definition Classes
    Matchers
  166. val they: TheyWord

    Permalink
    Attributes
    protected
    Definition Classes
    FlatSpecLike
  167. def thrownBy(fun: ⇒ Any): ResultOfThrownByApplication

    Permalink
    Definition Classes
    Matchers
  168. def toString(): String

    Permalink
    Definition Classes
    FlatSpec → AnyRef → Any
  169. def trap[T](f: ⇒ T): Throwable

    Permalink
    Definition Classes
    Assertions
  170. val typeCheck: TypeCheckWord

    Permalink
    Definition Classes
    MatcherWords
  171. def typeCheckedConstraint[A, B](implicit equivalenceOfA: Equivalence[A], ev: <:<[B, A]): Constraint[A, B]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  172. implicit def unconstrainedEquality[A, B](implicit equalityOfA: Equality[A]): Constraint[A, B]

    Permalink
    Definition Classes
    TripleEquals → TripleEqualsSupport
  173. def validatedAsXor(res0: Boolean, res1: Boolean): Unit

    Permalink

    The withXor method allows you to temporarily turn a Validated instance into an Xor instance and apply it to a function.

    withXor

    The withXor method allows you to temporarily turn a Validated instance into an Xor instance and apply it to a function.

    import cats.data.Xor
    
    def positive(field: String, i: Int): ConfigError Xor Int = {
    if (i >= 0) Xor.right(i)
    else Xor.left(ParseError(field))
    }

    So we can get Xor's short-circuiting behaviour when using the Validated type.

  174. val value: ValueWord

    Permalink
    Definition Classes
    Matchers
  175. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  176. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  177. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  178. def withClue[T](clue: Any)(fun: ⇒ T): T

    Permalink
    Definition Classes
    Assertions
  179. def withFixture(test: NoArgTest): Outcome

    Permalink
    Attributes
    protected
    Definition Classes
    Suite → AbstractSuite
  180. val writable: WritableWord

    Permalink
    Definition Classes
    MatcherWords

Deprecated Value Members

  1. def assert(o: Option[String]): Unit

    Permalink
    Definition Classes
    Assertions
    Annotations
    @deprecated
    Deprecated

    This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.

  2. def assert(o: Option[String], clue: Any): Unit

    Permalink
    Definition Classes
    Assertions
    Annotations
    @deprecated
    Deprecated

    This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.

  3. def assume(o: Option[String]): Unit

    Permalink
    Definition Classes
    Assertions
    Annotations
    @deprecated
    Deprecated

    This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.

  4. def assume(o: Option[String], clue: Any): Unit

    Permalink
    Definition Classes
    Assertions
    Annotations
    @deprecated
    Deprecated

    This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.

  5. def evaluating(fun: ⇒ Any): ResultOfEvaluatingApplication

    Permalink
    Definition Classes
    Matchers
    Annotations
    @deprecated
    Deprecated

    Please use 'an [Exception] should be thrownBy { ... }' syntax instead

  6. def expect(expected: Any)(actual: Any): Unit

    Permalink
    Definition Classes
    Assertions
    Annotations
    @deprecated
    Deprecated

    This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.

  7. def expect(expected: Any, clue: Any)(actual: Any): Unit

    Permalink
    Definition Classes
    Assertions
    Annotations
    @deprecated
    Deprecated

    This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.

  8. def expectResult(expected: Any)(actual: Any): Unit

    Permalink
    Definition Classes
    Assertions
    Annotations
    @deprecated
    Deprecated

    This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.

  9. def expectResult(expected: Any, clue: Any)(actual: Any): Unit

    Permalink
    Definition Classes
    Assertions
    Annotations
    @deprecated
    Deprecated

    This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.

Inherited from Section

Inherited from Matchers

Inherited from Explicitly

Inherited from MatcherWords

Inherited from Tolerance

Inherited from FlatSpec

Inherited from FlatSpecLike

Inherited from Documenting

Inherited from Alerting

Inherited from Notifying

Inherited from Informing

Inherited from CanVerb

Inherited from MustVerb

Inherited from ShouldVerb

Inherited from TestRegistration

Inherited from Suite

Inherited from Serializable

Inherited from AbstractSuite

Inherited from Assertions

Inherited from TripleEquals

Inherited from TripleEqualsSupport

Inherited from AnyRef

Inherited from Any

Ungrouped