Validator

object Validator

Simpler validator abstraction using Cats Validated https://typelevel.org/cats/datatypes/validated.html.

class Object
trait Matchable
class Any

Type members

Classlikes

final case class And extends Error

Sequence of errors resulting from the logical conjunction of constraints.

Sequence of errors resulting from the logical conjunction of constraints.

final implicit class BigDecimalMatchers(val value: BigDecimal) extends AnyVal
final implicit class BooleanOps(val value: Boolean) extends AnyVal
object Error
Companion:
class
sealed trait Error
Companion:
object
final implicit class IntMatchers(val value: Int) extends AnyVal
object Invalid

Invalid result helpers.

Invalid result helpers.

final implicit class OptionalBigDecimalMatchers(val value: Option[BigDecimal]) extends AnyVal
final implicit class OptionalIntMatchers(val value: Option[Int]) extends AnyVal
final implicit class OptionalStringMatchers(val value: Option[String]) extends AnyVal
final case class Or extends Error

Sequence of errors resulting from the logical disjunction of constraints.

Sequence of errors resulting from the logical disjunction of constraints.

final case class Single extends Error

Single error representation.

Single error representation.

final implicit class StringMatchers(val value: String) extends AnyVal
final implicit class ValidateOps[T](val thisValidate: T => Result)
final implicit class ValidationResultOps(val result: Result)

Types

type Result = Either[Error, Unit]

Validation result type.

Validation result type.

type Validate[-T] = T => Result

The validator function type.

The validator function type.

Value members

Concrete methods

def all[T](constraints: () => T*): () => T

Conjuction. Succeeds only if all constraints are valid.

Conjuction. Succeeds only if all constraints are valid.

def allWithComputedPrefix[T](errorPrefix: T => String, constraints: () => T*): () => T

Conjuction. Succeeds only if all constraints are valid, otherwise prepend calculated errorPrefix.

Conjuction. Succeeds only if all constraints are valid, otherwise prepend calculated errorPrefix.

def allWithPrefix[T](errorPrefix: String, constraints: () => T*): () => T

Conjuction. Succeeds only if all constraints are valid, otherwise prepend errorPrefix.

Conjuction. Succeeds only if all constraints are valid, otherwise prepend errorPrefix.

def always[T]: () => T

Validator that always succeeds.

Validator that always succeeds.

def any[T](constraints: () => T*): () => T

Disjunction. Succeeds if any of the constraints is valid.

Disjunction. Succeeds if any of the constraints is valid.

def anyWithComputedPrefix[T](errorPrefix: T => String, constraints: () => T*): () => T

Disjunction. Succeeds if any of the constraints is valid, otherwise prepend errorPrefix.

Disjunction. Succeeds if any of the constraints is valid, otherwise prepend errorPrefix.

def anyWithPrefix[T](errorPrefix: String, constraints: () => T*): () => T

Disjunction. Succeeds if any of the constraints is valid, otherwise prepend errorPrefix.

Disjunction. Succeeds if any of the constraints is valid, otherwise prepend errorPrefix.

def apply[T](constraints: () => T*): () => T

Runs all provided checks.

Runs all provided checks.

def checkEach[T, E](elements: T => Seq[E], constraint: () => E): () => T

Apply constraint to each element of the extracted sequence.

Apply constraint to each element of the extracted sequence.

def checkEachIfNonEmpty[T, E](elements: T => Seq[E], constraint: () => E): () => T

Apply constraint to each element of the extracted sequence if non empty.

Apply constraint to each element of the extracted sequence if non empty.

def checkEachIfNonEmptyWithErrorPrefix[T, E](elements: T => Seq[E], constraint: () => E, errorPrefix: Int => String): () => T

Apply constraint to each element of the extracted sequence if non empty. If invalid then compute and add prefix to the errors.

Apply constraint to each element of the extracted sequence if non empty. If invalid then compute and add prefix to the errors.

def checkEachIfSome[T, E](extract: T => Option[Seq[E]], constraint: () => E, isValidIfNone: Boolean): () => T

