Sha256: a8b6b17e58f260cde7a6bf53fcc003cea2656bc90dcbd1915660ef8f6b09637e

Contents?: true

Size: 762 Bytes

Versions: 34

Compression:

Stored size: 762 Bytes

Contents

# frozen_string_literal: true

class MiniDefender::Rules::DigitsBetween < MiniDefender::Rule
  def initialize(min, max)
    raise ArgumentError, 'Min must be a positive integer' unless min.is_a?(Integer) && min > 0
    raise ArgumentError, 'Max must be a positive integer' unless max.is_a?(Integer) && max > 0

    @min = min
    @max = max
  end

  def self.signature
    'digits_between'
  end

  def self.make(args)
    self.new(args[0].to_i, args[1].to_i)
  end

  def passes?(attribute, value, validator)
    value = value&.to_s

    value.is_a?(String) && /\A[0-9]+\z/.match?(value) && value.length >= @min && value.length <= @max
  end

  def message(attribute, value, validator)
    "The field must contain digits between #{@min} and #{@max}."
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
mini_defender-0.6.8 lib/mini_defender/rules/digits_between.rb
mini_defender-0.6.7 lib/mini_defender/rules/digits_between.rb
mini_defender-0.6.5 lib/mini_defender/rules/digits_between.rb
mini_defender-0.6.4 lib/mini_defender/rules/digits_between.rb
mini_defender-0.6.3 lib/mini_defender/rules/digits_between.rb
mini_defender-0.6.2 lib/mini_defender/rules/digits_between.rb
mini_defender-0.6.1 lib/mini_defender/rules/digits_between.rb
mini_defender-0.6.0 lib/mini_defender/rules/digits_between.rb
mini_defender-0.5.8 lib/mini_defender/rules/digits_between.rb
mini_defender-0.5.7 lib/mini_defender/rules/digits_between.rb
mini_defender-0.5.6 lib/mini_defender/rules/digits_between.rb
mini_defender-0.5.5 lib/mini_defender/rules/digits_between.rb
mini_defender-0.5.4 lib/mini_defender/rules/digits_between.rb
mini_defender-0.5.3 lib/mini_defender/rules/digits_between.rb
mini_defender-0.5.2 lib/mini_defender/rules/digits_between.rb
mini_defender-0.5.1 lib/mini_defender/rules/digits_between.rb
mini_defender-0.5.0 lib/mini_defender/rules/digits_between.rb
mini_defender-0.4.1 lib/mini_defender/rules/digits_between.rb
mini_defender-0.4.0 lib/mini_defender/rules/digits_between.rb
mini_defender-0.3.14 lib/mini_defender/rules/digits_between.rb