Class: SolidAgent::AgentManifest::InputSchema
- Inherits:
-
Object
- Object
- SolidAgent::AgentManifest::InputSchema
- 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.
Instance Attribute Summary collapse
-
#format ⇒ Symbol
Schema format (:picoschema or :json_schema).
-
#schema ⇒ Hash
The schema definition.
Class Method Summary collapse
-
.detect_format(schema) ⇒ Symbol
Detect schema format from content.
-
.from_hash(data) ⇒ InputSchema
Create from various input formats.
Instance Method Summary collapse
-
#field(field_name) ⇒ Hash?
Get field definition.
-
#field_names ⇒ Array<String>
Get all field names.
-
#initialize(schema:, format: :picoschema) ⇒ InputSchema
constructor
A new instance of InputSchema.
-
#required_fields ⇒ Array<String>
Get list of required field names.
-
#to_h ⇒ Hash
Convert to hash representation.
-
#to_json_schema ⇒ Hash
Convert schema to JSON Schema format.
-
#to_picoschema ⇒ Hash
Convert schema to Picoschema format.
-
#valid_input?(input_hash) ⇒ Boolean
Check if input is valid against schema.
-
#validate_input(input_hash) ⇒ Array<String>
Validate input data against this schema.
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
#format ⇒ Symbol
Returns Schema format (:picoschema or :json_schema).
35 36 37 |
# File 'lib/solid_agent/agent_manifest/input_schema.rb', line 35 def format @format end |
#schema ⇒ Hash
Returns 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
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
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
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_names ⇒ Array<String>
Get all field names
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_fields ⇒ Array<String>
Get list of required field names
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_h ⇒ Hash
Convert to hash representation
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_schema ⇒ Hash
Convert schema to JSON Schema format
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_picoschema ⇒ Hash
Convert schema to Picoschema format
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
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.
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.}"] end |