Class: SolidAgent::AgentManifest::InputSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_agent/agent_manifest/input_schema.rb

Overview

InputSchema wraps a schema definition, supporting both Picoschema (compact YAML format) and JSON Schema formats.

Provides bidirectional conversion between formats for interoperability.

Examples:

Picoschema format

schema = InputSchema.new(
  schema: {
    "query" => "string, the search query",
    "limit?" => "integer, max results"
  },
  format: :picoschema
)
schema.to_json_schema  # => JSON Schema object

JSON Schema format

schema = InputSchema.new(
  schema: {
    "type" => "object",
    "properties" => { "query" => { "type" => "string" } }
  },
  format: :json_schema
)
schema.to_picoschema  # => Picoschema object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, format: :picoschema) ⇒ InputSchema

Returns a new instance of InputSchema.



37
38
39
40
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 37

def initialize(schema:, format: :picoschema)
  @schema = schema
  @format = format.to_sym
end

Instance Attribute Details

#formatSymbol

Returns Schema format (:picoschema or :json_schema).

Returns:

  • (Symbol)

    Schema format (:picoschema or :json_schema)



35
36
37
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 35

def format
  @format
end

#schemaHash

Returns The schema definition.

Returns:

  • (Hash)

    The schema definition



32
33
34
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 32

def schema
  @schema
end

Class Method Details

.detect_format(schema) ⇒ Symbol

Detect schema format from content

Parameters:

  • schema (Hash)

    Schema to analyze

Returns:

  • (Symbol)

    Detected format



144
145
146
147
148
149
150
151
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 144

def self.detect_format(schema)
  return :json_schema if schema.nil?
  return :json_schema if schema["type"].present?
  return :json_schema if schema["properties"].present?
  return :json_schema if schema["$schema"].present?

  :picoschema
end

.from_hash(data) ⇒ InputSchema

Create from various input formats

Parameters:

  • data (Hash)

    Schema data

Returns:



133
134
135
136
137
138
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 133

def self.from_hash(data)
  schema = data["schema"] || data[:schema]
  format = data["format"] || data[:format] || detect_format(schema)

  new(schema: schema, format: format)
end

Instance Method Details

#field(field_name) ⇒ Hash?

Get field definition

Parameters:

  • field_name (String)

    Name of the field

Returns:

  • (Hash, nil)

    Field schema definition



124
125
126
127
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 124

def field(field_name)
  json = to_json_schema
  json.dig("properties", field_name.to_s)
end

#field_namesArray<String>

Get all field names

Returns:

  • (Array<String>)


115
116
117
118
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 115

def field_names
  json = to_json_schema
  (json["properties"] || {}).keys
end

#required_fieldsArray<String>

Get list of required field names

Returns:

  • (Array<String>)


107
108
109
110
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 107

def required_fields
  json = to_json_schema
  json["required"] || []
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)


73
74
75
76
77
78
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 73

def to_h
  {
    schema: schema,
    format: format
  }
end

#to_json_schemaHash

Convert schema to JSON Schema format

Returns:

  • (Hash)

    JSON Schema object



45
46
47
48
49
50
51
52
53
54
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 45

def to_json_schema
  case format
  when :picoschema
    Picoschema.to_json_schema(schema)
  when :json_schema
    schema
  else
    raise SchemaError, "Unknown schema format: #{format}"
  end
end

#to_picoschemaHash

Convert schema to Picoschema format

Returns:

  • (Hash)

    Picoschema object



59
60
61
62
63
64
65
66
67
68
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 59

def to_picoschema
  case format
  when :picoschema
    schema
  when :json_schema
    Picoschema.from_json_schema(schema)
  else
    raise SchemaError, "Unknown schema format: #{format}"
  end
end

#valid_input?(input_hash) ⇒ Boolean

Check if input is valid against schema

Parameters:

  • input_hash (Hash)

    Input data to validate

Returns:

  • (Boolean)


100
101
102
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 100

def valid_input?(input_hash)
  validate_input(input_hash).empty?
end

#validate_input(input_hash) ⇒ Array<String>

Validate input data against this schema

Requires the json_schemer gem for validation.

Parameters:

  • input_hash (Hash)

    Input data to validate

Returns:

  • (Array<String>)

    List of validation errors (empty if valid)



86
87
88
89
90
91
92
93
94
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 86

def validate_input(input_hash)
  return [] unless defined?(JSONSchemer)

  json_schema = to_json_schema
  schemer = JSONSchemer.schema(json_schema)
  schemer.validate(input_hash).map { |error| error["error"] }
rescue => e
  ["Schema validation error: #{e.message}"]
end