1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
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
|