Object/Trait

zio.prelude

Assertion

Related Docs: trait Assertion | package prelude

Permalink

object Assertion

An Assertion[A] is essentially a composable predicate from A => Boolean. They can be composed with standard Boolean operators of &&, || and !. This is primarily intended to be used with Newtype and Subtype, enhancing them with compile-time time validation.

For example, if you'd like to validate that a particular Int is precisely 4 digits long, you can create the following refined Newtype. (Note that the syntax is slightly difference between Scala 2 and Scala 3).

type Pin = Pin.Type
object Pin extends Newtype[Int] {
  // Scala 2 Syntax
  def assertion =
    assert(Assertion.between(1000, 9999))

  // Scala 3 Syntax
  override inline def assertion =
    Assertion.between(1000, 9999)
}

// PowerOfTwo(1000) compiles
// PowerOfTwo(5412) compiles
// PowerOfTwo(34567) fails with "34567 did not satisfy between(1000, 9999)"
// PowerOfTwo(234) fails with "123 did not satisfy between(1000, 9999)"
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Assertion
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. sealed trait Regex extends AnyRef

    Permalink

Value Members

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

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. object Regex

    Permalink
  5. val anything: Assertion[Any]

    Permalink
  6. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  7. def between[A](min: A, max: A)(implicit ordering: Ordering[A]): Assertion[A]

    Permalink

    Ensures the value falls between a given min and max (inclusive).

  8. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  9. def contains(string: String): Assertion[String]

    Permalink
  10. def divisibleBy[A](n: A)(implicit numeric: Numeric[A]): Assertion[A]

    Permalink
  11. def endsWith(suffix: String): Assertion[String]

    Permalink
  12. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  13. def equalTo[A](value: A): Assertion[A]

    Permalink
  14. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  15. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  16. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  17. def greaterThan[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]

    Permalink
  18. def greaterThanOrEqualTo[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]

    Permalink
  19. def hasLength(lengthAssertion: Assertion[Int]): Assertion[String]

    Permalink
  20. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  21. val isEmptyString: Assertion[String]

    Permalink
  22. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  23. def lessThan[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]

    Permalink
  24. def lessThanOrEqualTo[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]

    Permalink
  25. def matches(regex: scala.util.matching.Regex): Assertion[String]

    Permalink

    Matches a scala.util.matching.Regex.

    Matches a scala.util.matching.Regex.

    In order to use this for compile-time Assertions, make sure to use the string literal extension method, e.g.:

    Assertion.matches("\\w+@\\d{3,5}".r)
  26. def matches(regexString: String): Assertion[String]

    Permalink
  27. def matches(regex: Regex): Assertion[String]

    Permalink
  28. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  29. val never: Assertion[Any]

    Permalink
  30. def notEqualTo[A](value: A): Assertion[A]

    Permalink
  31. final def notify(): Unit

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

    Permalink
    Definition Classes
    AnyRef
  33. def powerOf[A](base: A)(implicit numeric: Numeric[A]): Assertion[A]

    Permalink

    Ensures that the value is a power of the given base.

    Ensures that the value is a power of the given base.

    type PowerOfTwo = PowerOfTwo.Type
    object PowerOfTwo extends Newtype[Int] {
      def assertion =
        assert(Assertion.powerOf(2))
    }
    
    // PowerOfTwo(1024) compiles
    // PowerOfTwo(1025) fails
  34. def startsWith(prefix: String): Assertion[String]

    Permalink
  35. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  36. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  37. final def wait(): Unit

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

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

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped