Sha256: 69044b32552d466406bf4e4e1eba64d83df81f54ad30ae9e13e9d8958f29271b

Contents?: true

Size: 881 Bytes

Versions: 5

Compression:

Stored size: 881 Bytes

Contents

# frozen_string_literal: true

class ListValidator < BaseValidator

  def validate_each(record, attribute, value)
    assert_valid_all_or_any_or_none!
    assign_attr_readers(record, attribute, value)
    valid?
  end

  private

  def assert_valid_all_or_any_or_none!
    return if options.key?(:all) || options.key?(:any) || options.key?(:none)

    raise ArgumentError, 'Missing ":all", ":any" or ":none" attribute for comparison.'
  end

  def valid_attr?
    options.slice(:all, :any, :none).each do |option, option_value|
      validate_check(option, option_value)
    end
  end

  def valid_content_type?(option, option_value)
    Array(value).public_send("#{option}?") { |val| option_value.include?(val) }
  end

  def validate_check(option, option_value)
    return if valid_content_type?(option, option_value)

    record.errors.add(attribute, *error_message)
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lite-validators-1.8.0 lib/lite/validators/list_validator.rb
lite-validators-1.7.2 lib/lite/validators/list_validator.rb
lite-validators-1.7.1 lib/lite/validators/list_validator.rb
lite-validators-1.7.0 lib/lite/validators/list_validator.rb
lite-validators-1.6.0 lib/lite/validators/list_validator.rb