sinatra-api 1.1.5 → 1.1.7
Sign up to get free protection for your applications and to get access to all the features.
@@ -37,7 +37,8 @@ module Sinatra::API
|
|
37
37
|
value = hash[key]
|
38
38
|
typename = definition[:type]
|
39
39
|
validator = definition[:validator]
|
40
|
-
validator
|
40
|
+
validator = validators[validator] if validator.is_a?(Symbol)
|
41
|
+
validator ||= validators[typename]
|
41
42
|
definition[:coerce] = true unless definition.has_key?(:coerce)
|
42
43
|
|
43
44
|
if validator
|
@@ -51,7 +52,7 @@ module Sinatra::API
|
|
51
52
|
rc = validator.validate(value, definition)
|
52
53
|
# ?
|
53
54
|
else
|
54
|
-
raise
|
55
|
+
raise "Invalid ParameterValidator #{validator.class}, must respond to #call or #validate"
|
55
56
|
end
|
56
57
|
|
57
58
|
if rc.is_a?(String)
|
@@ -178,7 +178,7 @@ module Sinatra::API
|
|
178
178
|
raise ArgumentError, 'API Argument type must be either :required or :optional'
|
179
179
|
end
|
180
180
|
|
181
|
-
if !h.has_key?(name)
|
181
|
+
if !h || !h.has_key?(name)
|
182
182
|
if type == :required
|
183
183
|
halt 400, "Missing required parameter :#{name}"
|
184
184
|
end
|
data/lib/sinatra/api/version.rb
CHANGED
@@ -1,3 +1,37 @@
|
|
1
1
|
describe Sinatra::API::ParameterValidator do
|
2
2
|
include_examples 'integration specs'
|
3
|
+
|
4
|
+
context 'defining parameters' do
|
5
|
+
it 'should define a required parameter using hash style' do
|
6
|
+
app.post '/' do
|
7
|
+
api_required!({
|
8
|
+
id: :integer
|
9
|
+
})
|
10
|
+
api_params.to_json
|
11
|
+
end
|
12
|
+
|
13
|
+
rc = api_call post '/', { id: "15" }.to_json
|
14
|
+
rc.should succeed(200)
|
15
|
+
rc.body[:id].should == 15
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should work with nested-hashes' do
|
19
|
+
pending 'nested parameter groups'
|
20
|
+
|
21
|
+
app.post '/' do
|
22
|
+
api_required!({
|
23
|
+
project: {
|
24
|
+
id: :integer
|
25
|
+
}
|
26
|
+
})
|
27
|
+
api_params.to_json
|
28
|
+
end
|
29
|
+
|
30
|
+
rc = api_call post '/', { project: { id: "15" } }.to_json
|
31
|
+
rc.should succeed(200)
|
32
|
+
puts rc.body.inspect
|
33
|
+
rc.body[:project][:id].should == 15
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
3
37
|
end
|
@@ -15,6 +15,44 @@ describe Sinatra::API::Parameters do
|
|
15
15
|
last_response.body.should match(/Missing required parameter :id/)
|
16
16
|
end
|
17
17
|
|
18
|
+
it 'should define a required group parameter' do
|
19
|
+
pending 'nested parameter groups'
|
20
|
+
|
21
|
+
app.post '/' do
|
22
|
+
api_required!({
|
23
|
+
id: nil,
|
24
|
+
project: {
|
25
|
+
name: nil
|
26
|
+
}
|
27
|
+
})
|
28
|
+
|
29
|
+
api_params.to_json
|
30
|
+
end
|
31
|
+
|
32
|
+
rc = api_call post '/', { id: 123, project: { name: 'adooga' } }.to_json
|
33
|
+
rc.should succeed
|
34
|
+
rc.body.should == {
|
35
|
+
id: 123,
|
36
|
+
project: {
|
37
|
+
name: 'adooga'
|
38
|
+
}
|
39
|
+
}.with_indifferent_access
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should reject when missing a required group parameter' do
|
43
|
+
app.post '/' do
|
44
|
+
api_required!({
|
45
|
+
id: nil,
|
46
|
+
project: {
|
47
|
+
name: nil
|
48
|
+
}
|
49
|
+
})
|
50
|
+
end
|
51
|
+
|
52
|
+
rc = api_call post '/', { id: 123 }.to_json
|
53
|
+
rc.should fail(400, 'missing name')
|
54
|
+
end
|
55
|
+
|
18
56
|
it 'should define required parameters using list style' do
|
19
57
|
app.post '/' do
|
20
58
|
api_required!([ :id, :name ])
|
@@ -141,4 +179,15 @@ describe Sinatra::API::Parameters do
|
|
141
179
|
}.to_json
|
142
180
|
end
|
143
181
|
|
182
|
+
it 'should conflict with route parameters' do
|
183
|
+
app.post '/chickens/:chicken_id' do
|
184
|
+
api_parameter! :chicken_id, type: :string
|
185
|
+
api_params.to_json
|
186
|
+
end
|
187
|
+
|
188
|
+
rc = api_call post '/chickens/12', { chicken_id: 'keeek' }.to_json
|
189
|
+
rc.should succeed(200)
|
190
|
+
rc.body[:chicken_id].should == '12'
|
191
|
+
end
|
192
|
+
|
144
193
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-11-
|
12
|
+
date: 2013-11-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|