Sha256: db5b65f534611ab33eb922531bdce712dc3446a9dc413203d8a69d3df8501f3e

Contents?: true

Size: 834 Bytes

Versions: 1

Compression:

Stored size: 834 Bytes

Contents

require "mountapi/schema"

module Mountapi
  module Route
    # Route parameter model
    # used to carry schema definition
    class Parameter
      attr_reader :name, :options, :location

      def initialize(name, location, options = OpenStruct.new)
        @name = name
        @location = location
        @options = options
      end

      def required
        options.required?
      end

      # @return [Mountapi::Schema]
      def schema
        @schema ||= Mountapi::Schema.build(options.schema)
      end

      def json_schema
        @json_schema ||= schema.to_json_schema
      end

      def cast(value)
        json_schema["nullable"] && value.nil? ? value : schema.cast(value)
      end

      def default
        schema.default
      end

      def self.build(*args)
        new(*args)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mountapi-0.11.1 lib/mountapi/route/parameter.rb