docs

stand-alone validation type

License

License

GroupId

GroupId

de.knutwalker
ArtifactId

ArtifactId

docs_2.11
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

docs
stand-alone validation type
Project URL

Project URL

https://github.com/knutwalker/validation/
Project Organization

Project Organization

de.knutwalker
Source Code Management

Source Code Management

https://github.com/knutwalker/validation/

Download docs_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/de.knutwalker/docs_2.11/ -->
<dependency>
    <groupId>de.knutwalker</groupId>
    <artifactId>docs_2.11</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/de.knutwalker/docs_2.11/
implementation 'de.knutwalker:docs_2.11:0.2.0'
// https://jarcasting.com/artifacts/de.knutwalker/docs_2.11/
implementation ("de.knutwalker:docs_2.11:0.2.0")
'de.knutwalker:docs_2.11:jar:0.2.0'
<dependency org="de.knutwalker" name="docs_2.11" rev="0.2.0">
  <artifact name="docs_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='de.knutwalker', module='docs_2.11', version='0.2.0')
)
libraryDependencies += "de.knutwalker" % "docs_2.11" % "0.2.0"
[de.knutwalker/docs_2.11 "0.2.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.6
de.knutwalker » tests_2.11 jar 0.2.0

Project Modules

There are no modules declared in this project.

Travis CI Coverage Status Maven Apache License

Validation

A standalone Validation data type, called Result. (see How do I error handle thee? for more on the general idea of a Validation)

The Result type is very similar to scalaz.Validation, cats.data.Validated, or org.scalactic.Or with the following differences:

  • no dependencies besides scala and no additional baggage

that is no Monads, Applicative Functors, Arrows, or Categories. The only thing that is provided besides Result is a NonEmptyVector which is similar to a regular Vector, only that there will always be at least one element in it.

The main incentive is, that Result can be used in places where a full-fledged dependency on scalaz and co. is undesired, e.g. in projects with many people that are unfamiliar with or intimidated by scalaz.

  • explicit symbolic and unsafe operators

By default, all operations are safe, meaning they are referentially transparent if their input is. Unsafe operations (foreach, get) can be enabled by importing validation.Result.unsafe._

All methods are named aptly (or so I hope) and with ascii characters. Symbolic names (such as | for getOrElse or |@| for and) can be enabled by importing validation.Result.symbolic._

  • implied underlying Vector to accumulate data

Scalaz differentiates between Validation[E, A] and type ValidationNel[E, A] = Validation[NonEmptyList[E], A] where as Result[E, A] has an implied NonEmptyVector accumulating the E. It behaves mostly as a permanent (imaginary) ResultNev[E, A].

What Validation is not

This library is not an implementation of a validation framework, just a general data structure.

Installing

// scala 2.11.x
libraryDependencies += "de.knutwalker" %% "validation" % "0.2.0"

// scala 2.12.x
libraryDependencies += "de.knutwalker" %% "validation" % "0.3.0"

Usage

import validation._

type Validated[A] = Result[String, A]

case class Person(name: String, age: Int)

def parseName(s: String): Validated[String] =
  if (s.trim.nonEmpty) Result.valid(s.trim)
  else Result.invalid(s"invalid name: '$s'")

def parseAge(s: String): Validated[Int] =
  Result.catching[NumberFormatException]
    .using(_.getMessage)
    .run(s.trim.toInt)
    .filter(_ >= 0, s"invalid age: '$s'")

def parsePerson(name: String, age: String): Validated[Person] =
   (parseName(name) and parseAge(age)) apply Person

parsePerson("Bernd", "42")
// res0: Validated[Person] = Valid(Person(Bernd,42))

parsePerson("Bernd", "fortytwo")
// res1: Validated[Person] = Invalid(For input string: "fortytwo")

parsePerson("Bernd", "-1337")
// res2: Validated[Person] = Invalid(invalid age: '-1337')

parsePerson("", "")
// res3: Validated[Person] = Invalids(NonEmptyVector(invalid name: '',For input string: ""))

Versions

Version
0.2.0