Overview
Accord is a validation library written in and for Scala. Compared to JSR 303 and Scalaz validation it aims to provide the following:
- Composable: Because JSR 303 is annotation based, validation rules cannot be composed (annotations cannot receive other annotations as parameters). This is a real problem with some Scala features, for example
Option
s or collections. Accord's validation rules are trivially composable. - Simple: Accord provides a dead-simple story for validation rule definition by leveraging macros, as well as the validation call site (see example below).
- Self-contained: Accord is macro-based but completely self-contained, and consequently only relies on the Scala runtime and reflection libraries.
- Integrated: Other than providing its own DSL and matcher library, Accord is designed to easily integrate with the larger Scala ecosystem, and provides out-of-the-box support for Scala.js, as well as integration modules for Spring Validation, Specs2 and ScalaTest.
Accord is developed and used at and distributed under the Apache License, Version 2.0, which basically means you can use and modify it freely. Feedback, bug reports and improvements are welcome!
Example
Importing the library for use:
import com.wix.accord._
Defining a validator:
import dsl._ // Import the validator DSL
case class Person( firstName: String, lastName: String )
case class Classroom( teacher: Person, students: Seq[ Person ] )
implicit val personValidator = validator[ Person ] { p =>
p.firstName is notEmpty // The expression being validated is resolved automatically, see below
p.lastName as "last name" is notEmpty // You can also explicitly describe the expression being validated
}
implicit val classValidator = validator[ Classroom ] { c =>
c.teacher is valid // Implicitly relies on personValidator!
c.students.each is valid
c.students have size > 0
}
Running a validator:
scala> val validPerson = Person( "Wernher", "von Braun" )
validPerson: Person = Person(Wernher,von Braun)
scala> validate( validPerson )
res0: com.wix.accord.Result = Success
scala> val invalidPerson = Person( "", "No First Name" )
invalidPerson: Person = Person(,No First Name)
scala> validate( invalidPerson )
res1: com.wix.accord.Result = Failure(List(RuleViolation(,must not be empty,firstName)))
scala> val explicitDescription = Person( "No Last Name", "" )
explicitDescription: Person = Person(No Last Name,)
scala> validate( explicitDescription )
res2: com.wix.accord.Result = Failure(List(RuleViolation(,must not be empty,last name)))
scala> val invalidClassroom = Classroom( Person( "Alfred", "Aho" ), Seq.empty )
invalidClassroom: Classroom = Classroom(Person(Alfred,Aho),List())
scala> validate( invalidClassroom )
res3: com.wix.accord.Result = Failure(List(RuleViolation(List(),has size 0, expected more than 0,students)))
Getting Started
Accord version 0.5 is available on Maven Central Repository. Scala versions 2.10.3+ and 2.11.x are supported. The next milestone is 0.6-SNAPSHOT and is available from the Sonatype snapshots repository.
SBT
Simply add the accord-core
module to your build settings:
// Regular (JVM) Scala projects:
libraryDependencies += "com.wix" %% "accord-core" % "0.5"
// Scala.js projects:
libraryDependencies += "com.wix" %%% "accord-core" % "0.5"
If you want to evaluate the upcoming snapshot release, add the Sonatype snapshot repository to your resolvers; typically this means adding the following to your build.sbt
file:
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
// Regular (JVM) Scala projects:
libraryDependencies += "com.wix" %% "accord-core" % "0.6-SNAPSHOT"
// Scala.js projects:
libraryDependencies += "com.wix" %%% "accord-core" % "0.6-SNAPSHOT"
Maven
Accord is published to the Maven Central Repository, so you simply have to add the appropriate dependency to your POM:
<dependencies>
<dependency>
<groupId>com.wix</groupId>
<artifactId>accord-core_${scala.tools.version}</artifactId>
<version>0.5</version>
</dependency>
</dependencies>
If you want to evaluate the upcoming snapshot release, add the Sonatype snapshot repository and a dependency on Accord to your POM:
<repositories>
<repository>
<id>sonatype-snapshots</id>
<name>Sonatype snapshot repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.wix</groupId>
<artifactId>accord-core_${scala.tools.version}</artifactId>
<version>0.6-SNAPSHOT</version>
</dependency>
</dependencies>