Module: Firecrawlrb::Schema

Defined in:
lib/firecrawlrb.rb

Constant Summary collapse

TYPE_MAPPING =
{
  string: "string",
  number: "number",
  float: "number",
  decimal: "number",
  integer: "integer",
  int: "integer",
  boolean: "boolean",
  bool: "boolean",
  array: "array",
  object: "object"
}.freeze

Class Method Summary collapse

Class Method Details

.normalize(input) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/firecrawlrb.rb', line 31

def self.normalize(input)
  case input
  when Hash
    # If it already looks like a raw JSON Schema (has :type or "type" key)
    if input.key?(:type) || input.key?("type") || input.key?(:properties) || input.key?("properties")
      input
    else
      properties = {}
      input.each do |key, value|
        properties[key.to_s] = normalize(value)
      end
      {
        type: "object",
        required: [],
        properties: properties
      }
    end
  when Array
    if input.empty?
      { type: "array", items: { type: "string" } }
    else
      { type: "array", items: normalize(input.first) }
    end
  when Symbol, String
    type_str = input.to_s.downcase.to_sym
    json_type = TYPE_MAPPING[type_str] || input.to_s
    { type: json_type }
  else
    { type: "string" }
  end
end