Skip to content

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

Validates that the #size of an Indexable value is between some minimum and maximum.

class User
  include AVD::Validatable

  def initialize(@emails : Array(String)); end

  @[Assert::Count(1..5)]
  property emails : Array(String)
end

Configuration#

Required Arguments#

range#

Type: ::Range

The ::Range that defines the minimum and maximum values, if any. An endless range can be used to only have a minimum or maximum.

Optional Arguments#

Note

This constraint does not support a message argument.

exact_message#

Type: String Default: This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.

The message that will be shown if min and max values are equal and the underlying collection’s count is not exactly this value. The message is pluralized depending on how many elements the underlying value has.

Placeholders#

The following placeholders can be used in this message:

  • {{ count }} - The current collection count
  • {{ limit }} - The exact expected collection count

min_message#

Type: String Default: This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more.

The message that will be shown if the underlying collection’s count is less than the min. The message is pluralized depending on how many elements the underlying value has.

Placeholders#

The following placeholders can be used in this message:

  • {{ count }} - The current collection count
  • {{ limit }} - The lower limit

max_message#

Type: String Default: This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.

The message that will be shown if the underlying collection’s count is greater than the max. The message is pluralized depending on how many elements the underlying value has.

Placeholders#

The following placeholders can be used in this message:

  • {{ count }} - The current collection count
  • {{ limit }} - The upper limit

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#

NOT_EQUAL_COUNT_ERROR = "ee29a9b5-924b-42dd-a810-044c86803244"#

TOO_FEW_ERROR = "07f04a04-c346-4983-9868-602c62a5d0c1"#

TOO_MANY_ERROR = "c35d873a-8095-4710-88d0-de68bce36055"#

Constructors#

.new(range : ::Range, min_message : String = "This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more.", max_message : String = "This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.", exact_message : String = "This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.", groups : Array(String) | String | Nil = nil, payload : Hash(String, String) | Nil = nil)#

View source

Methods#

#exact_message : String#

View source

#max : Int32 | ::Nil#

View source

#max_message : String#

View source

#min : Int32 | ::Nil#

View source

#min_message : String#

View source

#validated_by : AVD::ConstraintValidator.class#

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

View source