Class: Dry::Schema::JSON
- Inherits:
-
Object
- Object
- Dry::Schema::JSON
- Defined in:
- lib/last_llm/extensions/dry_schema_extensions.rb
Overview
Extensions to Dry::Schema::JSON for JSON Schema conversion
Instance Method Summary collapse
-
#json_schema ⇒ Hash
Convert the schema to a JSON schema hash.
Instance Method Details
#json_schema ⇒ Hash
Convert the schema to a JSON schema hash
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/last_llm/extensions/dry_schema_extensions.rb', line 10 def json_schema # Extract schema information json_schema = { type: 'object', properties: {}, required: [] } # Process each rule in the schema rules.each_value do |rule| # Skip if rule is not a proper rule object next unless rule.respond_to?(:name) property_name = rule.name.to_s property_def = {} # Determine if the property is required json_schema[:required] << property_name if rule.is_a?(Dry::Schema::Rule::Required) # Determine the property type if rule.respond_to?(:type) && rule.type.is_a?(Dry::Types::Nominal) case rule.type.primitive when String property_def['type'] = 'string' when Integer property_def['type'] = 'integer' when Float property_def['type'] = 'number' when TrueClass, FalseClass property_def['type'] = 'boolean' when Array property_def['type'] = 'array' # Try to determine the item type property_def['items'] = if rule.type.respond_to?(:member) && rule.type.member.respond_to?(:primitive) case rule.type.member.primitive when String { 'type' => 'string' } when Integer { 'type' => 'integer' } when Float { 'type' => 'number' } when TrueClass, FalseClass { 'type' => 'boolean' } when Hash { 'type' => 'object' } else {} end else {} end when Hash property_def['type'] = 'object' # For nested objects, we'd need a more sophisticated approach else property_def['type'] = 'string' # Default to string end end json_schema[:properties][property_name] = property_def end json_schema end |