Sha256: 60122d7fed2c7c45c02a9f6681448250312c9add060a487454147aca068ccae6
Contents?: true
Size: 1.68 KB
Versions: 1
Compression:
Stored size: 1.68 KB
Contents
require "mountapi/error/invalid_parameter" module Mountapi module Schema # Mixin for schema behaviour module Base def self.included(base) base.class_eval do extend ClassMethods include InstanceMethods extend Forwardable attr_reader :open_api_schema def_delegators :open_api_schema, :type, :required end end # class extensions module ClassMethods # @param [String] key_name # @param [Object] value # # @return [Hash{String => Object}] # # For a given key name , transform value to json schema # Override this method for custom value transformation def to_json_schema(key_name, value) { key_name => value } end end module InstanceMethods # @param [OpenApi3Parser::Schema] open_api_schema def initialize(open_api_schema) @open_api_schema = open_api_schema end def default open_api_schema.default end # @return [Hash] the json-schema specification 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 def raise_cast_error(value) raise Error::InvalidParameter.new([{message: "can't cast #{value.inspect} as #{self.class.name}"}]) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mountapi-0.11.1 | lib/mountapi/schema/base.rb |