com.wix.accord

dsl

package dsl

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

// 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+" ) )
)
Linear Supertypes
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. dsl
  2. BooleanOps
  3. OrderingOps
  4. GenericOps
  5. CollectionOps
  6. StringOps
  7. AnyRef
  8. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. trait BooleanOps extends AnyRef

    Provides a DSL for booleans.

  2. class CollectionDslContext[Inner, Outer] extends ContextTransformer[Inner, Outer]

  3. trait CollectionOps extends AnyRef

    Provides a DSL for collection-like objects.

  4. trait ContextTransformer[Inner, Outer] extends AnyRef

  5. implicit class Contextualizer[U] extends SimpleDslContext[U]

    Wraps expressions under validation with the Accord DSL.

  6. implicit class Descriptor[U] extends AnyRef

    Enables explicitly describing expression under validation.

  7. trait DslContext[Inner, Outer] extends AnyRef

  8. trait FallbackIndexDescriptions extends AnyRef

  9. trait GenericOps extends AnyRef

    Provides a DSL for untyped validators.

  10. type HasSize = Any { def size: Int }

    A structural type representing any object that has a size.

    A structural type representing any object that has a size.

    Definition Classes
    CollectionOps
  11. trait IndexedDescriptions[T] extends AnyRef

  12. trait OrderingOps extends AnyRef

    Provides a DSL for types with a corresponding implmentation of scala.math.Ordering.

  13. trait SimpleDslContext[U] extends DslContext[U, U] with ContextTransformer[U, U]

  14. trait StringOps extends AnyRef

    Provides a DSL for string validators.

  15. implicit class ValidatorBooleanOps[T] extends AnyRef

    Extends validators with useful helpers.

