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:
{{ value }}
- The current (invalid) value.{{ choices }}
- The available choices.
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:
{{ value }}
- The current (invalid) value.{{ choices }}
- The available choices.
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:
{{ value }}
- The current (invalid) value.{{ choices }}
- The available choices.{{ limit }}
- If multiple is true, enforces that at most this many values may be selected in order to be valid.
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:
{{ value }}
- The current (invalid) value.{{ choices }}
- The available choices.{{ limit }}
- If multiple is true, enforces that no more than this many values may be selected in order to be valid.
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.crConstant 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
- #choices : Array(String | Number::Primitive | Symbol)
- #max : Number::Primitive | Nil
- #max_message : String
- #min : Number::Primitive | Nil
- #min_message : String
- #multiple? : Bool
- #multiple_message : String
-
#validated_by : AVD::ConstraintValidator.class
Returns the
AVD::ConstraintValidator.class
that should handle validatingself
.
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
Instance Method Detail
Returns the AVD::ConstraintValidator.class
that should handle validating self
.