Apply constraint to each element of the extracted sequence if defined.

Apply constraint to each element of the extracted sequence if defined.

def checkEachIfSomeWithErrorPrefix[T, E](extract: T => Option[Seq[E]], constraint: () => E, errorPrefix: Int => String, isValidIfNone: Boolean): () => T

Apply constraint to each element of the extracted sequence if non empty. If invalid then compute and add prefix to the errors.

Apply constraint to each element of the extracted sequence if non empty. If invalid then compute and add prefix to the errors.

def checkEachWithErrorPrefix[T, E](elements: T => Seq[E], constraint: () => E, errorPrefix: Int => String): () => T

Apply constraint to each element of the extracted sequence. If invalid then compute and add prefix to the errors.

Apply constraint to each element of the extracted sequence. If invalid then compute and add prefix to the errors.

def checkEquals[T, A](value1: T => A, value2: T => A, error: String): () => T

Validate if two properties return the same value.

Validate if two properties return the same value.

def checkFromEither[T](test: T => Either[String, Any]): () => T

Validate if the test returns Right, otherwise fail with Left error.

Validate if the test returns Right, otherwise fail with Left error.

def checkIfAllDefined[T](extractors: Seq[T => Option[Any]], expectations: String): () => T

Check if all extracted optional properties are defined.

Check if all extracted optional properties are defined.

def checkIfAllEmpty[T](extractors: Seq[T => Option[Any]], expectations: String): () => T

Check if all extracted optional properties are empty.

Check if all extracted optional properties are empty.

def checkIfAllFalse[T](tests: Seq[T => Boolean], expectations: String): () => T

Check if all tests fails

Check if all tests fails

def checkIfAllOrNoneDefined[T](extractors: Seq[T => Option[Any]], expectations: String): () => T

Check if the extracted optional properties are either all defined or all empty.

Check if the extracted optional properties are either all defined or all empty.

def checkIfAllTrue[T](tests: Seq[T => Boolean], expectations: String): () => T

Check if all tests passes

Check if all tests passes

def checkIfAtLeastOneIsDefined[T](extractors: Seq[T => Option[Any]], expectations: String): () => T

Check if at least one extracted property is defined.

Check if at least one extracted property is defined.

def checkIfAtLeastOneIsTrue[T](tests: Seq[T => Boolean], expectations: String): () => T

Check if at least one test passes.

Check if at least one test passes.

def checkIfAtMostOneIsDefined[T](extractors: Seq[T => Option[Any]], expectations: String): () => T

Check if at most one extracted property is defined.

Check if at most one extracted property is defined.

def checkIfAtMostOneIsTrue[T](tests: Seq[T => Boolean], expectations: String): () => T

Check if at most one test passes.

Check if at most one test passes.

def checkIfOnlyOneIsDefined[T](extractors: Seq[T => Option[Any]], expectations: String): () => T

Check if one and only one extracted property is defined.

Check if one and only one extracted property is defined.

def checkIfOnlyOneIsTrue[T](tests: Seq[T => Boolean], expectations: String): () => T

Check if one and only one test passes.

Check if one and only one test passes.

def checkIfOnlyOneSetIsDefined[T](extractors: Seq[Set[T => Option[Any]]], expectations: String): () => T

Check if one and only one set of properties is fully defined.

Check if one and only one set of properties is fully defined.

def checkIfOnlyOneSetIsTrue[T](tests: Seq[Set[T => Boolean]], expectations: String): () => T

Check if one and only one set of tests passes.

Check if one and only one set of tests passes.

def checkIfSome[T, E](element: T => Option[E], constraint: () => E, isValidIfNone: Boolean): () => T

Apply constraint to the extracted property if defined, otherwise follow isValidIfNone flag.

Apply constraint to the extracted property if defined, otherwise follow isValidIfNone flag.

def checkIsDefined[T](test: T => Option[Any], error: String): () => T

Validate if the test returns Some, otherwise fail with error.

Validate if the test returns Some, otherwise fail with error.

