Class: EasyTalk::SchemaDefinition

Inherits:
Object
  • Object
show all
Extended by:
T::AllOf, T::AnyOf, T::OneOf, T::Sig
Defined in:
lib/easy_talk/schema_definition.rb

Overview

EasyTalk SchemaDefinition

SchemaDefinition provides the methods for defining a schema within the define_schema block. The @schema is a hash that contains the unvalidated schema definition for the model. A SchemaDefinition instanace is the passed to the Builder.build_schema method to validate and compile the schema.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from T::AnyOf

[]

Methods included from T::OneOf

[]

Methods included from T::AllOf

[]

Constructor Details

#initialize(name, schema = {}) ⇒ SchemaDefinition

Returns a new instance of SchemaDefinition.



20
21
22
23
24
# File 'lib/easy_talk/schema_definition.rb', line 20

def initialize(name, schema = {})
  @schema = schema
  @schema[:additional_properties] = false unless schema.key?(:additional_properties)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/easy_talk/schema_definition.rb', line 18

def name
  @name
end

#schemaObject (readonly)

Returns the value of attribute schema.



18
19
20
# File 'lib/easy_talk/schema_definition.rb', line 18

def schema
  @schema
end

Instance Method Details

#compose(*subschemas) ⇒ Object



32
33
34
35
# File 'lib/easy_talk/schema_definition.rb', line 32

def compose(*subschemas)
  @schema[:subschemas] ||= []
  @schema[:subschemas] += subschemas
end

#nullable_optional_property(name, type, constraints = {}, &blk) ⇒ Object

Helper method for nullable and optional properties



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/easy_talk/schema_definition.rb', line 63

def nullable_optional_property(name, type, constraints = {}, &blk)
  # Ensure type is nilable
  nilable_type = if type.respond_to?(:nilable?) && type.nilable?
                   type
                 else
                   T.nilable(type)
                 end

  # Ensure constraints include optional: true
  constraints = constraints.merge(optional: true)

  # Call standard property method
  property(name, nilable_type, constraints)
end

#optional?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/easy_talk/schema_definition.rb', line 58

def optional?
  @schema[:optional]
end

#property(name, type, constraints = {}, &blk) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/easy_talk/schema_definition.rb', line 40

def property(name, type, constraints = {}, &blk)
  validate_property_name(name)
  @schema[:properties] ||= {}

  if block_given?
    raise ArgumentError, 'Block-style sub-schemas are no longer supported. Use class references as types instead.'
  end

  @schema[:properties][name] = { type:, constraints: }
end

#validate_property_name(name) ⇒ Object



51
52
53
54
55
56
# File 'lib/easy_talk/schema_definition.rb', line 51

def validate_property_name(name)
  return if name.to_s.match?(/^[A-Za-z_][A-Za-z0-9_]*$/)

  raise InvalidPropertyNameError,
        "Invalid property name '#{name}'. Must start with letter/underscore and contain only letters, numbers, underscores"
end