Packages

  • package root
    Definition Classes
    root
  • package com
    Definition Classes
    root
  • package wix
    Definition Classes
    com
  • package accord
    Definition Classes
    wix
  • 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.

    Definition Classes
    accord
  • 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+" ) )
    )
    Definition Classes
    accord
  • package transform
    Definition Classes
    accord
  • DescriptionBuilders
  • LowPriorityDescriptionBuilders
  • ResultBuilders

trait ResultBuilders extends AnyRef

Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ResultBuilders
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. implicit class ResultValueOps extends AnyRef
  2. implicit class ViolationValueOps extends AnyRef

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  12. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  13. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  14. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  15. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  16. def toString(): String
    Definition Classes
    AnyRef → Any
  17. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  18. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped