Class: Lutaml::Model::Schema::Generator::Definition

Inherits:
Object
  • Object
show all
Includes:
SharedMethods
Defined in:
lib/lutaml/model/schema/generator/definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SharedMethods

#extract_register_from

Constructor Details

#initialize(type) ⇒ Definition

Returns a new instance of Definition.



12
13
14
15
# File 'lib/lutaml/model/schema/generator/definition.rb', line 12

def initialize(type)
  @type = type
  @name = type.name.gsub("::", "_")
end

Instance Attribute Details

#name ⇒ Object (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/lutaml/model/schema/generator/definition.rb', line 10

def name
  @name
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/lutaml/model/schema/generator/definition.rb', line 10

def type
  @type
end

Instance Method Details

#to_schema ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lutaml/model/schema/generator/definition.rb', line 17

def to_schema
  @schema = {
    name => {
      "type" => "object",
      "additionalProperties" => false,
      "properties" => properties_to_schema(type),
    },
  }

  # Choice validation follows the serializer's contract: unset
  # or empty members are omitted from output, and instances that
  # leave a whole group empty are accepted (#869). Members are
  # therefore never `required`; a max:1 group contributes an
  # at-most-one constraint over its members, and independent
  # groups combine conjunctively under allOf. Groups whose range
  # admits several members impose no serialization constraint.
  if type.choice_attributes.any?
    groups = type.choice_attributes
      .select { |choice| choice.max == 1 }
      .map { |choice| { "oneOf" => exclusive_branches(choice) } }

    if groups.one?
      @schema[name].merge!(groups.first)
    elsif groups.any?
      @schema[name]["allOf"] = groups
    end
  end

  @schema
end