Class: Raif::JsonSchemaBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/raif/json_schema_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJsonSchemaBuilder

Returns a new instance of JsonSchemaBuilder.

[View source]

7
8
9
10
11
# File 'lib/raif/json_schema_builder.rb', line 7

def initialize
  @properties = {}
  @required_properties = []
  @items_schema = nil
end

Instance Attribute Details

#items_schemaObject (readonly)

Returns the value of attribute items_schema.


5
6
7
# File 'lib/raif/json_schema_builder.rb', line 5

def items_schema
  @items_schema
end

#propertiesObject (readonly)

Returns the value of attribute properties.


5
6
7
# File 'lib/raif/json_schema_builder.rb', line 5

def properties
  @properties
end

#required_propertiesObject (readonly)

Returns the value of attribute required_properties.


5
6
7
# File 'lib/raif/json_schema_builder.rb', line 5

def required_properties
  @required_properties
end

Instance Method Details

#array(name, options = {}, &block) ⇒ Object

[View source]

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
# File 'lib/raif/json_schema_builder.rb', line 51

def array(name, options = {}, &block)
  items_schema = options.delete(:items) || {}

  if block_given?
    nested_builder = self.class.new
    nested_builder.instance_eval(&block)

    # If items were directly set using the items method
    if nested_builder.items_schema.present?
      items_schema = nested_builder.items_schema
    # If there are properties defined, it's an object schema
    elsif nested_builder.properties.any?
      items_schema = {
        type: "object",
        properties: nested_builder.properties,
        additionalProperties: false
      }

      # We currently use strict mode, which means that all properties are required
      items_schema[:required] = nested_builder.required_properties
    end
  end

  options[:items] = items_schema unless items_schema.empty?
  add_property(name, "array", options)
end

#boolean(name, options = {}) ⇒ Object

[View source]

25
26
27
# File 'lib/raif/json_schema_builder.rb', line 25

def boolean(name, options = {})
  add_property(name, "boolean", options)
end

#integer(name, options = {}) ⇒ Object

[View source]

17
18
19
# File 'lib/raif/json_schema_builder.rb', line 17

def integer(name, options = {})
  add_property(name, "integer", options)
end

#items(options = {}) ⇒ Object

Allow setting array items directly

[View source]

79
80
81
# File 'lib/raif/json_schema_builder.rb', line 79

def items(options = {})
  @items_schema = options
end

#number(name, options = {}) ⇒ Object

[View source]

21
22
23
# File 'lib/raif/json_schema_builder.rb', line 21

def number(name, options = {})
  add_property(name, "number", options)
end

#object(name = nil, options = {}, &block) ⇒ Object

[View source]

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/raif/json_schema_builder.rb', line 29

def object(name = nil, options = {}, &block)
  schema = {}

  if block_given?
    nested_builder = self.class.new
    nested_builder.instance_eval(&block)

    schema[:properties] = nested_builder.properties
    schema[:additionalProperties] = false

    # We currently use strict mode, which means that all properties are required
    schema[:required] = nested_builder.required_properties
  end

  # If name is nil, we're inside an array and should return the schema directly
  if name.nil?
    @items_schema = { type: "object" }.merge(schema)
  else
    add_property(name, "object", options.merge(schema))
  end
end

#string(name, options = {}) ⇒ Object

[View source]

13
14
15
# File 'lib/raif/json_schema_builder.rb', line 13

def string(name, options = {})
  add_property(name, "string", options)
end

#to_schemaObject

[View source]

83
84
85
86
87
88
89
90
# File 'lib/raif/json_schema_builder.rb', line 83

def to_schema
  {
    type: "object",
    additionalProperties: false,
    properties: @properties,
    required: @required_properties
  }
end