Module: JSONSchemer::Format

Included in:
Schema::Base
Defined in:
lib/json_schemer/format.rb

Constant Summary collapse

EMAIL_REGEX =

this is no good

/\A[^@\s]+@([\p{L}\d-]+\.)+[\p{L}\d\-]{2,}\z/i.freeze
LABEL_REGEX_STRING =
'[\p{L}\p{N}]([\p{L}\p{N}\-]*[\p{L}\p{N}])?'
HOSTNAME_REGEX =
/\A(#{LABEL_REGEX_STRING}\.)*#{LABEL_REGEX_STRING}\z/i.freeze
JSON_POINTER_REGEX_STRING =
'(\/([^~\/]|~[01])*)*'
JSON_POINTER_REGEX =
/\A#{JSON_POINTER_REGEX_STRING}\z/.freeze
RELATIVE_JSON_POINTER_REGEX =
/\A(0|[1-9]\d*)(#|#{JSON_POINTER_REGEX_STRING})?\z/.freeze
DATE_TIME_OFFSET_REGEX =
/(Z|[\+\-]([01][0-9]|2[0-3]):[0-5][0-9])\z/i.freeze
INVALID_QUERY_REGEX =
/[[:space:]]/.freeze

Instance Method Summary collapse

Instance Method Details

#iri_escape(data) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object

Raises:

  • (URI::InvalidURIError)


82
83
84
85
86
87
# 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) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/json_schemer/format.rb', line 60

def valid_date_time?(data)
  DateTime.rfc3339(data)
  DATE_TIME_OFFSET_REGEX.match?(data)
rescue ArgumentError
  false
end

#valid_email?(data) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/json_schemer/format.rb', line 67

def valid_email?(data)
  EMAIL_REGEX.match?(data)
end

#valid_hostname?(data) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# 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) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
# 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) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# 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) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/json_schemer/format.rb', line 120

def valid_json_pointer?(data)
  JSON_POINTER_REGEX.match?(data)
end

#valid_relative_json_pointer?(data) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# 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) ⇒ Boolean

Returns:

  • (Boolean)


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
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# 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) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
# 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) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
# File 'lib/json_schemer/format.rb', line 113

def valid_uri_template?(data)
  URITemplate.new(data)
  true
rescue URITemplate::Invalid
  false
end