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

Constructor Details

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

Returns a new instance of SchemaDefinition.



24
25
26
27
28
29
30
# File 'lib/easy_talk/schema_definition.rb', line 24

def initialize(name, schema = {})
  @schema = schema.dup
  @schema[:additional_properties] = EasyTalk.configuration.default_additional_properties unless @schema.key?(:additional_properties)
  @name = name
  @klass = nil # Initialize klass to nil
  @property_naming_strategy = EasyTalk.configuration.property_naming_strategy
end

Instance Attribute Details

#klassObject

Add accessor for the model class



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

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

Instance Method Details

#compose(*subschemas) ⇒ Object



47
48
49
50
# File 'lib/easy_talk/schema_definition.rb', line 47

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

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/easy_talk/schema_definition.rb', line 82

def nullable_optional_property(name, type, constraints = {})
  # 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)


76
77
78
# File 'lib/easy_talk/schema_definition.rb', line 76

def optional?
  @schema[:optional]
end

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



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/easy_talk/schema_definition.rb', line 53

def property(name, type, constraints = {}, &block)
  validate_property_name(name)
  constraints[:as] ||= @property_naming_strategy.call(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

#property_naming_strategy(strategy) ⇒ Object



98
99
100
# File 'lib/easy_talk/schema_definition.rb', line 98

def property_naming_strategy(strategy)
  @property_naming_strategy = EasyTalk::NamingStrategies.derive_strategy(strategy)
end

#validate_property_name(name) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/easy_talk/schema_definition.rb', line 67

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

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