File: validations.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (59 lines) | stat: -rw-r--r-- 1,778 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true

require 'active_support/concern'

module Grape
  module DSL
    module Validations
      extend ActiveSupport::Concern

      include Grape::DSL::Configuration

      module ClassMethods
        # Clears all defined parameters and validations. The main purpose of it is to clean up
        # settings, so next endpoint won't interfere with previous one.
        #
        #    params do
        #      # params for the endpoint below this block
        #    end
        #    post '/current' do
        #      # whatever
        #    end
        #
        #    # somewhere between them the reset_validations! method gets called
        #
        #    params do
        #      # params for the endpoint below this block
        #    end
        #    post '/next' do
        #      # whatever
        #    end
        def reset_validations!
          unset_namespace_stackable :declared_params
          unset_namespace_stackable :validations
          unset_namespace_stackable :params
          unset_description_field :params
        end

        # Opens a root-level ParamsScope, defining parameter coercions and
        # validations for the endpoint.
        # @yield instance context of the new scope
        def params(&block)
          Grape::Validations::ParamsScope.new(api: self, type: Hash, &block)
        end

        def document_attribute(names, opts)
          setting = description_field(:params)
          setting ||= description_field(:params, {})
          Array(names).each do |name|
            full_name = name[:full_name].to_s
            setting[full_name] ||= {}
            setting[full_name].merge!(opts)

            namespace_stackable(:params, full_name => opts)
          end
        end
      end
    end
  end
end