theforeman/foreman

View on GitHub
app/validators/array_hostnames_ips_validator.rb

Summary

Maintainability
A
25 mins
Test Coverage
# validates hostnames, IPv4, IPv6 and subnets
class ArrayHostnamesIpsValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors.add attribute, _("must contain valid hostnames") unless value.all? do |item|
      item.to_s.match(URI::HOST) || (IPAddr.new(item.to_s) rescue nil)
    end
  end
end