Sha256: ecf5f4ed38fc402a84422b102f80750540a5bbaec6dbd00090a8cf39ac3ad064

Contents?: true

Size: 486 Bytes

Versions: 2

Compression:

Stored size: 486 Bytes

Contents

require "min_max_validator/version"
require 'active_model'


class MinMaxValidator < ActiveModel::Validator

  def validate(record)
    min = record.send options[:fields][:min]
    max = record.send options[:fields][:max]

    unless valid_max_value?(min: min, max: max)
      record.errors.add(options[:fields][:max], options[:fields][:msg])
    end
  end

  private

  def valid_max_value?(min: 0, max: nil)
    if max.present?
      max >= min
    else
      true
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
min_max_validator-0.0.2 lib/min_max_validator.rb
min_max_validator-0.0.1 lib/min_max_validator.rb