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

Overview

Validates a property is within a given Range.

Optional Arguments

Example

class Example
  include Assert

  def initialize; end

  @[Assert::InRange(Range(Float64, Nil), range: 0.0..)]
  property minimum_only : Int32 = 27

  @[Assert::InRange(Range(Nil, Int64), range: ..100_000_000_000)]
  property maximum_only : Int32 = 50_000

  @[Assert::InRange(Range(Float64, Float64), range: 0.0..1000.0)]
  property range : Float64 = 3.14

  @[Assert::InRange(Range(Int32, Int32), range: 0..10, not_in_range_message: "That is not a valid %{property_name}")]
  property fav_number : UInt8 = 8_u8

  @[Assert::InRange(Range(UInt8, UInt8), range: 0_u8..50_u8, min_message: "Number of cores must be positive", max_message: "There must be less than 50 cores")]
  property cores : Int32 = 32
end

Example.new.valid? # => true

NOTE The generic RangeType represents the type of range.

Defined in:

assertions/in_range.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 = nil, groups : Array(String) | Nil = nil) new

Constructor Detail

def self.new(property_name : String, actual : PropertyType, range : RangeType, not_in_range_message : String | Nil = nil, min_message : String | Nil = nil, max_message : String | Nil = nil, message : String | Nil = nil, groups : Array(String) | Nil = 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]