1

I'd like to extend NumberValidator to also validate for min and max values. I'd like to still call the default NumberValidator and have it return its own description (Must be a number) should it fail. Right now, I'm always getting my own implementation's description ('Must be a number between...`)

This is what I have thus far:

class NumberValidator < Apipie::Validator::BaseValidator

  def initialize(param_description, argument, options)
    super(param_description)
    @number_validator = Apipie::Validator::NumberValidator.new(param_description)
    @type = argument
    @options = options
  end

  def validate(value)
    return false unless @number_validator.valid?(value)

    if (@options[:min]) && (@options[:max])
      self.class.validate(value) && value.to_i.between?(@options[:min], @options[:max])
    end

    true
  end

  def self.build(param_description, argument, options, block)
    if argument == :number
      self.new(param_description, argument, options)
    end
  end

  def description
    "Must be a number between #{@options[:min]} and #{@options[:max]}."
  end
end
OwenH
  • 58
  • 7

0 Answers0