Sha256: a10d0400acc6475193de9c4ff743563494f464cfb9ec725f9f3ee7baeedd97f7

Contents?: true

Size: 1.02 KB

Versions: 15

Compression:

Stored size: 1.02 KB

Contents

require 'json-schema/attributes/format'
require 'ipaddr'
require 'socket'

module JSON
  class Schema
    class IPFormat < FormatAttribute
      def self.validate(current_schema, data, fragments, processor, validator, options = {})
        return unless data.is_a?(String)

        begin
          ip = IPAddr.new(data)
        rescue ArgumentError => e
          raise e unless e.message == 'invalid address'
        end

        family = ip_version == 6 ? Socket::AF_INET6 : Socket::AF_INET
        unless ip && ip.family == family
          error_message = "The property '#{build_fragment(fragments)}' must be a valid IPv#{ip_version} address"
          validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
        end
      end

      def self.ip_version
        raise NotImplementedError
      end
    end

    class IP4Format < IPFormat
      def self.ip_version
        4
      end
    end

    class IP6Format < IPFormat
      def self.ip_version
        6
      end
    end
  end
end

Version data entries

15 entries across 14 versions & 4 rubygems

Version Path
json-schema-ouidou-2.9.1 lib/json-schema/attributes/formats/ip.rb
json-schema-ouidou-2.9.0 lib/json-schema/attributes/formats/ip.rb
mountapi-0.11.1 vendor/json-schema/lib/json-schema/attributes/formats/ip.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/bundler/gems/json-schema-2253a5ee6679/lib/json-schema/attributes/formats/ip.rb
json-schema-2.8.1 lib/json-schema/attributes/formats/ip.rb
json-schema-2.8.0 lib/json-schema/attributes/formats/ip.rb
json-schema-2.7.0 lib/json-schema/attributes/formats/ip.rb
json-schema-2.6.2 lib/json-schema/attributes/formats/ip.rb
json-schema-2.6.1 lib/json-schema/attributes/formats/ip.rb
json-schema-2.6.0 lib/json-schema/attributes/formats/ip.rb
json-schema-2.5.2 lib/json-schema/attributes/formats/ip.rb
json-schema-openc-fork-0.0.2 lib/json-schema/attributes/formats/ip.rb
json-schema-2.5.1 lib/json-schema/attributes/formats/ip.rb
json-schema-openc-fork-0.0.1 lib/json-schema/attributes/formats/ip.rb
json-schema-2.5.0 lib/json-schema/attributes/formats/ip.rb