package accord
The entry-point to the Accord library.
Overview
An Accord validator is a typeclass, which adds data validation rules over an existing
domain model. The api
module deals with the usage site; details on how to define validators can be found
in the core
module.
To use a validator, simply import this package, make sure the validator is in scope and use the validate function:
scala> import com.wix.accord._ import com.wix.accord._ scala> import MyDomain._ import MyDomain._ scala> val person = Person( name = "Niklaus", surname = "Wirth", age = 81 ) person: MyDomain.Person = Person(Niklaus,Wirth,81) scala> validate( person ) res0: com.wix.accord.Result = Success
See Result, Success and Failure for details of the result model.
- Alphabetic
- By Inheritance
- accord
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
- class BaseValidator[T] extends Validator[T]
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" )
- T
The object type this validator operates on.
- case class Failure(violations: Set[Violation]) extends Result with Product with Serializable
An object representing a failed validation result.
An object representing a failed validation result.
- violations
The violations that caused the validation to fail.
- case class GroupViolation(value: Any, constraint: String, children: Set[Violation], path: Path = Path.empty) extends Violation with Product with Serializable
Describes the violation of a group of constraints.
Describes the violation of a group of constraints. For example, the
Or
combinator found in the built-in combinator library produces a group violation when all of its predicates fail.- value
The value of the object which failed validation.
- constraint
A textual description of the constraint being violated (for example, "doesn't meet any of the requirements").
- children
The set of violations contained within the group.
- path
The path to the object under validation.
- class MaybeNullSafeValidator[T] extends Validator[T]
A base validator implementation that, depends on the specified type's nullability, checks for null values prior to executing the specified test.
A base validator implementation that, depends on the specified type's nullability, checks for null values prior to executing the specified test.
- T
The object type this validator operates on.
- class NullSafeValidator[T <: AnyRef] extends BaseValidator[T]
An extension to com.wix.accord.BaseValidator that transparently fails on nulls.
An extension to com.wix.accord.BaseValidator that transparently fails on nulls.
- T
The object type this validator operates on.
- trait Nullability[T] extends AnyRef
A type class that indicates whether or not a type is nullable.
A type class that indicates whether or not a type is nullable.
- T
The type for which nullability is indicated.
- sealed trait Result extends AnyRef
A base trait for validation results.
A base trait for validation results.
- case class RuleViolation(value: Any, constraint: String, path: Path = Path.empty) extends Violation with Product with Serializable
Describes a simple validation rule violation (i.e.
Describes a simple validation rule violation (i.e. one without hierarchy). Most built-in combinators emit this type of violation.
- value
The value of the object which failed the validation rule.
- constraint
A textual description of the constraint being violated (for example, "must not be empty").
- path
The path to the object under validation.
- trait Validator[-T] extends (T) ⇒ Result
A validator over some type
T
.A validator over some type
T
.Overview
An Accord validator is a function
T => Result
, whereT
is the type of the object under validation and Result is an instance of com.wix.accord.Result.Implementation note: While theoretically a validator can be defined as a type alias, in practice this doesn't allow to specify an error message when it's implicitly missing at the call site.
- T
The object type this validator operates on.
- Annotations
- @implicitNotFound("A validator for type ${T} not found. Did you forget to import an implicit validator for " +
"this type? (If you own the code, you may want to move the validator to the companion object " +
"for ${T} so it's automatically included in the implicit search scope).")
- sealed trait Violation extends AnyRef
A base trait for all violation types.
- trait ViolationBuilder extends AnyRef
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", ... )
Value Members
- def validate[T](x: T)(implicit validator: Validator[T]): Result
Validates the specified object and returns a validation com.wix.accord.Result.
Validates the specified object and returns a validation com.wix.accord.Result. An implicit com.wix.accord.Validator must be in scope for this call to succeed.
- T
The type of the object to validate.
- x
The object to validate.
- validator
A validator for objects of type
T
.- returns
A com.wix.accord.Result indicating success or failure of the validation.
- object Descriptions
- object Nullability
- object Success extends Result with Product with Serializable
An object representing a successful validation result.
- object Validator
A companion object mostly responsible for allowing null-safe validation of boxed Java primitive types.
- object ViolationBuilder extends ViolationBuilder
Deprecated Value Members
- object Implicits
Provides alternative syntax for validation.
Provides alternative syntax for validation. Instead of having to explicitly call com.wix.accord.validate, an object can be validated by calling
instanceUnderTest.validate
. This is strictly an aesthetic preference, there are no differences in implementation or execution.- Annotations
- @deprecated
- Deprecated
(Since version 0.7) Will be removed in subsequent releases; you can reimplement this easily if needed.