Skip to content

Commit 79435d6

Browse files
committed
allow passing documentation to requires/optional
The documentation information are inserted into @last_description hash if defined (you need to call desc "xxxx" before defining parameters)
1 parent 4800c93 commit 79435d6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lib/grape/validations.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ def reset_validations!
8181
settings[:validations] = []
8282
end
8383

84+
def document_attribute(names, opts)
85+
if @last_description
86+
@last_description[:params] ||= {}
87+
88+
Array(names).each do |name|
89+
@last_description[:params][name.to_sym] ||= {}
90+
@last_description[:params][name.to_sym].merge!(opts)
91+
end
92+
end
93+
end
94+
8495
def requires(*attrs)
8596
validations = {:presence => true}
8697
if attrs.last.is_a?(Hash)
@@ -100,6 +111,18 @@ def optional(*attrs)
100111
end
101112

102113
def validates(attrs, validations)
114+
doc_attrs = { :required => validations.keys.include?(:presence) }
115+
116+
if coerce_type = validations[:coerce]
117+
doc_attrs[:type] = coerce_type.to_s
118+
end
119+
120+
if desc = validations.delete(:desc)
121+
doc_attrs[:desc] = desc
122+
end
123+
124+
document_attribute(attrs, doc_attrs)
125+
103126
validations.each do |type, options|
104127
validator_class = Validations::validators[type]
105128
if validator_class

0 commit comments

Comments
 (0)