Simplifies base validator implementation.
Simplifies base validator implementation. Validators typically consist of an assertion/test and a resulting violation; this implementation takes two functions that describe this behavior and wires the appropriate logic. For example:
class IsNull extends BaseValidator[ AnyRef ]( _ == null, _ -> "is not a null" )
The object type this validator operates on.
An extension to com.wix.accord.BaseValidator that transparently fails on nulls.
An extension to com.wix.accord.BaseValidator that transparently fails on nulls.
The object type this validator operates on.
Provides a convenience DSL for generating violations:
Provides a convenience DSL for generating violations:
- Rule violations can be created by specifying a value and constraint message as a tuple, for example:
v -> "must not be empty"
- Group violations can be created by extending the above to include children, as in:
v -> "does not match any of the rules" -> Seq( v.firstName -> "first name must be empty", ... )
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.
Provides a DSL for defining validation rules.
Provides a DSL for defining validation rules. For example:
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 p.lastName is notEmpty }
implicit val classValidator = validator[ Classroom ] { c =>
c.teacher is valid // Implicitly relies on personValidator!
c.students.each is valid
c.students have size > 0
}
These validators can later be executed via com.wix.accord.validate. A macro transforms each
validation block into a chain of validation rules at compile-time, and annotates accesses to getters
so that violation messages are automatically generated; for instance, the rule p.firstName is notEmpty
will generate the violation message "firstName must not be empty" automatically.