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
# File 'lib/a2a/schema/definition.rb', line 27

def initialize(hash = {})
  props    = self.class.schema_properties
  snake    = self.class.snake_to_camel_map
  @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] = value.is_a?(Definition) ? value.to_h : value
    end
  end
end

Class Method Details

.definition_nameObject



50
51
52
# File 'lib/a2a/schema/definition.rb', line 50

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

.schemaObject

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



46
47
48
# File 'lib/a2a/schema/definition.rb', line 46

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

.schema_propertiesObject



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

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

.snake_to_camel_mapObject



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

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

Instance Method Details

#==(other) ⇒ Object



87
88
89
# File 'lib/a2a/schema/definition.rb', line 87

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

#inspectObject



91
92
93
# File 'lib/a2a/schema/definition.rb', line 91

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.



83
84
85
# File 'lib/a2a/schema/definition.rb', line 83

def to_h
  deep_compact(@data)
end

#valid!Object

Raises:



68
69
70
71
72
73
74
75
76
# File 'lib/a2a/schema/definition.rb', line 68

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)


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

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