Class: JSON::SchemaDsl::Entity

Inherits:
Dry::Struct
  • Object
show all
Includes:
AstNode
Defined in:
lib/json/schema_dsl/entity.rb

Overview

The basic entity type for json schemas.

This is mostly used in cases where you don’t exactly know what type a property will have, for example if the property is an ‘anyOf` of different types.

Internally it is used as the superclass of all other types.

Direct Known Subclasses

Array, Boolean, Integer, Null, Numeric, Object, String

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AstNode

included, #render, #update

Class Method Details

.required_typeObject

nodoc



16
17
18
# File 'lib/json/schema_dsl/entity.rb', line 16

def required_type
  (Types::Bool | Types::Coercible::Array.of(Types::Coercible::String).default { [] })
end

Instance Method Details

#to_hHash<Symbol, Object>

Returns this entity as a hash and all children and properties as simple values. This structure is used to render the eventual schema by the renderer.

Returns:

  • (Hash<Symbol, Object>)

    Returns this entity as a hash and all children and properties as simple values. This structure is used to render the eventual schema by the renderer.

See Also:

  • Rederer#initialize


41
42
43
44
45
46
47
48
49
50
# File 'lib/json/schema_dsl/entity.rb', line 41

def to_h
  super.transform_values do |v|
    is_array = v.is_a?(::Array)
    if (is_array ? v.first : v).respond_to?(:to_h)
      is_array ? v.map(&:to_h) : v.to_h
    else
      v
    end
  end
end