class

Assert::Assertions::Size(PropertyType, RangeType)

Inherits Assert::Assertions::Assertion / Reference / Object

Validates a property's size is within a given Range.

Optional Arguments

  • exact_message - Message to display if range's begin and end are the same and actual is not that value.
  • min_message - Message to display if actual is too small.
  • max_message - Message to display if actual is too big.
  • normalizer - Execute a Proc to alter actual before checking its validity.

Example

class Example
  include Assert

  def initialize; end

  @[Assert::Size(Range(Float64, Float64), range: 2.0..3.0)]
  property fav_numbers : Array(Int32) = [1, 2, 3]

  @[Assert::Size(Range(Float64, Float64), range: 5.0..10.0, min_message: "Password should be at least 5 characters", max_message: "Password cannot be more than 10 characters")]
  property password : String = "monkey12"

  @[Assert::Size(Range(Int32, Int32), range: 5..5, exact_message: "Value must be exactly 5 characters")]
  property exact_value : String = "hello"

  @[Assert::Size(Range(Float64, Float64), range: 5.0..10.0, normalizer: ->(actual : String) { actual.strip })]
  property normalizer : String = "   crystal   "
end

Example.new.valid? # => true

NOTE: PropertyType can be anything that implements #size. NOTE: The generic RangeType represents the type of range.

Constructors

new(property_name : String, actual : PropertyType, range : RangeType, normalizer : Proc(PropertyType, PropertyType) | Nil = nil, exact_message : String | Nil = nil, min_message : String | Nil = nil, max_message : String | Nil = nil, message : String | Nil = nil, groups : Array(String) | Nil = nil)
Source

Instance methods

default_message_template

Returns the default #message_template to use if no message is provided.

Source
message

The message to display if self is not valid.

NOTE: This method is defined automatically, and is just present for documentation purposes.

Source
valid?

Returns true if a property satisfies self, otherwise false. ameba:disable Metrics/CyclomaticComplexity

Source