Class: A2A::Schema::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/schema/definition.rb

Overview

Base class for schema-validated A2A protocol objects.

Each A2A definition type (Agent Card, Agent Capabilities, Task, etc.) gets a dynamically-generated subclass of Definition with:

- A JSONSchemer sub-schema attached (.schema)
- Reader methods for each property (snake_case)
- Validation via .valid? / .valid!

caps = A2A::Schema["Agent Capabilities"].new(
  streaming: true,
  push_notifications: false
)
caps.valid?             #=> true
caps.streaming          #=> true
caps.push_notifications #=> false
caps.to_h               #=> { "streaming" => true, "pushNotifications" => false }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Definition

Returns a new instance of Definition.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/a2a/schema/definition.rb', line 27

def initialize(hash = {})
  props    = self.class.schema_properties
  snake    = self.class.snake_to_camel_map
  refs     = self.class.property_refs
  @data    = {}

  hash.each do |key, value|
    k = key.to_s

    # Resolve snake_case input to camelCase storage key
    camel = snake[k] || k

    if props.include?(camel)
      @data[camel] = if value.is_a?(Definition)
        value.to_h
      elsif (ref_info = refs[camel])
        wrap_ref(value, ref_info)
      else
        value
      end
    end
  end
end

Class Method Details

.definition_nameObject



57
58
59
# File 'lib/a2a/schema/definition.rb', line 57

def self.definition_name
  raise "A2A::Schema::Definition should NOT be instantiated directly"
end

.property_refsObject



69
70
71
# File 'lib/a2a/schema/definition.rb', line 69

def self.property_refs
  raise "A2A::Schema::Definition should NOT be instantiated directly"
end

.schemaObject

— class methods overridden by the factory ———————–



53
54
55
# File 'lib/a2a/schema/definition.rb', line 53

def self.schema
  raise "A2A::Schema::Definition should NOT be instantiated directly"
end

.schema_propertiesObject



61
62
63
# File 'lib/a2a/schema/definition.rb', line 61

def self.schema_properties
  raise "A2A::Schema::Definition should NOT be instantiated directly"
end

.snake_to_camel_mapObject



65
66
67
# File 'lib/a2a/schema/definition.rb', line 65

def self.snake_to_camel_map
  raise "A2A::Schema::Definition should NOT be instantiated directly"
end

Instance Method Details

#==(other) ⇒ Object



98
99
100
# File 'lib/a2a/schema/definition.rb', line 98

def ==(other)
  other.is_a?(Definition) && to_h == other.to_h
end

#inspectObject



102
103
104
# File 'lib/a2a/schema/definition.rb', line 102

def inspect
  "#<#{self.class.definition_name} #{to_h.inspect}>"
end

#to_hObject

Returns the data as a plain Hash with camelCase string keys, matching the JSON wire format. Nested Definition instances are auto-coerced via deep_compact.



94
95
96
# File 'lib/a2a/schema/definition.rb', line 94

def to_h
  deep_compact(@data)
end

#valid!Object

Raises:



79
80
81
82
83
84
85
86
87
# File 'lib/a2a/schema/definition.rb', line 79

def valid!
  errors = self.class.schema.validate(to_h).to_a
  return true if errors.empty?

  raise ValidationError.new(errors,
    definition_name: self.class.definition_name,
    data: to_h
  )
end

#valid?Boolean

— validation —————————————————-

Returns:

  • (Boolean)


75
76
77
# File 'lib/a2a/schema/definition.rb', line 75

def valid?
  self.class.schema.valid?(to_h)
end