class Athena::Validator::Constraints::AtLeastOneOf

Overview

Validates that a value satisfies at least one of the provided constraints. Validation stops as soon as one constraint is satisfied.

Configuration

Required Arguments

constraints

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

The AVD::Constraint(s) from which at least one of has to be satisfied in order for the validation to succeed.

Optional Arguments

include_internal_messages

Type: Bool Default: true

If the validation failed message should include the list of messages for the internal constraints. See the message argument for an example.

message_collection

Type: String Default: Each element of this collection should satisfy its own set of constraints.

The message that will be shown if validation fails and the internal constraint is an AVD::Constraints::All. See the message argument for an example.

message

Type: String Default: This value should satisfy at least one of the following constraints:

The intro that will be shown if validation fails. By default, it'll be followed by the list of messages from the internal constraints; configurable via the include_internal_messages argument.

For example, if the grades property in the example below fails to validate, the message will be:

This value should satisfy at least one of the following constraints: [1] This value is too short. It should have 3 items or more. [2] Each element of this collection should satisfy its own set of constraints.

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

class Example
  include AVD::Validatable

  def initialize(@password : String, @grades : Array(Int32)); end

  # Asserts the password contains an `#` or is at least 10 characters long.
  @[Assert::AtLeastOneOf([
    @[Assert::Regex(/#/)],
    @[Assert::Size(10..)],
  ])]
  getter password : String

  # Asserts the `grades` array contains at least 3 elements or
  # that each element is greater than or equal to 5.
  @[Assert::AtLeastOneOf([
    @[Assert::Size(3..)],
    @[Assert::All([
      @[Assert::GreaterThanOrEqual(5)],
    ])],
  ])]
  getter grades : Array(Int32)
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/at_least_one_of.cr

Constant Summary

AT_LEAST_ONE_OF_ERROR = "811994eb-b634-42f5-ae98-13eec66481b6"
DEFAULT_ERROR_MESSAGE = "This value should satisfy at least one of the following constraints:"

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) 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)? 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) 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, include_internal_messages : Bool = true, message_collection : String = "Each element of this collection should satisfy its own set of constraints.", message : String = "This value should satisfy at least one of the following constraints:", groups : Array(String) | String | Nil = nil, payload : Hash(String, String)? = nil) #

[View source]

Instance Method Detail

def include_internal_messages : Bool #

[View source]
def message_collection : String #

[View source]
def validated_by : AVD::ConstraintValidator.class #

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


[View source]