1 # frozen_string_literal: true
3 # Validates that an inet/cidr column was given parseable input. ActiveRecord
4 # casts anything unparseable to nil, so the cast value alone can't tell a
5 # blank field from a typo; compare it against the raw input instead.
6 class InetValidator < ActiveModel::EachValidator
7 def validate_each(record, attribute, value)
8 raw = record.read_attribute_before_type_cast(attribute)
9 record.errors.add(attribute, options[:message] || :invalid) if raw.present? && value.nil?