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.



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



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

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



39
40
41
# File 'lib/raif/json_schema_builder.rb', line 39

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

#build_with_instance(instance, &block) ⇒ JsonSchemaBuilder

Build schema with instance context for instance-dependent schemas The block receives the instance as a parameter and has access to the builder methods

Parameters:

  • instance (Object)

    The instance to use as context

  • block (Proc)

    The block to evaluate with instance context

Returns:



19
20
21
22
23
24
25
# File 'lib/raif/json_schema_builder.rb', line 19

def build_with_instance(instance, &block)
  # Evaluate the block in the context of the builder, passing the instance as parameter
  # This allows the block to use both builder methods (string, integer, etc.)
  # and access the instance parameter for conditional logic
  instance_exec(instance, &block)
  self
end

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



31
32
33
# File 'lib/raif/json_schema_builder.rb', line 31

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

#items(options = {}) ⇒ Object

Allow setting array items directly



93
94
95
# File 'lib/raif/json_schema_builder.rb', line 93

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

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



35
36
37
# File 'lib/raif/json_schema_builder.rb', line 35

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

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



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

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



27
28
29
# File 'lib/raif/json_schema_builder.rb', line 27

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

#to_schemaObject



97
98
99
100
101
102
103
104
# File 'lib/raif/json_schema_builder.rb', line 97

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