Class: ActiveModel::Validations::IpAddressValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::IpAddressValidator
- Defined in:
- lib/activemodel/validations/ip_address_validator.rb
Constant Summary collapse
- PERMITTED_VERSION =
%i[v4 v6].freeze
Instance Method Summary collapse
- #check_validity! ⇒ Object
-
#initialize(options) ⇒ IpAddressValidator
constructor
A new instance of IpAddressValidator.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ IpAddressValidator
Returns a new instance of IpAddressValidator.
11 12 13 14 15 |
# File 'lib/activemodel/validations/ip_address_validator.rb', line 11 def initialize() [:version] ||= %i[v4 v6] [:version] = Array([:version]) super end |
Instance Method Details
#check_validity! ⇒ Object
24 25 26 27 28 |
# File 'lib/activemodel/validations/ip_address_validator.rb', line 24 def check_validity! return if [:version].present? && ([:version] - PERMITTED_VERSION).blank? raise ArgumentError, "Either :v4 and/or :v6 must be supplied." end |
#validate_each(record, attribute, value) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/activemodel/validations/ip_address_validator.rb', line 17 def validate_each(record, attribute, value) return if [:version].include?(:v4) && ::Resolv::IPv4::Regex.match?(value) return if [:version].include?(:v6) && ::Resolv::IPv6::Regex.match?(value) record_error(record, attribute, value) end |