Skip to content

class Athena::Validator::Constraints::Choice
inherits Athena::Validator::Constraint #

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 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.

Constants#

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#

.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

Methods#

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

View source

#max : Number::Primitive | ::Nil#

View source

#max_message : String#

View source

#min : Number::Primitive | ::Nil#

View source

#min_message : String#

View source

#multiple? : Bool#

View source

#multiple_message : String#

View source

#validated_by : AVD::ConstraintValidator.class#

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

View source