2
2
3
3
module Grape
4
4
module DSL
5
+ # Defines DSL methods, meant to be applied to a ParamsScope, which define
6
+ # and describe the parameters accepted by an endpoint, or all endpoints
7
+ # within a namespace.
5
8
module Parameters
6
9
extend ActiveSupport ::Concern
7
10
@@ -39,6 +42,47 @@ def use(*names)
39
42
alias_method :use_scope , :use
40
43
alias_method :includes , :use
41
44
45
+ # Require one or more parameters for the current endpoint.
46
+ #
47
+ # @param attrs list of parameter names, or, if :using is
48
+ # passed as an option, which keys to include (:all or :none) from
49
+ # the :using hash. The last key can be a hash, which specifies
50
+ # options for the parameters
51
+ # @option attrs :type [Class] the type to coerce this parameter to before
52
+ # passing it to the endpoint. See Grape::ParameterTypes for supported
53
+ # types, or use a class that defines `::parse` as a custom type
54
+ # @option attrs :desc [String] description to document this parameter
55
+ # @option attrs :default [Object] default value, if parameter is optional
56
+ # @option attrs :values [Array] permissable values for this field. If any
57
+ # other value is given, it will be handled as a validation error
58
+ # @option attrs :using [Hash[Symbol => Hash]] a hash defining keys and
59
+ # options, like that returned by Grape::Entity#documentation. The value
60
+ # of each key is an options hash accepting the same parameters
61
+ # @option attrs :except [Array[Symbol]] a list of keys to exclude from
62
+ # the :using Hash. The meaning of this depends on if :all or :none was
63
+ # passed; :all + :except will make the :except fields optional, whereas
64
+ # :none + :except will make the :except fields required
65
+ #
66
+ # @example
67
+ #
68
+ # params do
69
+ # # Basic usage: require a parameter of a certain type
70
+ # requires :user_id, type: Integer
71
+ #
72
+ # # You don't need to specify type; String is default
73
+ # requires :foo
74
+ #
75
+ # # Multiple params can be specified at once if they share
76
+ # # the same options.
77
+ # requires :x, :y, :z, type: Date
78
+ #
79
+ # # Nested parameters can be handled as hashes. You must
80
+ # # pass in a block, within which you can use any of the
81
+ # # parameters DSL methods.
82
+ # requires :user, type: Hash do
83
+ # requires :name, type: String
84
+ # end
85
+ # end
42
86
def requires ( *attrs , &block )
43
87
orig_attrs = attrs . clone
44
88
@@ -55,6 +99,10 @@ def requires(*attrs, &block)
55
99
end
56
100
end
57
101
102
+ # Allow, but don't require, one or more parameters for the current
103
+ # endpoint.
104
+ # @param (see #requires)
105
+ # @option (see #requires)
58
106
def optional ( *attrs , &block )
59
107
orig_attrs = attrs . clone
60
108
@@ -77,24 +125,35 @@ def optional(*attrs, &block)
77
125
end
78
126
end
79
127
128
+ # Disallow the given parameters to be present in the same request.
129
+ # @param attrs [*Symbol] parameters to validate
80
130
def mutually_exclusive ( *attrs )
81
131
validates ( attrs , mutual_exclusion : true )
82
132
end
83
133
134
+ # Require exactly one of the given parameters to be present.
135
+ # @param (see #mutually_exclusive)
84
136
def exactly_one_of ( *attrs )
85
137
validates ( attrs , exactly_one_of : true )
86
138
end
87
139
140
+ # Require at least one of the given parameters to be present.
141
+ # @param (see #mutually_exclusive)
88
142
def at_least_one_of ( *attrs )
89
143
validates ( attrs , at_least_one_of : true )
90
144
end
91
145
146
+ # Require that either all given params are present, or none are.
147
+ # @param (see #mutually_exclusive)
92
148
def all_or_none_of ( *attrs )
93
149
validates ( attrs , all_or_none_of : true )
94
150
end
95
151
96
152
alias_method :group , :requires
97
153
154
+ # @param params [Hash] initial hash of parameters
155
+ # @return hash of parameters relevant for the current scope
156
+ # @api private
98
157
def params ( params )
99
158
params = @parent . params ( params ) if @parent
100
159
if @element
0 commit comments