def checkIsEmpty[T](test: T => Option[Any], error: String): () => T

Validate if the test returns None, otherwise fail with error.

Validate if the test returns None, otherwise fail with error.

def checkIsFalse[T](test: T => Boolean, error: String): () => T

Validate if the test fails, otherwise fail with error.

Validate if the test fails, otherwise fail with error.

def checkIsTrue[T](test: T => Boolean, error: String): () => T

Validate if the test passes, otherwise fail with error.

Validate if the test passes, otherwise fail with error.

def checkNotEquals[T, A](value1: T => A, value2: T => A, error: String): () => T

Validate if two properties return different value.

Validate if two properties return different value.

def checkWith[T, E](element: T => E, constraint: () => E): () => T

Validate with the provided constraint applied to the extracted property.

Validate with the provided constraint applied to the extracted property.

def checkWithImplicitly[T, E](element: T => E)(implicit constraint: () => E): () => T

Validate using the provided implicit constraint applied to the extracted property.

Validate using the provided implicit constraint applied to the extracted property.

def conditionally[T](test: T => Boolean, constraintWhenTrue: () => T, constraintWhenFalse: () => T): () => T

Depending on the test result follow continue with either first or second constraint.

Depending on the test result follow continue with either first or second constraint.

def never[T]: () => T

Validator that always fails.

Validator that always fails.

def product[A, B](constraintA: () => A, constraintB: () => B): () => (A, B)

Combine two constraints to make a constraint on a pair.

Combine two constraints to make a constraint on a pair.

def product[A, B, C](constraintA: () => A, constraintB: () => B, constraintC: () => C): () => (A, B, C)

Combine three constraints to make a constraint on a triplet.

Combine three constraints to make a constraint on a triplet.

def product[A, B, C, D](constraintA: () => A, constraintB: () => B, constraintC: () => C, constraintD: () => D): () => (A, B, C, D)

Combine four constraints to make a constraint on a tuple.

Combine four constraints to make a constraint on a tuple.

def validate[T](constraints: () => T*): () => T

Runs all provided checks. Provided as a named alias to the apply method.

Runs all provided checks. Provided as a named alias to the apply method.

def when[T](guardConstraint: () => T, constraintWhenValid: () => T, constraintWhenInvalid: () => T): () => T

Depending on the guard constraint result continue with either first or second constraint.

Depending on the guard constraint result continue with either first or second constraint.

def whenFalse[T](test: T => Boolean, constraintWhenFalse: () => T): () => T

If the test is false then try the next constraint, otherwise valid.

If the test is false then try the next constraint, otherwise valid.

def whenInvalid[T](guardConstraint: () => T, constraintWhenInvalid: () => T): () => T

If the guard constraint is invalid then try next constraint.

If the guard constraint is invalid then try next constraint.

def whenTrue[T](test: T => Boolean, constraintWhenTrue: () => T): () => T

If the test is true then check the next constraint, otherwise valid.

If the test is true then check the next constraint, otherwise valid.

def whenValid[T](guardConstraint: () => T, constraintWhenValid: () => T): () => T

If the guard constraint is valid then check next constraint.

If the guard constraint is valid then check next constraint.

Concrete fields

val Valid: Right[Nothing, Unit]

Successsful validation result alias.

Successsful validation result alias.

Implicits

Implicits

final implicit def BigDecimalMatchers(value: BigDecimal): BigDecimalMatchers
final implicit def BooleanOps(value: Boolean): BooleanOps
final implicit def IntMatchers(value: Int): IntMatchers
final implicit def OptionalBigDecimalMatchers(value: Option[BigDecimal]): OptionalBigDecimalMatchers
final implicit def OptionalIntMatchers(value: Option[Int]): OptionalIntMatchers
final implicit def OptionalStringMatchers(value: Option[String]): OptionalStringMatchers
final implicit def StringMatchers(value: String): StringMatchers
final implicit def ValidateOps[T](thisValidate: T => Result): ValidateOps[T]
final implicit def ValidationResultOps(result: Result): ValidationResultOps