Class: RubyLLM::Contract::SchemaValidator::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/contract/contract/schema_validator/node.rb

Overview

Immutable validation context for one schema node and its current path.

Instance Method Summary collapse

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


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

def array?
  value.is_a?(Array)
end

#array_item(index, item, item_schema) ⇒ Object



64
65
66
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 64

def array_item(index, item, item_schema)
  self.class.new(value: item, schema: item_schema, path: "#{path}[#{index}]")
end

#child(field, child_value, child_schema) ⇒ Object



60
61
62
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 60

def child(field, child_value, child_schema)
  self.class.new(value: child_value, schema: child_schema, path: qualify(field))
end

#expected_typeObject



8
9
10
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 8

def expected_type
  schema[:type]&.to_s
end

#extra_keysObject



52
53
54
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 52

def extra_keys
  value.keys.map(&:to_s)
end

#field_value(field) ⇒ Object



45
46
47
48
49
50
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 45

def field_value(field)
  symbolized = field.to_sym
  return value[symbolized] if value.key?(symbolized)

  value[field.to_s]
end

#hash?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 16

def hash?
  value.is_a?(Hash)
end

#items_schemaObject



36
37
38
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 36

def items_schema
  schema[:items]
end

#key_present?(field) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 40

def key_present?(field)
  symbolized = field.to_sym
  value.key?(symbolized) || value.key?(field.to_s)
end

#numeric?Boolean

Returns:

  • (Boolean)


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

def numeric?
  value.is_a?(Numeric)
end

#object_schema?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 12

def object_schema?
  expected_type == "object" || schema.key?(:properties)
end

#propertiesObject



28
29
30
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 28

def properties
  schema[:properties] || {}
end

#qualify(field) ⇒ Object



56
57
58
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 56

def qualify(field)
  path ? "#{path}.#{field}" : field.to_s
end

#required_fieldsObject



32
33
34
# File 'lib/ruby_llm/contract/contract/schema_validator/node.rb', line 32

def required_fields
  Array(schema[:required]).map(&:to_s)
end