Class: EasyTalk::Builders::ObjectBuilder

Inherits:
BaseBuilder show all
Extended by:
T::Sig
Defined in:
lib/easy_talk/builders/object_builder.rb

Overview

ObjectBuilder is responsible for turning a SchemaDefinition of an “object” type into a validated JSON Schema hash. It:

1) Recursively processes the schema’s :properties, 2) Determines which properties are required (unless optional), 3) Handles sub-schema composition (allOf, anyOf, oneOf, not), 4) Produces the final object-level schema hash.

Constant Summary collapse

VALID_OPTIONS =

Required by BaseBuilder: recognized schema options for “object” types

{
  properties: { type: T::Hash[T.any(Symbol, String), T.untyped], key: :properties },
  additional_properties: { type: T::Boolean, key: :additionalProperties },
  subschemas: { type: T::Array[T.untyped], key: :subschemas },
  required: { type: T::Array[T.any(Symbol, String)], key: :required },
  defs: { type: T.untyped, key: :$defs },
  allOf: { type: T.untyped, key: :allOf },
  anyOf: { type: T.untyped, key: :anyOf },
  oneOf: { type: T.untyped, key: :oneOf },
  not: { type: T.untyped, key: :not }
}.freeze

Constants inherited from BaseBuilder

BaseBuilder::COMMON_OPTIONS

Instance Attribute Summary

Attributes inherited from BaseBuilder

#name, #options, #schema

Instance Method Summary collapse

Methods inherited from BaseBuilder

#build, collection_type?

Constructor Details

#initialize(schema_definition) ⇒ ObjectBuilder

Returns a new instance of ObjectBuilder.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/easy_talk/builders/object_builder.rb', line 32

def initialize(schema_definition)
  # Keep a reference to the original schema definition
  @schema_definition = schema_definition
  # Duplicate the raw schema hash so we can mutate it safely
  @original_schema = schema_definition.schema.dup

  # We'll collect required property names in this Set
  @required_properties = Set.new

  # Usually the name is a string (class name). Fallback to :klass if nil.
  name_for_builder = schema_definition.name ? schema_definition.name.to_sym : :klass

  # Build the base structure: { type: 'object' } plus any top-level options
  super(
    name_for_builder,
    { type: 'object' },    # minimal "object" structure
    build_options_hash,    # method below merges & cleans final top-level keys
    VALID_OPTIONS
  )
end