robertgauld/osm

View on GitHub
lib/array_of_validator.rb

Summary

Maintainability
A
1 hr
Test Coverage
class ArrayOfValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors.add(attribute, 'must be an Array') unless value.is_a?(Array)
    value.each do |value_item|
      unless value_item.is_a?(options[:item_type])
        record.errors.add(attribute, "items in the Array must be a #{options[:item_type].name}")
      else
        # We don't want to check the item is valid if it's the wrong type
        unless options[:item_valid].nil?
          # Check validity of item matches item_valid option
          unless value_item.valid?.eql?(options[:item_valid])
            message = "contains #{value_item.valid? ? 'a valid' : 'an invalid'} item"
            record.errors.add(attribute, message)
          end
        end
      end
    end
  end
end