Skip to content

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

Validates that the length of a String is between some minimum and maximum. Non String values are stringified via #to_s.

class User
  include AVD::Validatable

  def initialize(@username : String); end

  @[Assert::Length(3..30)]
  property username : 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.

unit#

Type: AVD::Constraints::Length::Unit Default: AVD::Constraints::Length::Unit::CODEPOINTS

Which unit should be used to determine the length of the string.

exact_message#

Type: String Default: This value should have exactly {{ limit }} character.|This value should have exactly {{ limit }} characters.

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

Placeholders#

The following placeholders can be used in this message:

  • {{ limit }} - The exact expected length.
  • {{ value }} - The current (invalid) value.
  • {{ value_length }} - The current value's length.

min_message#

Type: String Default: This value is too short. It should have {{ limit }} character or more.|This value is too short. It should have {{ limit }} characters or more.

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

Placeholders#

The following placeholders can be used in this message:

  • {{ limit }} - The exact minimum length.
  • {{ min }} - The expected minimum length.
  • {{ max }} - The expected maximum length.
  • {{ value }} - The current (invalid) value.
  • {{ value_length }} - The current value's length.

max_message#

Type: String Default: This value is too long. It should have {{ limit }} character or less.|This value is too long. It should have {{ limit }} characters or less.

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

Placeholders#

The following placeholders can be used in this message:

  • {{ limit }} - The exact maximum length.
  • {{ min }} - The expected minimum length.
  • {{ max }} - The expected maximum length.
  • {{ value }} - The current (invalid) value.
  • {{ value_length }} - The current value's length.

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_LENGTH_ERROR = "03ef6899-6e39-4e7a-9ac9-5f4374736273"#

TOO_LONG_ERROR = "e07eee2c-be7a-4ac3-be6b-2ea344250f99"#

TOO_SHORT_ERROR = "643f9d15-a5fd-41b7-b6d8-85f40855ba11"#

Constructors#

.new(range : ::Range, min_message : String = "This value is too short. It should have {{ limit }} character or more.|This value is too short. It should have {{ limit }} characters or more.", max_message : String = "This value is too long. It should have {{ limit }} character or less.|This value is too long. It should have {{ limit }} characters or less.", exact_message : String = "This value should have exactly {{ limit }} character.|This value should have exactly {{ limit }} characters.", unit : AVD::Constraints::Length::Unit = :codepoints, 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

#unit : AVD::Constraints::Length::Unit#

View source

#validated_by : AVD::ConstraintValidator.class#

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

View source