Class: Druid::Query::IntervalsValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/druid/query.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/druid/query.rb', line 26

def validate_each(record, attribute, value)
  if !value.is_a?(Array) || value.blank?
    record.errors.add(attribute, 'must be a list with at least one interval')
    return
  end
  value.each do |interval|
    parts = interval.to_s.split('/')
    record.errors.add(attribute, 'must consist of two ISO8601 dates seperated by /') unless parts.length == 2
    parts = parts.map do |ts|
      ISO8601::DateTime.new(ts) rescue nil
    end
    record.errors.add(attribute, 'must consist of valid ISO8601 dates') unless parts.all?
    record.errors.add(attribute, 'first date needs to be < second date') unless parts.first.to_time < parts.last.to_time
  end
end