Assertion

org.finos.morphir.prelude.Assertion$
See theAssertion companion trait
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)"

Attributes

Companion
trait
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Assertion.type

Members list

Type members

Classlikes

object Regex

Attributes

Companion
trait
Supertypes
trait Sum
trait Mirror
class Object
trait Matchable
class Any
Self type
Regex.type
sealed trait Regex

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Alphanumeric
class AndThen
object AnyChar.type
object Anything.type
class CharacterSet
class Digit
object End.type
class Literal
class OrElse
class Range
class Repeat
object Start.type
class Whitespace
Show all
Self type

Value members

Concrete methods

def between[A](min: A, max: A)(implicit ordering: Ordering[A]): Assertion[A]

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

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

Attributes

def contains(string: String): Assertion[String]
def divisibleBy[A](n: A)(implicit numeric: Numeric[A]): Assertion[A]
def endsWith(suffix: String): Assertion[String]
def equalTo[A](value: A): Assertion[A]
def greaterThan[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]
def greaterThanOrEqualTo[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]
def hasLength(lengthAssertion: Assertion[Int]): Assertion[String]
def lessThan[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]
def lessThanOrEqualTo[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]
def matches(regex: Regex): Assertion[String]
def matches(regexString: String): Assertion[String]
def matches(regex: Regex): Assertion[String]

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)

Attributes

def notEqualTo[A](value: A): Assertion[A]
def powerOf[A](base: A)(implicit numeric: Numeric[A]): Assertion[A]

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

Attributes

def startsWith(prefix: String): Assertion[String]

Concrete fields

val anything: Assertion[Any]
val isEmptyString: Assertion[String]
val never: Assertion[Any]