package accord
- Alphabetic
- By Inheritance
- accord
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Package Members
- package combinators
Aggregates all implemented combinators for use by the DSL.
Aggregates all implemented combinators for use by the DSL. Can, though not intended to, be used directly by end-user code.
- package dsl
Provides a DSL for defining validation rules.
Provides a DSL for defining validation rules.
Overview
Accord provides a convenient DSL for defining validation rules. To define a validator over some type
T
, import this package and invoke validator[T]. You can then use the provided sample object to define various rules:scala> case class Person( name: String, age: Int ) defined class Person scala> import com.wix.accord.dsl._ // Import the validator DSL import com.wix.accord.dsl._ scala> implicit val personValidator = validator[ Person ] { p => | // Validation rules: | p.name is notEmpty | p.age should be >= 18 | } personValidator: com.wix.accord.transform.ValidationTransform.TransformedValidator[Person] = <function1>
Accord adds an implicit logical
and
relation between the rules, so all rules must apply in order for the validation to be successful. You can specify as many rules as you like.Descriptions
Each validation rule has an associated description (accessible via com.wix.accord.Violation.description). This description is automatically generated by Accord:
scala> import com.wix.accord._ import com.wix.accord._ scala> validate( Person( "", 15 ) ) res1: com.wix.accord.Result = Failure(Set(RuleViolation(,must not be empty,Some(name)), RuleViolation(15,got 15, expected 18 or more,Some(age))))
You can also explicitly provide a description with the "as" modifier:
scala> implicit val personValidator = validator[ Person ] { p => | p.name as "Full name" is notEmpty | p.age as "Age" should be >= 18 | } personValidator: com.wix.accord.transform.ValidationTransform.TransformedValidator[Person] = <function1> scala> validate( Person( "", 15 ) ) res2: com.wix.accord.Result = Failure(Set(RuleViolation(,must not be empty,Some(Full name)), RuleViolation(15,got 15, expected 18 or more,Some(Age))))
Combinators
Accord offers a built-in library of building blocks (called "combinators") that can be composed into more complex validation rules:
General-purpose
// Equality sample.field is equalTo( "value" ) sample.field is notEqualTo( "value" ) // Nullability (only applies to reference types) sample.field is aNull sample.field is notNull // Delegation sample.field is valid // Implicitly, or sample.field is valid( myOwnValidator ) // Explicitly
Primitives
// Booleans sample.booleanField is true sample.booleanField is false // Strings sample.stringField should startWith( "prefix" ) sample.stringField should endWith( "suffix" ) sample.stringField should matchRegex( "b[aeiou]t" ) // Matches "bat" and "dingbat" sample.stringField should matchRegexFully( "b[aeiou]t" ) // Matches "bat" but not "dingbat" sample.stringField should matchRegex( pattern ) // You can also use java.util.regex.Pattern sample.stringField should matchRegex( regex ) // ... or scala.util.matching.Regex sample.stringField is blank // Matches empty or whitespace-only strings sample.stringField is notBlank // You can use "must" instead of "should": sample.stringField must startWith( "prefix" ) // Strings are also "collection-like", so all collection combinators apply (see below) sample.stringField is notEmpty // Numerics (applies to any type with an instance of scala.math.Ordering in implicit search scope): sample.intField should be < 5 sample.intField should be > 5 sample.intField should be <= 5 sample.intField should be >= 5 sample.intField should be == 5 sample.intField is between( 0, 10 ) sample.intField is between( 0, 10 ).exclusive sample.intField is within( 0 to 10 ) // Inclusive sample.intField is within( 0 until 10 ) // Exclusive
Collections
// Emptiness sample.seq is empty sample.seq is notEmpty // This applies to any type that has a boolean "isEmpty" property, such as string) // The "each" modifier applies the validation to all members of a collection: sample.seq.each should be >= 10 sample.option.each should be >= 10 // Allows None or Some(15) // Size (applies to any type with an integer "size" property) // See "Numerics" above for additional operations sample.seq has size >= 8 sample.entities have size >= 8 // You can use "have" in place of "has"
Boolean Expressions
// Logical AND (not strictly required, since you can just split into separate rules) ( person.name is notEmpty ) and ( person.age should be >= 18 ) // Logical OR ( person.email is notEmpty ) or ( person.phoneNumber is notEmpty ) // You can also nest rules: ( fromJava.tags is aNull ) or ( ( fromJava.tags is notEmpty ) and ( fromJava.tags.each should matchRegexFully( "\\S+" ) ) )
- package transform
Type Members
- trait DescriptionBuilders extends LowPriorityDescriptionBuilders
- trait LowPriorityDescriptionBuilders extends AnyRef
- trait ResultBuilders extends AnyRef
Value Members
- def validate[T](x: T)(implicit validator: Validator[T]): Result
- object DescriptionBuilders extends DescriptionBuilders
- object ResultBuilders extends ResultBuilders