abstract class Athena::Validator::Constraints::Compound

Overview

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][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

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

Defined in:

constraints/compound.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(groups : Array(String) | String | Nil = nil, payload : Hash(String, String) | Nil = nil) #

[View source]

Instance Method Detail

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

[View source]
def validated_by : AVD::ConstraintValidator.class #
Description copied from class Athena::Validator::Constraint

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


[View source]