Sha256: 1bbb8046faa343400e3d286da0b8cacf680c981fd59cfa761facc47fc2d3b44d

Contents?: true

Size: 997 Bytes

Versions: 1

Compression:

Stored size: 997 Bytes

Contents

require "mountapi/schema/base"

module Mountapi
  module Schema
    # Schema implementation for object
    class Object
      include Base

      # @param [Object] value the inbound value for this schema
      #
      # For object we also deep cast properties
      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

      # @param [String] key_name
      # @param [Object] value
      #
      # Override the Base method to deep validates properties
      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mountapi-0.11.1 lib/mountapi/schema/object.rb