Class: Mountapi::Schema::Object

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/mountapi/schema/object.rb

Overview

Schema implementation for object

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

included

Class Method Details

.to_json_schema(key_name, value) ⇒ Object

Override the Base method to deep validates properties

Parameters:



27
28
29
30
31
32
33
34
# File 'lib/mountapi/schema/object.rb', line 27

def self.to_json_schema(key_name, value)
  if key_name == "properties"
    value = value.inject({}) do |acc, (name, schema)|
      acc.merge({name => Schema.build(schema).to_json_schema})
    end
  end
  {key_name => value}
end

Instance Method Details

#cast(value) ⇒ Object

For object we also deep cast properties

Parameters:

  • value (Object)

    the inbound value for this schema



12
13
14
15
16
17
18
19
20
21
# File 'lib/mountapi/schema/object.rb', line 12

def cast(value)
  raise_cast_error(value) unless value.is_a?(Hash)
  open_api_schema.properties.inject({}) do |acc, (k,val)|
    if value.has_key?(k)
      acc.merge(k.to_sym => Schema.build(val).cast(value[k]))
    else
      acc
    end
  end
end