Class: Fitting::Doc::JsonSchema

Inherits:
Step
  • Object
show all
Defined in:
lib/fitting/doc/json_schema.rb

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary

Attributes inherited from Step

#index_after, #index_before, #index_medium, #next_steps, #res_after, #res_before, #res_medium, #step_cover_size, #step_key

Instance Method Summary collapse

Methods inherited from Step

#index_offset, #mark_enum, #mark_required, #new_index_offset, #next, #nocover!, #range, #valid?

Constructor Details

#initialize(json_schema, super_schema) ⇒ JsonSchema

Returns a new instance of JsonSchema.



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
# File 'lib/fitting/doc/json_schema.rb', line 16

def initialize(json_schema, super_schema)
  @super_schema = super_schema
  @logs = []
  @step_cover_size = 0
  @step_key = json_schema
  @next_steps = []
  @oneOf = false
  Fitting::Cover::JSONSchemaOneOf.new(json_schema).combi.each do |combination|
    @oneOf = true
    @next_steps.push(CombinationOneOf.new(combination[0], combination[1][0], combination[1][1], json_schema))
  end
  combinations = Fitting::Cover::JSONSchemaEnum.new(json_schema).combi
  if combinations.size > 1
    combinations.each do |comb|
      @next_steps.push(CombinationEnum.new(comb[0], comb[1][0], comb[1][1], json_schema))
    end
  end

  if json_schema['type'] != 'array'
    combinations = Fitting::Cover::JSONSchema.new(json_schema).combi
    combinations.each do |comb|
      @next_steps.push(CombinationOptional.new(comb[0], comb[1][0], comb[1][1], json_schema))
    end
  end
end

Instance Method Details

#cover!(log) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fitting/doc/json_schema.rb', line 42

def cover!(log)
  if @super_schema
    @step_cover_size += 1
    @logs.push(log.body)
    @next_steps.each { |combination| combination.cover!(log) }
  elsif JSON::Validator.fully_validate(@step_key, log.body) == []
    @step_cover_size += 1
    @logs.push(log.body)
    @next_steps.each { |combination| combination.cover!(log) }
  else
    raise Fitting::Doc::JsonSchema::NotFound.new "json-schema: #{::JSON.pretty_generate(@step_key)}\n\n"\
      "body: #{::JSON.pretty_generate(log.body)}\n\n"\
      "error #{JSON::Validator.fully_validate(@step_key, log.body).first}"
  end
rescue JSON::Schema::SchemaError => e
  raise Fitting::Doc::JsonSchema::NotFound.new "json-schema: #{::JSON.pretty_generate(@step_key)}\n\n"\
      "body: #{::JSON.pretty_generate(log.body)}\n\n"\
      "error #{e.message}"
rescue Fitting::Doc::CombinationOneOf::NotFound => e
  raise Fitting::Doc::JsonSchema::NotFound.new "#{e.message}\n\nsource json-schema: #{::JSON.pretty_generate(@step_key)}\n\n"
end

#logsObject



64
65
66
# File 'lib/fitting/doc/json_schema.rb', line 64

def logs
  @logs
end

#mark_range(index, res) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fitting/doc/json_schema.rb', line 68

def mark_range(index, res)
  start_index = index
  end_index = start_index + 2

  (start_index..end_index).each do |i|
    res[i] = @step_cover_size
  end

  if @step_key["required"]
    mark_required(end_index, res, @step_key)
  end
  end_index
end

#report(res, index) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fitting/doc/json_schema.rb', line 86

def report(res, index)
  @index_before = index
  @res_before = [] + res

  index = mark_range(index, res)
  @index_medium = index
  @res_medium = [] + res

  if @next_steps != []
    new_index = index
    @next_steps.each do |next_step|
      if @oneOf
        res, new_index = next_step.report(res, new_index)
      else
        res, new_index = next_step.report(res, @index_before)
      end
    end
  end

  index += index_offset
  @index_after = index
  @res_after = [] + res
  [res, index]
end

#to_hashObject



82
83
84
# File 'lib/fitting/doc/json_schema.rb', line 82

def to_hash
  @step_key
end