Class: Gitlab::Config::Entry::Validators::PortNamePresentAndUniqueValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/gitlab/config/entry/validators.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object


304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/gitlab/config/entry/validators.rb', line 304

def validate_each(record, attribute, value)
  return unless value.is_a?(Array)

  ports_size = value.count
  return if ports_size <= 1

  named_ports = value.select { |e| e.is_a?(Hash) }.map { |e| e[:name] }.compact.map(&:downcase)

  if ports_size != named_ports.size
    record.errors.add(attribute, 'when there is more than one port, a unique name should be added')
  end

  if ports_size != named_ports.uniq.size
    record.errors.add(attribute, 'each port name must be different')
  end
end