class Druid::Query::IntervalsValidator

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# 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