Class: Shale::Schema::JSONGenerator::Schema Private

Inherits:
Object
  • Object
show all
Defined in:
lib/shale/schema/json_generator/schema.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class representing JSON schema

Constant Summary collapse

DIALECT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

JSON Schema dialect (aka version)

'https://json-schema.org/draft/2020-12/schema'

Instance Method Summary collapse

Constructor Details

#initialize(types, id: nil, title: nil, description: nil) ⇒ Schema

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize Schema object

Parameters:



21
22
23
24
25
26
# File 'lib/shale/schema/json_generator/schema.rb', line 21

def initialize(types, id: nil, title: nil, description: nil)
  @types = types
  @id = id
  @title = title
  @description = description
end

Instance Method Details

#as_jsonHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return JSON schema as Ruby Hash

Examples:

Shale::Schema::JSONGenerator::Schema.new(types).as_json

Returns:

  • (Hash)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/shale/schema/json_generator/schema.rb', line 36

def as_json
  schema = {
    '$schema' => DIALECT,
    '$id' => @id,
    'title' => @title,
    'description' => @description,
  }

  unless @types.empty?
    root = @types.first
    root.nullable = false

    schema['$ref'] = "#/$defs/#{root.name}"
    schema['$defs'] = @types
      .sort { |a, b| a.name <=> b.name }
      .to_h { |e| [e.name, e.as_json] }
  end

  schema.compact
end