Class: RubyLLM::Contract::SchemaValidator

Inherits:
Object
  • Object
show all
Includes:
Concerns::DeepSymbolize
Defined in:
lib/ruby_llm/contract/contract/schema_validator.rb

Overview

Client-side validation of parsed output against an output_schema. Checks required fields, enum constraints, number ranges, and nested objects. This complements provider-side enforcement (with_schema) and catches violations when using Test adapter or providers that ignore schemas.

Defined Under Namespace

Classes: FieldCheck

Constant Summary collapse

SIZE_BOUNDS =
{
  string: { min_key: :minLength, max_key: :maxLength, metric: "length" },
  array: { min_key: :minItems, max_key: :maxItems, metric: "array length" }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::DeepSymbolize

#deep_symbolize

Constructor Details

#initialize(parsed_output, schema) ⇒ SchemaValidator

Returns a new instance of SchemaValidator.



24
25
26
27
28
# File 'lib/ruby_llm/contract/contract/schema_validator.rb', line 24

def initialize(parsed_output, schema)
  @output = parsed_output
  @json_schema = extract_schema(schema)
  @errors = []
end

Class Method Details

.validate(parsed_output, schema) ⇒ Object



20
21
22
# File 'lib/ruby_llm/contract/contract/schema_validator.rb', line 20

def self.validate(parsed_output, schema)
  new(parsed_output, schema).validate
end

Instance Method Details

#validateObject



30
31
32
33
34
35
36
37
# File 'lib/ruby_llm/contract/contract/schema_validator.rb', line 30

def validate
  return [] unless @json_schema.is_a?(Hash)

  return validate_non_hash_output unless @output.is_a?(Hash)

  validate_object(@output, @json_schema, prefix: nil)
  @errors
end