Module: RubyLLM::RedCandle::SchemaValidator
- Defined in:
- lib/ruby_llm/red_candle/schema_validator.rb
Overview
Validates JSON schemas for structured generation
Class Method Summary collapse
-
.valid?(schema) ⇒ Boolean
Check if a schema is valid without raising.
-
.validate(schema) ⇒ Array<String>
Validate a schema and return any errors.
-
.validate!(schema) ⇒ true
Validate a schema for structured generation.
Class Method Details
.valid?(schema) ⇒ Boolean
Check if a schema is valid without raising
71 72 73 |
# File 'lib/ruby_llm/red_candle/schema_validator.rb', line 71 def valid?(schema) validate(schema).empty? end |
.validate(schema) ⇒ Array<String>
Validate a schema and return any errors
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 |
# File 'lib/ruby_llm/red_candle/schema_validator.rb', line 25 def validate(schema) errors = [] # Check schema is a Hash unless schema.is_a?(Hash) errors << "Schema must be a Hash, got #{schema.class.name}" return errors # Can't continue validation end # Check type is "object" schema_type = schema[:type] || schema["type"] if schema_type.nil? errors << "Schema must have a 'type' field" elsif schema_type.to_s != "object" errors << "Schema type must be 'object' for structured generation, got '#{schema_type}'" end # Check properties exist and are valid properties = schema[:properties] || schema["properties"] if properties.nil? errors << "Schema must have a 'properties' field defining the expected output structure" elsif !properties.is_a?(Hash) errors << "Schema 'properties' must be a Hash, got #{properties.class.name}" elsif properties.empty? errors << "Schema 'properties' must define at least one property" else # Validate each property has a type properties.each do |key, value| unless value.is_a?(Hash) errors << "Property '#{key}' must be a Hash with at least a 'type' field" next end prop_type = value[:type] || value["type"] if prop_type.nil? errors << "Property '#{key}' must have a 'type' field" end end end errors end |
.validate!(schema) ⇒ true
Validate a schema for structured generation
12 13 14 15 16 17 18 19 20 |
# File 'lib/ruby_llm/red_candle/schema_validator.rb', line 12 def validate!(schema) errors = validate(schema) return true if errors.empty? raise RubyLLM::Error.new( nil, (errors, schema) ) end |