class EasyTalk::Property

Property class for building a JSON schema property.

def as_json(*_args)

Returns:
  • (Hash) - The JSON representation of the object

Parameters:
  • _args (Array) -- Optional arguments
def as_json(*_args)
  build.as_json
end

def build

Returns:
  • (Object) - The built property.
def build
  if nilable_type?
    build_nilable_schema
  elsif builder
    args = builder.collection_type? ? [name, type, constraints] : [name, constraints]
    builder.new(*args).build
  elsif type.respond_to?(:schema)
    # merge the top-level constraints from *this* property
    # e.g. :title, :description, :default, etc
    type.schema.merge!(constraints)
  else
    'object'
  end
end

def build_nilable_schema

def build_nilable_schema
  # Extract the non-nil type from the Union
  actual_type = type.types.find { |t| t != NilClass }
  # Create a property with the actual type
  non_nil_schema = Property.new(name, actual_type, constraints).build
  # Merge the types into an array
  non_nil_schema.merge(
    type: [non_nil_schema[:type], 'null']
  )
end

def builder

Returns:
  • (Builder) - The builder associated with the property type.
def builder
  @builder ||= TYPE_TO_BUILDER[type.class.name.to_s] || TYPE_TO_BUILDER[type.name.to_s]
end

def initialize(name, type = nil, constraints = {})

def initialize(name, type = nil, constraints = {})
  @name = name
  @type = type
  @constraints = constraints
  raise ArgumentError, 'property type is missing' if type.blank?
end

def nilable_type?

def nilable_type?
  return unless type.respond_to?(:types)
  return unless type.types.all? { |t| t.respond_to?(:raw_type) }
  type.types.any? { |t| t.raw_type == NilClass }
end