Value Members

  1. def <[T](other: T)(implicit arg0: Ordering[T]): combinators.LesserThan[T]

    Generates a validator that succeeds only if the provided value is less than the specified bound.

    Generates a validator that succeeds only if the provided value is less than the specified bound.

    Definition Classes
    OrderingOps
  2. def <=[T](other: T)(implicit arg0: Ordering[T]): combinators.LesserThanOrEqual[T]

    Generates a validator that succeeds if the provided value is less than or equal to the specified bound.

    Generates a validator that succeeds if the provided value is less than or equal to the specified bound.

    Definition Classes
    OrderingOps
  3. def ==[T](other: T)(implicit arg0: Ordering[T]): combinators.EquivalentTo[T]

    Generates a validator that succeeds if the provided value is exactly equal to the specified value.

    Generates a validator that succeeds if the provided value is exactly equal to the specified value.

    Definition Classes
    OrderingOps
  4. def >[T](other: T)(implicit arg0: Ordering[T]): combinators.GreaterThan[T]

    Generates a validator that succeeds only if the provided value is greater than the specified bound.

    Generates a validator that succeeds only if the provided value is greater than the specified bound.

    Definition Classes
    OrderingOps
  5. def >=[T](other: T)(implicit arg0: Ordering[T]): combinators.GreaterThanOrEqual[T]

    Generates a validator that succeeds if the provided value is greater than or equal to the specified bound.

    Generates a validator that succeeds if the provided value is greater than or equal to the specified bound.

    Definition Classes
    OrderingOps
  6. object Aggregates

  7. object Compatibility

    Stubs for forwards compatibility with 2.

  8. object IndexedDescriptions extends FallbackIndexDescriptions

  9. def aNull: Validator[AnyRef]

    Specifies a validator that succeeds only if the validation expression is null.

    Specifies a validator that succeeds only if the validation expression is null.

    Definition Classes
    GenericOps
  10. def anInstanceOf[T <: AnyRef](implicit arg0: ClassTag[T]): Validator[AnyRef]

    Specifies a validator that succeeds only if the validation expression evaluates to the specified type.

    Specifies a validator that succeeds only if the validation expression evaluates to the specified type. Respects nulls.

    Definition Classes
    GenericOps
  11. val be: OrderingOps

    Provides the "be" keyword.

    Provides the "be" keyword.

    Makes all members of OrderingOps accessible via the following syntax:

    p.age should be > 5`
    See also

    com.wix.accord.dsl.OrderingOps

  12. def between[T](lowerBound: T, upperBound: T)(implicit arg0: Ordering[T]): combinators.InRangeInclusive[T]

    Generates a validator that succeeds if the provided value is between (inclusive) the specified bounds.

    Generates a validator that succeeds if the provided value is between (inclusive) the specified bounds. The method exclusive is provided to specify an exclusive upper bound.

    Definition Classes
    OrderingOps
  13. implicit def booleanToBooleanValidator(b: Boolean): Validator[Boolean]

    An implicit conversion from boolean to a respective IsTrue/IsFalse instance; this enables syntax such as customer.emailOptIn is true.

    An implicit conversion from boolean to a respective IsTrue/IsFalse instance; this enables syntax such as customer.emailOptIn is true.

    Definition Classes
    BooleanOps
  14. def distinct: combinators.Distinct.type

    Specifies a validator that fails on collections with duplicate elements.

    Specifies a validator that fails on collections with duplicate elements.

    Definition Classes
    CollectionOps
  15. def empty[T <: AnyRef](implicit ev: (T) ⇒ combinators.HasEmpty): Validator[T]

    Specifies a validator that succeeds on empty instances; the object under validation must implement def isEmpty: Boolean (see com.wix.accord.combinators.HasEmpty).

    Specifies a validator that succeeds on empty instances; the object under validation must implement def isEmpty: Boolean (see com.wix.accord.combinators.HasEmpty).

    Definition Classes
    CollectionOps
  16. def endWith(suffix: String): Validator[String]

    Specifies a validator that operates on strings and succeeds only if the validation expression ends with the specified suffix.

    Specifies a validator that operates on strings and succeeds only if the validation expression ends with the specified suffix.

    Definition Classes
    StringOps
  17. def equalTo[T](to: T): Validator[T]

    Specifies a validator that succeeds only if the validation expression is equal to the specified value.

    Specifies a validator that succeeds only if the validation expression is equal to the specified value. Respects nulls an performs equality checks via java.lang.Object.equals.

    Definition Classes
    GenericOps
  18. implicit def genericTraversableOnce2HasSize[T](gto: T)(implicit ev: (T) ⇒ GenTraversableOnce[_]): HasSize

    An implicit conversion to enable any collection-like object (e.

    An implicit conversion to enable any collection-like object (e.g. strings, options) to be handled by com.wix.accord.dsl.CollectionDslContext.

    java.lang.String does not directly implement size (in practice it is implemented in scala.collection.IndexedSeqOptimized via an implicit conversion and an inheritance stack), and this is a case where the Scala compiler does not always infer structural types correctly. By requiring a view bound from T to scala.collection.GenTraversableOnce we can force any collection-like structure to conform to the structural type com.wix.accord.dsl.CollectionOps.HasSize, and by requiring a view bound from T to com.wix.accord.dsl.CollectionOps.HasSize at the call site (via com.wix.accord.dsl.CollectionDslContext) we additionally support any class that directly conforms to the structural type as well.

    T

    The type that conforms, directly or implicitly, to com.wix.accord.dsl.CollectionOps.HasSize.

    gto

    An object that is, or is implicitly convertible to, scala.collection.GenTraversableOnce.

    returns

    The specified object, strictly-typed as com.wix.accord.dsl.CollectionOps.HasSize.

    Definition Classes
    CollectionOps
  19. def in[T](items: T*): Validator[T]

    Specifies a validator that succeeds only if the object exists in the specified set of items.

    Specifies a validator that succeeds only if the object exists in the specified set of items.

    Definition Classes
    CollectionOps
  20. def in[T](set: Set[T]): Validator[T]

    Specifies a validator that succeeds only if the object exists in the specified set.

    Specifies a validator that succeeds only if the object exists in the specified set.

    Definition Classes
    CollectionOps
  21. def matchRegex(pattern: Pattern): Validator[String]

    Specifies a validator that operates on strings and succeeds only if the validation expression matches the specified regular expression.

    Specifies a validator that operates on strings and succeeds only if the validation expression matches the specified regular expression.

    Definition Classes
    StringOps
  22. def matchRegex(regex: Regex): Validator[String]

    Specifies a validator that operates on strings and succeeds only if the validation expression matches the specified regular expression.

    Specifies a validator that operates on strings and succeeds only if the validation expression matches the specified regular expression.

    Definition Classes
    StringOps
  23. def matchRegex(regex: String): Validator[String]

    Specifies a validator that operates on strings and succeeds only if the validation expression matches the specified regular expression.

    Specifies a validator that operates on strings and succeeds only if the validation expression matches the specified regular expression.

    Definition Classes
    StringOps
  24. def matchRegexFully(pattern: Pattern): Validator[String]

    Specifies a validator that operates on strings and succeeds only if the validation expression **fully** matches the specified regular expression.

    Specifies a validator that operates on strings and succeeds only if the validation expression **fully** matches the specified regular expression. See com.wix.accord.combinators.StringCombinators.MatchesRegex for a full explanation of the difference between partial and full matching.

    Definition Classes
    StringOps
  25. def matchRegexFully(regex: Regex): Validator[String]

    Specifies a validator that operates on strings and succeeds only if the validation expression **fully** matches the specified regular expression.

    Specifies a validator that operates on strings and succeeds only if the validation expression **fully** matches the specified regular expression. See com.wix.accord.combinators.StringCombinators.MatchesRegex for a full explanation of the difference between partial and full matching.

    Definition Classes
    StringOps
  26. def matchRegexFully(regex: String): Validator[String]

    Specifies a validator that operates on strings and succeeds only if the validation expression **fully** matches the specified regular expression.

    Specifies a validator that operates on strings and succeeds only if the validation expression **fully** matches the specified regular expression. See com.wix.accord.combinators.StringCombinators.MatchesRegex for a full explanation of the difference between partial and full matching.

    Definition Classes
    StringOps
  27. def notAnInstanceOf[T <: AnyRef](implicit arg0: ClassTag[T]): Validator[AnyRef]

    Specifies a validator that fails only if the validation expression evaluates to the specified type.

    Specifies a validator that fails only if the validation expression evaluates to the specified type. Respects nulls.

    Definition Classes
    GenericOps
  28. def notEmpty[T <: AnyRef](implicit ev: (T) ⇒ combinators.HasEmpty): Validator[T]

    Specifies a validator that fails on empty instances; the object under validation must implement def isEmpty: Boolean (see com.wix.accord.combinators.HasEmpty).

    Specifies a validator that fails on empty instances; the object under validation must implement def isEmpty: Boolean (see com.wix.accord.combinators.HasEmpty).

    Definition Classes
    CollectionOps
  29. def notEqualTo[T](to: T): Validator[T]

    Specifies a validator that succeeds only if the validation expression is not equal to the specified value.

    Specifies a validator that succeeds only if the validation expression is not equal to the specified value. Respects nulls an performs equality checks via java.lang.Object.equals.

    Definition Classes
    GenericOps
  30. def notNull: Validator[AnyRef]

    Specifies a validator that succeeds only if the validation expression is not null.

    Specifies a validator that succeeds only if the validation expression is not null.

    Definition Classes
    GenericOps
  31. def prefix: String

    Attributes
    protected
    Definition Classes
    CollectionOps
  32. val size: OrderingOps

    Provides access to size-based validators (where the object under validation must implement def size: Int, see com.wix.accord.dsl.CollectionOps.HasSize).

    Provides access to size-based validators (where the object under validation must implement def size: Int, see com.wix.accord.dsl.CollectionOps.HasSize). Enables syntax such as c.students has size > 0.

    Definition Classes
    CollectionOps
  33. def snippet: String

    Attributes
    protected
    Definition Classes
    OrderingOps
  34. def startWith(prefix: String): Validator[String]

    Specifies a validator that operates on strings and succeeds only if the validation expression starts with the specified prefix.

    Specifies a validator that operates on strings and succeeds only if the validation expression starts with the specified prefix.

    Definition Classes
    StringOps
  35. def valid[T](implicit validator: Validator[T]): Validator[T]

    Delegates validation to a pre-defined validation rule, which is encoded as an implicit com.wix.accord.Validator in scope.

    Delegates validation to a pre-defined validation rule, which is encoded as an implicit com.wix.accord.Validator in scope. Enables composition of validation rules, as in:

    case class Address( address1: String, address2: String, city: String, ... ) case class Item( sku: String, count: Int, ... ) case class Shipment( items: Seq[ Item ], address: Location, ... )

    implicit val addressValidator = validator[ Address ] { ... } implicit val itemValidator = validator[ Item ] { ... }

    implicit val shipmentValidator = validator[ Shipment ] { shipment => shipment.address is valid // Implicitly uses addressValidator shipment.items.each is valid // Implicitly uses itemValidator }

    Definition Classes
    GenericOps
  36. def validator[T](v: (T) ⇒ Unit): TransformedValidator[T]

    Defines a new validator for the specified type.

    Defines a new validator for the specified type.

    This function takes a block of validation rules (encoded as a "thunk"), and rewrites them as a full-fledged instance of Validator. A full example and details of the available syntax can be found in the package documentation.

    Details of the code transformation are documented in ValidationTransform (fair warning: this description is quite low-level and fairly involved, and will likely not be of interest to most users).

    T

    The type under validation.

    v

    The validation code block; may contain any combination of validation statements.

    returns

    The validation code block rewritten as a com.wix.accord.Validator for the specified type T.

    Annotations
    @macroImpl( ... )
    See also

    com.wix.accord.dsl

  37. def within[T](range: NumericRange[T])(implicit arg0: Ordering[T]): combinators.InRange[T]

    Generates a validator that succeeds if the provided value is within the specified range.

    Generates a validator that succeeds if the provided value is within the specified range.

    Definition Classes
    OrderingOps
  38. def within(range: Range): combinators.InRange[Int]

    Generates a validator that succeeds if the provided value is within the specified range.

    Generates a validator that succeeds if the provided value is within the specified range.

    Definition Classes
    OrderingOps

Inherited from BooleanOps

Inherited from OrderingOps

Inherited from GenericOps

Inherited from CollectionOps

Inherited from StringOps

Inherited from AnyRef

Inherited from Any

Ungrouped