Packages

  • package root
    Definition Classes
    root
  • package com
    Definition Classes
    root
  • package wix
    Definition Classes
    com
  • package accord
    Definition Classes
    wix
  • 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
  • Aggregates
  • BooleanOps
  • CollectionDslContext
  • CollectionOps
  • ContextTransformer
  • Contextualizer
  • Descriptor
  • DslContext
  • FallbackIndexDescriptions
  • GenericOps
  • IndexedDescriptions
  • OrderingOps
  • SimpleDslContext
  • StringOps
  • ValidatorBooleanOps
c

com.wix.accord.dsl

Descriptor

implicit class Descriptor[U] extends AnyRef

Enables explicitly describing expression under validation.

After the macro transform, the resulting validator will use the specified description to render violations. See the as method for full example.

U

The type of the provided expression.

See also

com.wix.accord.dsl

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

Instance Constructors

  1. new Descriptor(value: U)

    value

    The value to wrap with an explicit description.

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. def as(description: String): U

    Tags the specified validation expression with an explicit description.

    Tags the specified validation expression with an explicit description. Enables syntax such as: p.firstName as "first name" is notEmpty; violations for this validation rule will be rendered with the specified expression (instead of the implicit rule), for example:

    --- TODO --- update example!

    scala> case class Person( firstName: String, lastName: String )
    defined class Person
    
    scala> implicit val personValidator = validator[ Person ] { p => p.firstName as "first name" is notEmpty }
    personValidator: com.wix.accord.combinators.And[Person] = <function1>
    
    scala> validate( Person( "", "last" ) )
    res0: com.wix.accord.Result = Failure(List(Violation(first name must not be empty,)))

    Without the explicit description, the violation would read "firstName must not be empty".

    description

    The description to use for the expression in case of violations.

    returns

    The validation expression tagged with the explicit description.

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

Inherited from AnyRef

Inherited from Any

Ungrouped