class Assert::Assertions::Choice(PropertyType, ChoicesType)
Overview
Validates a property is a valid choice.
Optional Arguments
- min_matches - Must select at least min_matches to be valid.
- min_message - Message to display if too few choices are selected.
- max_matches - Must select at most max_matches to be valid.
- max_message - Message to display if too many choices are selected.
- multiple_message - Message to display if one or more values in actual are not in choices.
Example
class Example
include Assert
def initialize; end
@[Assert::Choice(choices: ["Active", "Inactive", "Pending"])]
property status : String = "Inactive"
@[Assert::Choice(choices: [2, 4, 6], multiple_message: "One ore more value is invalid")]
property fav_numbers : Array(Int32) = [2, 4, 6]
@[Assert::Choice(choices: ['a', 'b', 'c'], min_matches: 2, min_message: "You must have at least 2 choices")]
property fav_letters_min : Array(Char) = ['a', 'c']
@[Assert::Choice(choices: ['a', 'b', 'c'], max_matches: 2, max_message: "You must have at most 2 choices")]
property fav_letters_max : Array(Char) = ['a']
end
Example.new.valid? # => true
NOTE The generic ChoicesType
represents the type of choices.
Defined in:
assertions/choice.crConstructors
Instance Method Summary
-
#default_message_template : String
Returns the default
#message_template
to use if no message is provided. -
#message : String
The message to display if
self
is not valid. -
#valid? : Bool
Returns
true
if a property satisfiesself
, otherwisefalse
.
Instance methods inherited from class Assert::Assertions::Assertion
default_message_template : String
default_message_template,
groups : Array(String)
groups,
message : String
message,
message_template : String
message_template,
property_name : String
property_name,
valid? : Bool
valid?
Constructor methods inherited from class Assert::Assertions::Assertion
new(property_name : String, message : String | Nil = nil, groups : Array(String) | Nil = nil)
new
Constructor Detail
def self.new(property_name : String, actual : PropertyType, choices : ChoicesType, min_matches : Int32 | Nil = nil, max_matches : Int32 | Nil = nil, min_message : String | Nil = nil, max_message : String | Nil = nil, multiple_message : String | Nil = nil, message : String | Nil = nil, groups : Array(String) | Nil = nil)
#
Instance Method Detail
def default_message_template : String
#
Returns the default #message_template
to use if no message is provided.
def message : String
#
The message to display if self
is not valid.
NOTE This method is defined automatically, and is just present for documentation purposes.