ProductPromotion
Logo

Scala

made by https://0x3d.site

GitHub - splink/veto: If you don't agree with the data
If you don't agree with the data. Contribute to splink/veto development by creating an account on GitHub.
Visit Site

GitHub - splink/veto: If you don't agree with the data

GitHub - splink/veto: If you don't agree with the data

Build Status

Veto

A scala validation library without dependencies.

Let the code speak:

Validate a simple case class
case class Size(width: Int, height: Int)

object SizeValidator extends ModelValidator[Size] {
  override def apply(size: Size)(implicit context: Option[Context] = None) = {
    Check(size)
      .field(_.width, "width")(isGreaterThan(0) and isSmallerOrEqual(5))
      .field(_.height, "height")(isGreaterThan(0) and isSmallerOrEqual(5))
      .validate
  }
}

val size = Size(0, 6)

val result: Xor[Size] = SizeValidator(size) 
result match {
  case Valid(s) =>
    println(s"valid $s")
  case iv: Invalid =>
    iv.errors.foreach(e => println(e.message))
}

// '0' must be greater than '0'.
// '6' must be smaller or equal to '5'.
Validate values wrapped in Option
val validator: Validator[Option[Int]] = optional(isPositive[Int])
val result: Xor[Option[Int]] = validator(Some(-1))
Validate elements in a List
val validator: Validator[List[Size]] = listValidator(SizeValidator)
val result: Xor[List[Size]] = validator(List(Size(0, 0), Size(1, 0)))
Validate keys or values in a Map
// pick the values
val validator: Validator[Map[String, Size]] = mapValidator[String, Size](tuple2Value(stringContains("one")))
val result: Xor[Map[String, Size]] = validator(Map("one" -> Size(0, 0)))
// pick the keys
val validator: Validator[Map[String, Size]] = mapValidator[String, Size](tuple2Key(stringContains("one")))
val result: Xor[Map[String, Size]] = validator(Map("one" -> Size(0, 0)))

Recursive fields are supported
case class Item(name: String, items: List[Item])

object ItemValidator extends ModelValidator[Item] {
  override def apply(item: Item)(implicit parent: Option[Context]) = {
    Check(item)
      .field(_.name, "name")(stringNonEmpty)
      .field(_.items, "items")(listValidator(ItemValidator))
      .validate
  }
}

val item = Item("one", Item("two", Nil) :: Item("", Nil) :: Nil)

ItemValidator(item)
It's quick to create a custom Validator:
def stringContains(value: String) = Validator[String] { (s, context) =>
  if (s.contains(value)) Valid(s)
  else Invalid(Error(context, 'stringContains, Seq(s, value)))
}

Context is taken care of upstream, namely Check does the job when it is used to declare which fields of a class are to be validated. So you just need to perform the check for validity. If the value is valid, return Valid with the value and if not, return Invalid. Invalid requires the context (from upstream), as well as an error message key and a Seq of values which are to be used in the error message. Like String'{}' should contain '{}'.

Validate fields whose validity depends on the values of each other
case class Person(name: String, age: Int, birthday: LocalDate)

def AgeValidator = Validator[(Int, LocalDate)] {
  case ((age, birthday), context) =>
    val shouldBeAge = Period.between(birthday, LocalDate.now()).getYears
    val isValid = shouldBeAge == age
    if (isValid) Valid(age -> birthday)
    else Invalid(Error(context, 'wrongAge, Seq(age, birthday, shouldBeAge)))
}

object PersonValidator extends ModelValidator[Person] {
  override def apply(person: Person)(implicit parent: Option[Context]) = {
    Check(person)
      .field(_.name, "name")(stringNonEmpty)
      .field(p => p.age -> p.birthday, "age")(AgeValidator)
      .validate
  }
}

PersonValidator(Person("max", 30, LocalDate.of(1950, 1, 1)))

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory