Class: Randrizer::Drivers::JSONSchema::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/randrizer/drivers/json_schema/driver.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed_schema) ⇒ Driver

Returns a new instance of Driver.



23
24
25
# File 'lib/randrizer/drivers/json_schema/driver.rb', line 23

def initialize(parsed_schema)
  @json = parsed_schema
end

Instance Attribute Details

#pathObject (readonly)

JSON Schemas are much more complex than one would initially imagine. This is a very simple driver for basic schemas, but not all the attributes and properties of types are respected. TODO: support for list and dict datatypes



16
17
18
# File 'lib/randrizer/drivers/json_schema/driver.rb', line 16

def path
  @path
end

Class Method Details

.for(content:) ⇒ Object



18
19
20
21
# File 'lib/randrizer/drivers/json_schema/driver.rb', line 18

def self.for(content:)
  parsed_content = JSON.parse(content)
  new(parsed_content)
end

Instance Method Details

#type_treeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/randrizer/drivers/json_schema/driver.rb', line 27

def type_tree
  dict_items = []

  required_keys = json.fetch("required", [])
  json.fetch("properties", []).map do |key, attrs|
    key_type = Types::Const[key]
    root_type = attrs["type"]
    value_type = gen_value_type(root_type, attrs)

    unless required_keys.include?(key)
      key_type = Types::Optional[inner_type: key_type]
    end

    dict_items.append([key_type, value_type])
  end

  Types::Dict[dict_items]
end