module JSONSchemer::Format

Constants

DATE_TIME_OFFSET_REGEX
EMAIL_REGEX

this is no good

HOSTNAME_REGEX
INVALID_QUERY_REGEX
JSON_POINTER_REGEX
JSON_POINTER_REGEX_STRING
LABEL_REGEX_STRING
RELATIVE_JSON_POINTER_REGEX

Public Instance Methods

iri_escape(data) click to toggle source
# File lib/json_schemer/format.rb, line 102
def iri_escape(data)
  data.gsub(/[^[:ascii:]]/) do |match|
    us = match
    tmp = +''
    us.each_byte do |uc|
      tmp << sprintf('%%%02X', uc)
    end
    tmp
  end.force_encoding(Encoding::US_ASCII)
end
parse_uri_scheme(data) click to toggle source
# File lib/json_schemer/format.rb, line 82
def parse_uri_scheme(data)
  scheme, _userinfo, _host, _port, _registry, _path, opaque, query, _fragment = URI::RFC3986_PARSER.split(data)
  # URI::RFC3986_PARSER.parse allows spaces in these and I don't think it should
  raise URI::InvalidURIError if INVALID_QUERY_REGEX.match?(query) || INVALID_QUERY_REGEX.match?(opaque)
  scheme
end
valid_date_time?(data) click to toggle source
# File lib/json_schemer/format.rb, line 60
def valid_date_time?(data)
  DateTime.rfc3339(data)
  DATE_TIME_OFFSET_REGEX.match?(data)
rescue ArgumentError => e
  false
end
valid_email?(data) click to toggle source
# File lib/json_schemer/format.rb, line 67
def valid_email?(data)
  EMAIL_REGEX.match?(data)
end
valid_hostname?(data) click to toggle source
# File lib/json_schemer/format.rb, line 71
def valid_hostname?(data)
  HOSTNAME_REGEX.match?(data) && data.split('.').all? { |label| label.size <= 63 }
end
valid_ip?(data, type) click to toggle source
# File lib/json_schemer/format.rb, line 75
def valid_ip?(data, type)
  ip_address = IPAddr.new(data)
  type == :v4 ? ip_address.ipv4? : ip_address.ipv6?
rescue IPAddr::InvalidAddressError
  false
end
valid_json?(data) click to toggle source
# File lib/json_schemer/format.rb, line 53
def valid_json?(data)
  JSON.parse(data)
  true
rescue JSON::ParserError
  false
end
valid_json_pointer?(data) click to toggle source
# File lib/json_schemer/format.rb, line 120
def valid_json_pointer?(data)
  JSON_POINTER_REGEX.match?(data)
end
valid_relative_json_pointer?(data) click to toggle source
# File lib/json_schemer/format.rb, line 124
def valid_relative_json_pointer?(data)
  RELATIVE_JSON_POINTER_REGEX.match?(data)
end
valid_spec_format?(data, format) click to toggle source
# File lib/json_schemer/format.rb, line 14
def valid_spec_format?(data, format)
  case format
  when 'date-time'
    valid_date_time?(data)
  when 'date'
    valid_date_time?("#{data}T04:05:06.123456789+07:00")
  when 'time'
    valid_date_time?("2001-02-03T#{data}")
  when 'email'
    data.ascii_only? && valid_email?(data)
  when 'idn-email'
    valid_email?(data)
  when 'hostname'
    data.ascii_only? && valid_hostname?(data)
  when 'idn-hostname'
    valid_hostname?(data)
  when 'ipv4'
    valid_ip?(data, :v4)
  when 'ipv6'
    valid_ip?(data, :v6)
  when 'uri'
    valid_uri?(data)
  when 'uri-reference'
    valid_uri_reference?(data)
  when 'iri'
    valid_uri?(iri_escape(data))
  when 'iri-reference'
    valid_uri_reference?(iri_escape(data))
  when 'uri-template'
    valid_uri_template?(data)
  when 'json-pointer'
    valid_json_pointer?(data)
  when 'relative-json-pointer'
    valid_relative_json_pointer?(data)
  when 'regex'
    EcmaReValidator.valid?(data)
  end
end
valid_uri?(data) click to toggle source
# File lib/json_schemer/format.rb, line 89
def valid_uri?(data)
  !!parse_uri_scheme(data)
rescue URI::InvalidURIError
  false
end
valid_uri_reference?(data) click to toggle source
# File lib/json_schemer/format.rb, line 95
def valid_uri_reference?(data)
  parse_uri_scheme(data)
  true
rescue URI::InvalidURIError
  false
end
valid_uri_template?(data) click to toggle source
# File lib/json_schemer/format.rb, line 113
def valid_uri_template?(data)
  URITemplate.new(data)
  true
rescue URITemplate::Invalid
  false
end