Class: JSON::Schema::FormatAttribute

Inherits:
Attribute
  • Object
show all
Defined in:
lib/json-schema/attributes/format.rb

Class Method Summary collapse

Methods inherited from Attribute

build_fragment, validation_error

Class Method Details

.validate(current_schema, data, fragments, validator, options = {}) ⇒ Object



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/json-schema/attributes/format.rb', line 4

def self.validate(current_schema, data, fragments, validator, options = {})
  case current_schema.schema['format']

  # Timestamp in restricted ISO-8601 YYYY-MM-DDThh:mm:ssZ
  when 'date-time'
    if data.is_a?(String)
      error_message = "The property '#{build_fragment(fragments)}' must be a date/time in the ISO-8601 format of YYYY-MM-DDThh:mm:ssZ"
      validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if !data.is_a?(String)
      r = Regexp.new('^\d\d\d\d-\d\d-\d\dT(\d\d):(\d\d):(\d\d)Z$')
      if (m = r.match(data))
        parts = data.split("T")
        begin
          Date.parse(parts[0])
        rescue Exception
          validation_error(error_message, fragments, current_schema, options[:record_errors])
          return
        end
        begin
          validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if m[1].to_i > 23
          validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if m[2].to_i > 59
          validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if m[3].to_i > 59
        rescue Exception
          validation_error(error_message, fragments, current_schema, options[:record_errors])
          return
        end
      else
        validation_error(error_message, fragments, current_schema, options[:record_errors])
        return
      end
    end

  # Date in the format of YYYY-MM-DD
  when 'date'
    if data.is_a?(String)
      error_message = "The property '#{build_fragment(fragments)}' must be a date in the format of YYYY-MM-DD"
      validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if !data.is_a?(String)
      r = Regexp.new('^\d\d\d\d-\d\d-\d\d$')
      if (m = r.match(data))
        begin
          Date.parse(data)
        rescue Exception
          validation_error(error_message, fragments, current_schema, options[:record_errors])
          return
        end
      else
        validation_error(error_message, fragments, current_schema, options[:record_errors])
        return
      end
    end

  # Time in the format of HH:MM:SS
  when 'time'
    if data.is_a?(String)
      error_message = "The property '#{build_fragment(fragments)}' must be a time in the format of hh:mm:ss"
      validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if !data.is_a?(String)
      r = Regexp.new('^(\d\d):(\d\d):(\d\d)$')
      if (m = r.match(data))
        validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if m[1].to_i > 23
        validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if m[2].to_i > 59
        validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if m[3].to_i > 59
      else
        validation_error(error_message, fragments, current_schema, options[:record_errors])
        return
      end
    end

  # IPv4 in dotted-quad format
  when 'ip-address', 'ipv4'
    if data.is_a?(String)
      error_message = "The property '#{build_fragment(fragments)}' must be a valid IPv4 address"
      validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if !data.is_a?(String)
      r = Regexp.new('^(\d+){1,3}\.(\d+){1,3}\.(\d+){1,3}\.(\d+){1,3}$')
      if (m = r.match(data))
        1.upto(4) do |x|
          validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if m[x].to_i > 255
        end
      else
        validation_error(error_message, fragments, current_schema, options[:record_errors])
        return
      end
    end

  # IPv6 in standard format (including abbreviations)
  when 'ipv6'
    if data.is_a?(String)
      error_message = "The property '#{build_fragment(fragments)}' must be a valid IPv6 address"
      validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if !data.is_a?(String)
      r = Regexp.new('^[a-f0-9:]+$')
      if (m = r.match(data))
        # All characters are valid, now validate structure
        parts = data.split(":")
        validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if parts.length > 8
        condensed_zeros = false
        parts.each do |part|
          if part.length == 0
            validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if condensed_zeros
            condensed_zeros = true
          end
          validation_error(error_message, fragments, current_schema, options[:record_errors]) and return if part.length > 4
        end
      else
        validation_error(error_message, fragments, current_schema, options[:record_errors])
        return
      end
    end
  end
end