class Athena::Validator::Constraints::Sequentially

Overview

Validates a value against a collection of constraints, stopping once the first violation is raised.

Configuration

Required Arguments

constraints

Type: Array(AVD::Constraint) | AVD::Constraint

The AVD::Constraint(s) that are to be applied sequentially.

Optional Arguments

NOTE This constraint does not support a message argument.

groups

Type: Array(String) | String | Nil Default: nil

The [validation groups][Athena::Validator::Constraint--validation-groups] this constraint belongs to. AVD::Constraint::DEFAULT_GROUP is assumed if nil.

payload

Type: Hash(String, String)? Default: nil

Any arbitrary domain-specific data that should be stored with this constraint. The [payload][Athena::Validator::Constraint--payload] is not used by Athena::Validator, but its processing is completely up to you.

Usage

Suppose you have an object with a address property which should meet the following criteria:

If you were to apply all of these constraints to the address property, you may run into some problems. For example, multiple violations may be added for the same property, or you may perform a useless and heavy external call to geolocalize the address when it is not in a proper format.

To solve this we can validate these constraints sequentially.

class Location
  include AVD::Validatable

  PATTERN = /some_pattern/

  def initialize(@address : String); end

  @[Assert::Sequentially([
    @[Assert::NotBlank],
    @[Assert::Size(10..)],
    @[Assert::Regex(Location::PATTERN)],
    @[Assert::CustomGeolocalizationConstraint],
  ])]
  getter address : String
end

NOTE The annotation approach only supports two levels of nested annotations. Manually wire up the constraint via code if you require more than that.

Defined in:

constraints/sequentially.cr

Constructors

Instance Method Summary

Instance methods inherited from class Athena::Validator::Constraints::Composite

add_implicit_group(group : String) : Nil add_implicit_group, constraints : Array(AVD::Constraint) constraints

Constructor methods inherited from class Athena::Validator::Constraints::Composite

new(constraints : Array(AVD::Constraint) | AVD::Constraint, message : String, groups : Array(String) | String | Nil = nil, payload : Hash(String, String) | Nil = nil) new

Instance methods inherited from class Athena::Validator::Constraint

add_implicit_group(group : String) : Nil add_implicit_group, groups : Array(String) groups, groups=(groups : Array(String)) groups=, message : String message, payload : Hash(String, String) | Nil payload, validated_by : AVD::ConstraintValidator.class validated_by

Constructor methods inherited from class Athena::Validator::Constraint

new(message : String, groups : Array(String) | String | Nil = nil, payload : Hash(String, String) | Nil = nil) new

Class methods inherited from class Athena::Validator::Constraint

error_name(error_code : String) : String error_name

Constructor Detail

def self.new(constraints : Array(AVD::Constraint) | AVD::Constraint, groups : Array(String) | String | Nil = nil, payload : Hash(String, String) | Nil = nil) #

[View source]

Instance Method Detail

def validated_by : AVD::ConstraintValidator.class #

Returns the AVD::ConstraintValidator.class that should handle validating self.


[View source]