class

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

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

Validates a property is within a given Range.

Optional Arguments

  • not_in_range_message - Message to display if actual is not included in range.
  • min_message - Message to display if range only has a minimum and actual is too small.
  • max_message - Message to display if range only has a maximum and actual is too big.

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.

Constructors

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)
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