Class: JSONSchemer::Result

Inherits:
Struct
  • Object
show all
Defined in:
lib/json_schemer/result.rb

Constant Summary collapse

CLASSIC_ERROR_TYPES =
Hash.new do |hash, klass|
  hash[klass] = klass.name.rpartition('::').last.sub(/\A[[:alpha:]]/, &:downcase)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#annotationObject

Returns the value of attribute annotation

Returns:

  • (Object)

    the current value of annotation



3
4
5
# File 'lib/json_schemer/result.rb', line 3

def annotation
  @annotation
end

#detailsObject

Returns the value of attribute details

Returns:

  • (Object)

    the current value of details



3
4
5
# File 'lib/json_schemer/result.rb', line 3

def details
  @details
end

#ignore_nestedObject

Returns the value of attribute ignore_nested

Returns:

  • (Object)

    the current value of ignore_nested



3
4
5
# File 'lib/json_schemer/result.rb', line 3

def ignore_nested
  @ignore_nested
end

#instanceObject

Returns the value of attribute instance

Returns:

  • (Object)

    the current value of instance



3
4
5
# File 'lib/json_schemer/result.rb', line 3

def instance
  @instance
end

#instance_locationObject

Returns the value of attribute instance_location

Returns:

  • (Object)

    the current value of instance_location



3
4
5
# File 'lib/json_schemer/result.rb', line 3

def instance_location
  @instance_location
end

#keyword_locationObject

Returns the value of attribute keyword_location

Returns:

  • (Object)

    the current value of keyword_location



3
4
5
# File 'lib/json_schemer/result.rb', line 3

def keyword_location
  @keyword_location
end

#nestedObject

Returns the value of attribute nested

Returns:

  • (Object)

    the current value of nested



3
4
5
# File 'lib/json_schemer/result.rb', line 3

def nested
  @nested
end

#nested_keyObject

Returns the value of attribute nested_key

Returns:

  • (Object)

    the current value of nested_key



3
4
5
# File 'lib/json_schemer/result.rb', line 3

def nested_key
  @nested_key
end

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



3
4
5
# File 'lib/json_schemer/result.rb', line 3

def source
  @source
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



3
4
5
# File 'lib/json_schemer/result.rb', line 3

def type
  @type
end

#validObject

Returns the value of attribute valid

Returns:

  • (Object)

    the current value of valid



3
4
5
# File 'lib/json_schemer/result.rb', line 3

def valid
  @valid
end

Instance Method Details

#basicObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/json_schemer/result.rb', line 65

def basic
  out = to_output_unit
  if nested&.any?
    out[nested_key] = Enumerator.new do |yielder|
      results = [self]
      while result = results.pop
        if result.ignore_nested || !result.nested&.any?
          yielder << result.to_output_unit
        else
          previous_results_size = results.size
          result.nested.reverse_each do |nested_result|
            results << nested_result if nested_result.valid == valid
          end
          yielder << result.to_output_unit unless (results.size - previous_results_size) == 1
        end
      end
    end
  end
  out
end

#classicObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/json_schemer/result.rb', line 112

def classic
  Enumerator.new do |yielder|
    unless valid
      results = [self]
      while result = results.pop
        if result.ignore_nested || !result.nested&.any?
          yielder << result.to_classic
        else
          previous_results_size = results.size
          result.nested.reverse_each do |nested_result|
            results << nested_result if nested_result.valid == valid
          end
          yielder << result.to_classic if (results.size - previous_results_size) == 0
        end
      end
    end
  end
end

#detailedObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/json_schemer/result.rb', line 86

def detailed
  return to_output_unit if ignore_nested || !nested&.any?
  matching_results = nested.select { |nested_result| nested_result.valid == valid }
  if matching_results.size == 1
    matching_results.first.detailed
  else
    out = to_output_unit
    if matching_results.any?
      out[nested_key] = Enumerator.new do |yielder|
        matching_results.each { |nested_result| yielder << nested_result.detailed }
      end
    end
    out
  end
end

#errorObject



25
26
27
28
29
30
31
32
# File 'lib/json_schemer/result.rb', line 25

def error
  return @error if defined?(@error)
  resolved_instance_location = Location.resolve(instance_location)
  @error = source.error(
    :formatted_instance_location => resolved_instance_location.empty? ? 'root' : "`#{resolved_instance_location}`",
    :details => details
  )
end

#flagObject



61
62
63
# File 'lib/json_schemer/result.rb', line 61

def flag
  { 'valid' => valid }
end

#insert_property_defaults(context) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/json_schemer/result.rb', line 131

def insert_property_defaults(context)
  instance_locations = {}

  results = [[self, true]]
  while (result, valid = results.pop)
    next if result.source.is_a?(Schema::NOT_KEYWORD_CLASS)

    valid &&= result.valid
    result.nested&.each { |nested_result| results << [nested_result, valid] }

    if result.source.is_a?(Schema::PROPERTIES_KEYWORD_CLASS) && result.instance.is_a?(Hash)
      result.source.parsed.each do |property, schema|
        next if result.instance.key?(property) || !schema.parsed.key?('default')
        default = schema.parsed.fetch('default')
        instance_location = Location.join(result.instance_location, property)
        keyword_location = Location.join(Location.join(result.keyword_location, property), default.keyword)
        default_result = default.validate(nil, instance_location, keyword_location, nil)
        instance_locations[result.instance_location] ||= {}
        instance_locations[result.instance_location][property] ||= []
        instance_locations[result.instance_location][property] << [default_result, valid]
      end
    end
  end

  inserted = false

  instance_locations.each do |instance_location, properties|
    original_instance = context.original_instance(instance_location)
    properties.each do |property, results_with_tree_validity|
      property_inserted = yield(original_instance, property, results_with_tree_validity)
      inserted ||= (property_inserted != false)
    end
  end

  inserted
end

#output(output_format) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/json_schemer/result.rb', line 8

def output(output_format)
  case output_format
  when 'classic'
    classic
  when 'flag'
    flag
  when 'basic'
    basic
  when 'detailed'
    detailed
  when 'verbose'
    verbose
  else
    raise UnknownOutputFormat, output_format
  end
end

#to_classicObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/json_schemer/result.rb', line 46

def to_classic
  schema = source.schema
  out = {
    'data' => instance,
    'data_pointer' => Location.resolve(instance_location),
    'schema' => schema.value,
    'schema_pointer' => schema.schema_pointer,
    'root_schema' => schema.root.value,
    'type' => type || CLASSIC_ERROR_TYPES[source.class]
  }
  out['error'] = error
  out['details'] = details if details
  out
end

#to_output_unitObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/json_schemer/result.rb', line 34

def to_output_unit
  out = {
    'valid' => valid,
    'keywordLocation' => Location.resolve(keyword_location),
    'absoluteKeywordLocation' => source.absolute_keyword_location,
    'instanceLocation' => Location.resolve(instance_location)
  }
  out['error'] = error unless valid
  out['annotation'] = annotation if valid && annotation
  out
end

#verboseObject



102
103
104
105
106
107
108
109
110
# File 'lib/json_schemer/result.rb', line 102

def verbose
  out = to_output_unit
  if nested&.any?
    out[nested_key] = Enumerator.new do |yielder|
      nested.each { |nested_result| yielder << nested_result.verbose }
    end
  end
  out
end