Class: Ollama::SchemaValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/schema_validator.rb

Overview

JSON schema validation for structured output.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ SchemaValidator

Returns a new instance of SchemaValidator.



20
21
22
23
24
25
# File 'lib/ollama/schema_validator.rb', line 20

def initialize(schema)
  @required = Array(schema["required"])
  @properties = schema["properties"] || {}
  @additional_properties = schema.fetch("additionalProperties", false)
  @type = schema["type"]
end

Class Method Details

.validate!(data, schema) ⇒ Object

Parameters:

  • data (Hash)
  • schema (Hash)

Raises:



11
12
13
14
15
16
17
18
# File 'lib/ollama/schema_validator.rb', line 11

def self.validate!(data, schema)
  return data unless schema.is_a?(Hash)
  return data if schema.empty?

  new(schema).validate!(data)
rescue JSON::ParserError => e
  raise SchemaViolationError, "Invalid schema: #{e.message}"
end

Instance Method Details

#validate!(data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/ollama/schema_validator.rb', line 27

def validate!(data)
  raise SchemaViolationError, "Expected object, got #{data.class}" unless data.is_a?(Hash)
  raise SchemaViolationError, "Expected hash, got nil" if data.nil?

  check_additional_properties!(data)
  check_required!(data)
  check_property_types!(data)

  data
end