Module: RubyPi::Schema

Defined in:
lib/ruby_pi/tools/schema.rb

Class Method Summary collapse

Class Method Details

.array(description: nil, required: false, items: nil, min_items: nil, max_items: nil, unique_items: nil) ⇒ Hash

Builds a JSON Schema for an array property.

Parameters:

  • description (String, nil) (defaults to: nil)

    Human-readable description.

  • required (Boolean) (defaults to: false)

    Whether this property is required.

  • items (Hash, nil) (defaults to: nil)

    JSON Schema hash describing each array element.

  • min_items (Integer, nil) (defaults to: nil)

    Minimum number of items.

  • max_items (Integer, nil) (defaults to: nil)

    Maximum number of items.

  • unique_items (Boolean, nil) (defaults to: nil)

    Whether items must be unique.

Returns:

  • (Hash)

    A JSON Schema hash for an array type.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ruby_pi/tools/schema.rb', line 115

def array(description: nil, required: false, items: nil,
          min_items: nil, max_items: nil, unique_items: nil)
  schema = { type: "array" }
  schema[:description] = description if description
  # Strip internal _required flag from item schemas if present
  schema[:items] = sanitize(items) if items
  schema[:minItems] = min_items if min_items
  schema[:maxItems] = max_items if max_items
  schema[:uniqueItems] = unique_items unless unique_items.nil?
  schema[:_required] = true if required
  schema
end

.boolean(description = nil, required: false) ⇒ Hash

Builds a JSON Schema for a boolean property.

Parameters:

  • description (String) (defaults to: nil)

    Human-readable description.

  • required (Boolean) (defaults to: false)

    Whether this property is required.

Returns:

  • (Hash)

    A JSON Schema hash for a boolean type.



99
100
101
102
103
104
# File 'lib/ruby_pi/tools/schema.rb', line 99

def boolean(description = nil, required: false)
  schema = { type: "boolean" }
  schema[:description] = description if description
  schema[:_required] = true if required
  schema
end

.integer(description = nil, required: false, minimum: nil, maximum: nil, enum: nil) ⇒ Hash

Builds a JSON Schema for an integer property.

Parameters:

  • description (String) (defaults to: nil)

    Human-readable description.

  • required (Boolean) (defaults to: false)

    Whether this property is required.

  • minimum (Integer, nil) (defaults to: nil)

    Minimum value (inclusive).

  • maximum (Integer, nil) (defaults to: nil)

    Maximum value (inclusive).

  • enum (Array<Integer>, nil) (defaults to: nil)

    Allowed integer values.

Returns:

  • (Hash)

    A JSON Schema hash for an integer type.



66
67
68
69
70
71
72
73
74
# File 'lib/ruby_pi/tools/schema.rb', line 66

def integer(description = nil, required: false, minimum: nil, maximum: nil, enum: nil)
  schema = { type: "integer" }
  schema[:description] = description if description
  schema[:minimum] = minimum if minimum
  schema[:maximum] = maximum if maximum
  schema[:enum] = enum if enum
  schema[:_required] = true if required
  schema
end

.number(description = nil, required: false, minimum: nil, maximum: nil, enum: nil) ⇒ Hash

Builds a JSON Schema for a number (float/decimal) property.

Parameters:

  • description (String) (defaults to: nil)

    Human-readable description.

  • required (Boolean) (defaults to: false)

    Whether this property is required.

  • minimum (Numeric, nil) (defaults to: nil)

    Minimum value (inclusive).

  • maximum (Numeric, nil) (defaults to: nil)

    Maximum value (inclusive).

  • enum (Array<Numeric>, nil) (defaults to: nil)

    Allowed numeric values.

Returns:

  • (Hash)

    A JSON Schema hash for a number type.



84
85
86
87
88
89
90
91
92
# File 'lib/ruby_pi/tools/schema.rb', line 84

def number(description = nil, required: false, minimum: nil, maximum: nil, enum: nil)
  schema = { type: "number" }
  schema[:description] = description if description
  schema[:minimum] = minimum if minimum
  schema[:maximum] = maximum if maximum
  schema[:enum] = enum if enum
  schema[:_required] = true if required
  schema
end

.object(**properties) ⇒ Hash

Builds a JSON Schema for an object with named properties.

Each keyword argument key is a property name, and its value is a schema hash produced by one of the other builder methods (‘.string`, `.integer`, etc.). Properties marked with `required: true` are collected into the top-level “required” array.

Parameters:

  • properties (Hash{Symbol => Hash})

    Property name to schema mappings.

Returns:

  • (Hash)

    A JSON Schema hash for an object type.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ruby_pi/tools/schema.rb', line 137

def object(**properties)
  required_fields = []
  cleaned_properties = {}

  properties.each do |name, prop_schema|
    # Extract and consume the internal _required flag
    if prop_schema[:_required]
      required_fields << name.to_s
    end
    # Store a sanitized copy (without _required) under the property name
    cleaned_properties[name] = sanitize(prop_schema)
  end

  schema = {
    type: "object",
    properties: cleaned_properties
  }
  schema[:required] = required_fields unless required_fields.empty?
  schema
end

.string(description = nil, required: false, enum: nil, format: nil, min_length: nil, max_length: nil, pattern: nil) ⇒ Hash

Builds a JSON Schema for a string property.

Parameters:

  • description (String) (defaults to: nil)

    Human-readable description of the property.

  • required (Boolean) (defaults to: false)

    Whether this property is required (used by ‘.object`).

  • enum (Array<String>, nil) (defaults to: nil)

    Allowed values for the string.

  • format (String, nil) (defaults to: nil)

    JSON Schema format hint (e.g. “date-time”, “email”).

  • min_length (Integer, nil) (defaults to: nil)

    Minimum string length.

  • max_length (Integer, nil) (defaults to: nil)

    Maximum string length.

  • pattern (String, nil) (defaults to: nil)

    Regex pattern the string must match.

Returns:

  • (Hash)

    A JSON Schema hash for a string type.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_pi/tools/schema.rb', line 45

def string(description = nil, required: false, enum: nil, format: nil,
           min_length: nil, max_length: nil, pattern: nil)
  schema = { type: "string" }
  schema[:description] = description if description
  schema[:enum] = enum if enum
  schema[:format] = format if format
  schema[:minLength] = min_length if min_length
  schema[:maxLength] = max_length if max_length
  schema[:pattern] = pattern if pattern
  schema[:_required] = true if required
  schema
end