Skip to content

abstract class Athena::Validator::Constraints::Compound
inherits Athena::Validator::Constraints::Composite #

Allows creating a custom set of reusable constraints, representing rules to use consistently across your application.

Note

See AVD::Constraint@custom-constraints for common documentation on defining custom constraints.

Configuration#

Optional Arguments#

Note

This constraint does not support a message argument.

groups#

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

The 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 is not used by Athena::Validator, but its processing is completely up to you.

Usage#

This constraint is not used directly on its own; instead it's used to create another constraint.

# Define a compound constraint to centralize the logic to validate a password.
#
# NOTE: The constraint _MUST_ be defined within the `AVD::Constraints` namespace for implementation reasons.  This may change in the future.
class AVD::Constraints::ValidPassword < AVD::Constraints::Compound
  # Define a method that returns an array of the constraints we want to be a part of `self`.
  def constraints : Array(AVD::Constraint)
    [
      AVD::Constraints::NotBlank.new,       # Not empty/null
      AVD::Constraints::Size.new(12..),     # At least 12 characters longs
      AVD::Constraints::Regex.new(/^\d.*/), # Must start with a number
    ]
  end
end

We can then use this constraint as we would any other.

Either as an annotation

@[Assert::ValidPassword]
getter password : String
or directly.

constraint = AVD::Constraints::ValidPassword.new

Class methods#

.new(groups : Array(String) | String | Nil = nil, payload : Hash(String, String)? = nil) #

View source

Methods#

abstract #constraints : Array(AVD::Constraint) #

View source

#validated_by : AVD::ConstraintValidator.class #

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

View source