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

Overview

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

Optional Arguments

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.

Defined in:

assertions/size.cr

Constructors

Instance Method Summary

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, groups : Array(String)? = nil) new

Constructor Detail

def self.new(property_name : String, actual : PropertyType, range : RangeType, normalizer : Proc(PropertyType, PropertyType)? = nil, exact_message : String? = nil, min_message : String? = nil, max_message : String? = nil, message : String? = nil, groups : Array(String)? = nil) #

[View source]

Instance Method Detail

def default_message_template : String #

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


[View source]
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.


[View source]
def valid? : Bool #

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


[View source]