Sha256: 4e0de4a6d29de3f911a80c04e2e91a696017dad6c7947217c1983f3e0319c3dd

Contents?: true

Size: 661 Bytes

Versions: 4

Compression:

Stored size: 661 Bytes

Contents

# frozen_string_literal: true

class BooleanValidator < BaseValidator

  CHECKS ||= {
    all: nil,
    false_only: %w[0 f false n no off],
    true_only: %w[1 t true y yes on]
  }.freeze

  def validate_each(record, attribute, value)
    assert_valid_check!
    super
  end

  private

  def assert_valid_check!
    assert_valid_option!(:check, CHECKS.keys)
  end

  def check
    options[:check] || :all
  end

  def error_message
    [options[:message] || I18n.t("errors.messages.boolean.#{check}")]
  end

  def valid_attr?
    checks = CHECKS[check] || [].concat(CHECKS[:false_only], CHECKS[:true_only])
    checks.include?(value.to_s.downcase)
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lite-validators-1.1.1 lib/lite/validators/boolean_validator.rb
lite-validators-1.1.0 lib/lite/validators/boolean_validator.rb
lite-validators-1.0.7 lib/lite/validators/boolean_validator.rb
lite-validators-1.0.6 lib/lite/validators/boolean_validator.rb