class Athena::Validator::Constraints::Choice

Overview

Validates that a value is one of a given set of valid choices; can also be used to validate that each item in a collection is one of those valid values.

Configuration

Required Arguments

choices

Type: Array(String | Number::Primitive | Symbol)

The choices that are considered valid.

Optional Arguments

message

Type: String Default: This value is not a valid choice.

The message that will be shown if the value is not a valid choice and multiple is false.

Placeholders

The following placeholders can be used in this message:

multiple_message

Type: String Default: One or more of the given values is invalid.

The message that will be shown if one of the values is not a valid choice and multiple is true.

Placeholders

The following placeholders can be used in this message:

min_message

Type: String Default: You must select at least {{ limit }} choice.|You must select at least {{ limit }} choices.

The message that will be shown if too few choices are chosen as per the range option.

Placeholders

The following placeholders can be used in this message:

max_message

Type: String Default: You must select at most {{ limit }} choice.|You must select at most {{ limit }} choices.

The message that will be shown if too many choices are chosen as per the range option.

Placeholders

The following placeholders can be used in this message:

range

Type: ::Range? Default: nil

If multiple is true, is used to define the "range" of how many choices must be valid for the value to be considered valid. For example, if set to (3..), but there are only 2 valid items in the input enumerable then validation will fail.

Beginless/endless ranges can be used to define only a lower/upper bound.

multiple

Type: Bool Default: false

If true, the input value is expected to be an Enumerable instead of a single scalar value. The constraint will check each item in the enumerable is valid choice.

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.

Defined in:

constraints/choice.cr

Constant Summary

NO_SUCH_CHOICE_ERROR = "c7398ea5-e787-4ee9-9fca-5f2c130614d6"
TOO_FEW_ERROR = "3573357d-c9a8-4633-a742-c001086fd5aa"
TOO_MANY_ERROR = "91d0d22b-a693-4b9c-8b41-bc6392cf89f4"

Constructors

Instance Method Summary

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(choices : Array(String | Number::Primitive | Symbol), message : String = "This value is not a valid choice.", multiple_message : String = "One or more of the given values is invalid.", min_message : String = "You must select at least {{ limit }} choice.|You must select at least {{ limit }} choices.", max_message : String = "You must select at most {{ limit }} choice.|You must select at most {{ limit }} choices.", multiple : Bool = false, range : ::Range | Nil = nil, groups : Array(String) | String | Nil = nil, payload : Hash(String, String) | Nil = nil) #

[View source]

Instance Method Detail

def choices : Array(String | Number::Primitive | Symbol) #

[View source]
def max : Number::Primitive | Nil #

[View source]
def max_message : String #

[View source]
def min : Number::Primitive | Nil #

[View source]
def min_message : String #

[View source]
def multiple? : Bool #

[View source]
def multiple_message : String #

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

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


[View source]