Module: Mountapi::Schema::Base::InstanceMethods

Defined in:
lib/mountapi/schema/base.rb

Instance Method Summary collapse

Instance Method Details

#defaultObject



38
39
40
# File 'lib/mountapi/schema/base.rb', line 38

def default
  open_api_schema.default
end

#initialize(open_api_schema) ⇒ Object

Parameters:

  • open_api_schema (OpenApi3Parser::Schema)


34
35
36
# File 'lib/mountapi/schema/base.rb', line 34

def initialize(open_api_schema)
  @open_api_schema = open_api_schema
end

#raise_cast_error(value) ⇒ Object



59
60
61
# File 'lib/mountapi/schema/base.rb', line 59

def raise_cast_error(value)
  raise Error::InvalidParameter.new([{message: "can't cast #{value.inspect} as #{self.class.name}"}])
end

#to_json_schemaHash

Returns the json-schema specification.

Returns:

  • (Hash)

    the json-schema specification



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mountapi/schema/base.rb', line 43

def to_json_schema
  schema = open_api_schema.to_h.inject({}) do |acc, (key, value)|
    if value.nil?
      acc
    else
      acc.merge(self.class.to_json_schema(key.to_s, value))
    end.merge("id" => self.class.name)
  end

  if schema["nullable"]
    schema["type"] = [schema["type"], "null"]
  end

  schema
end