grape 1.5.2 → 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +47 -0
  3. data/CONTRIBUTING.md +2 -1
  4. data/README.md +33 -3
  5. data/UPGRADING.md +71 -2
  6. data/grape.gemspec +5 -5
  7. data/lib/grape/api/instance.rb +13 -17
  8. data/lib/grape/api.rb +18 -13
  9. data/lib/grape/cookies.rb +2 -0
  10. data/lib/grape/dsl/desc.rb +3 -5
  11. data/lib/grape/dsl/headers.rb +5 -2
  12. data/lib/grape/dsl/helpers.rb +7 -5
  13. data/lib/grape/dsl/inside_route.rb +17 -8
  14. data/lib/grape/dsl/middleware.rb +4 -4
  15. data/lib/grape/dsl/parameters.rb +3 -3
  16. data/lib/grape/dsl/request_response.rb +9 -6
  17. data/lib/grape/dsl/routing.rb +2 -2
  18. data/lib/grape/dsl/settings.rb +5 -5
  19. data/lib/grape/endpoint.rb +21 -36
  20. data/lib/grape/error_formatter/json.rb +2 -6
  21. data/lib/grape/error_formatter/xml.rb +2 -6
  22. data/lib/grape/exceptions/empty_message_body.rb +11 -0
  23. data/lib/grape/exceptions/validation.rb +1 -2
  24. data/lib/grape/formatter/json.rb +1 -0
  25. data/lib/grape/formatter/serializable_hash.rb +2 -1
  26. data/lib/grape/formatter/xml.rb +1 -0
  27. data/lib/grape/locale/en.yml +1 -1
  28. data/lib/grape/middleware/auth/dsl.rb +7 -1
  29. data/lib/grape/middleware/base.rb +3 -1
  30. data/lib/grape/middleware/formatter.rb +4 -4
  31. data/lib/grape/middleware/stack.rb +2 -2
  32. data/lib/grape/middleware/versioner/accept_version_header.rb +3 -5
  33. data/lib/grape/middleware/versioner/header.rb +6 -4
  34. data/lib/grape/middleware/versioner/param.rb +1 -0
  35. data/lib/grape/middleware/versioner/parse_media_type_patch.rb +2 -1
  36. data/lib/grape/middleware/versioner/path.rb +2 -0
  37. data/lib/grape/parser/json.rb +1 -1
  38. data/lib/grape/parser/xml.rb +1 -1
  39. data/lib/grape/path.rb +1 -0
  40. data/lib/grape/request.rb +3 -0
  41. data/lib/grape/router/pattern.rb +1 -1
  42. data/lib/grape/router/route.rb +2 -2
  43. data/lib/grape/router.rb +6 -0
  44. data/lib/grape/util/inheritable_setting.rb +1 -3
  45. data/lib/grape/util/lazy_value.rb +3 -2
  46. data/lib/grape/util/strict_hash_configuration.rb +1 -1
  47. data/lib/grape/validations/params_scope.rb +88 -55
  48. data/lib/grape/validations/types/custom_type_coercer.rb +1 -2
  49. data/lib/grape/validations/types/dry_type_coercer.rb +1 -1
  50. data/lib/grape/validations/types/json.rb +2 -1
  51. data/lib/grape/validations/types/primitive_coercer.rb +3 -3
  52. data/lib/grape/validations/validators/all_or_none.rb +8 -5
  53. data/lib/grape/validations/validators/allow_blank.rb +9 -7
  54. data/lib/grape/validations/validators/as.rb +6 -8
  55. data/lib/grape/validations/validators/at_least_one_of.rb +7 -4
  56. data/lib/grape/validations/validators/base.rb +75 -70
  57. data/lib/grape/validations/validators/coerce.rb +63 -79
  58. data/lib/grape/validations/validators/default.rb +37 -34
  59. data/lib/grape/validations/validators/exactly_one_of.rb +9 -6
  60. data/lib/grape/validations/validators/except_values.rb +13 -11
  61. data/lib/grape/validations/validators/multiple_params_base.rb +24 -20
  62. data/lib/grape/validations/validators/mutual_exclusion.rb +8 -5
  63. data/lib/grape/validations/validators/presence.rb +7 -4
  64. data/lib/grape/validations/validators/regexp.rb +8 -5
  65. data/lib/grape/validations/validators/same_as.rb +18 -15
  66. data/lib/grape/validations/validators/values.rb +61 -56
  67. data/lib/grape/validations.rb +6 -0
  68. data/lib/grape/version.rb +1 -1
  69. data/lib/grape.rb +4 -1
  70. data/spec/grape/api/custom_validations_spec.rb +77 -45
  71. data/spec/grape/api/deeply_included_options_spec.rb +3 -3
  72. data/spec/grape/api/defines_boolean_in_params_spec.rb +2 -1
  73. data/spec/grape/api/invalid_format_spec.rb +2 -0
  74. data/spec/grape/api/recognize_path_spec.rb +1 -1
  75. data/spec/grape/api/routes_with_requirements_spec.rb +8 -8
  76. data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +9 -15
  77. data/spec/grape/api_remount_spec.rb +16 -15
  78. data/spec/grape/api_spec.rb +510 -220
  79. data/spec/grape/dsl/callbacks_spec.rb +2 -1
  80. data/spec/grape/dsl/headers_spec.rb +39 -9
  81. data/spec/grape/dsl/helpers_spec.rb +3 -2
  82. data/spec/grape/dsl/inside_route_spec.rb +6 -4
  83. data/spec/grape/dsl/logger_spec.rb +16 -18
  84. data/spec/grape/dsl/middleware_spec.rb +2 -1
  85. data/spec/grape/dsl/parameters_spec.rb +2 -0
  86. data/spec/grape/dsl/request_response_spec.rb +1 -0
  87. data/spec/grape/dsl/routing_spec.rb +10 -7
  88. data/spec/grape/endpoint/declared_spec.rb +259 -12
  89. data/spec/grape/endpoint_spec.rb +77 -55
  90. data/spec/grape/entity_spec.rb +22 -22
  91. data/spec/grape/exceptions/body_parse_errors_spec.rb +3 -0
  92. data/spec/grape/exceptions/invalid_accept_header_spec.rb +61 -22
  93. data/spec/grape/exceptions/validation_errors_spec.rb +13 -10
  94. data/spec/grape/exceptions/validation_spec.rb +5 -3
  95. data/spec/grape/extensions/param_builders/hash_spec.rb +7 -7
  96. data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +8 -8
  97. data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +8 -8
  98. data/spec/grape/integration/rack_sendfile_spec.rb +1 -1
  99. data/spec/grape/loading_spec.rb +8 -8
  100. data/spec/grape/middleware/auth/dsl_spec.rb +15 -6
  101. data/spec/grape/middleware/auth/strategies_spec.rb +60 -20
  102. data/spec/grape/middleware/base_spec.rb +24 -15
  103. data/spec/grape/middleware/error_spec.rb +2 -2
  104. data/spec/grape/middleware/exception_spec.rb +111 -161
  105. data/spec/grape/middleware/formatter_spec.rb +27 -6
  106. data/spec/grape/middleware/globals_spec.rb +7 -4
  107. data/spec/grape/middleware/stack_spec.rb +14 -12
  108. data/spec/grape/middleware/versioner/accept_version_header_spec.rb +2 -1
  109. data/spec/grape/middleware/versioner/header_spec.rb +14 -13
  110. data/spec/grape/middleware/versioner/param_spec.rb +7 -1
  111. data/spec/grape/middleware/versioner/path_spec.rb +5 -1
  112. data/spec/grape/middleware/versioner_spec.rb +1 -1
  113. data/spec/grape/parser_spec.rb +4 -0
  114. data/spec/grape/path_spec.rb +52 -52
  115. data/spec/grape/presenters/presenter_spec.rb +7 -6
  116. data/spec/grape/request_spec.rb +6 -4
  117. data/spec/grape/util/inheritable_setting_spec.rb +7 -7
  118. data/spec/grape/util/inheritable_values_spec.rb +3 -2
  119. data/spec/grape/util/reverse_stackable_values_spec.rb +3 -1
  120. data/spec/grape/util/stackable_values_spec.rb +7 -5
  121. data/spec/grape/validations/instance_behaivour_spec.rb +9 -10
  122. data/spec/grape/validations/multiple_attributes_iterator_spec.rb +1 -0
  123. data/spec/grape/validations/params_scope_spec.rb +46 -10
  124. data/spec/grape/validations/single_attribute_iterator_spec.rb +2 -1
  125. data/spec/grape/validations/types/primitive_coercer_spec.rb +4 -4
  126. data/spec/grape/validations/types_spec.rb +8 -8
  127. data/spec/grape/validations/validators/all_or_none_spec.rb +50 -56
  128. data/spec/grape/validations/validators/allow_blank_spec.rb +136 -140
  129. data/spec/grape/validations/validators/at_least_one_of_spec.rb +50 -56
  130. data/spec/grape/validations/validators/coerce_spec.rb +99 -22
  131. data/spec/grape/validations/validators/default_spec.rb +72 -78
  132. data/spec/grape/validations/validators/exactly_one_of_spec.rb +71 -77
  133. data/spec/grape/validations/validators/except_values_spec.rb +3 -3
  134. data/spec/grape/validations/validators/mutual_exclusion_spec.rb +71 -77
  135. data/spec/grape/validations/validators/presence_spec.rb +16 -1
  136. data/spec/grape/validations/validators/regexp_spec.rb +25 -31
  137. data/spec/grape/validations/validators/same_as_spec.rb +14 -20
  138. data/spec/grape/validations/validators/values_spec.rb +183 -178
  139. data/spec/grape/validations_spec.rb +99 -58
  140. data/spec/integration/eager_load/eager_load_spec.rb +2 -2
  141. data/spec/integration/multi_json/json_spec.rb +1 -1
  142. data/spec/integration/multi_xml/xml_spec.rb +1 -1
  143. data/spec/shared/versioning_examples.rb +12 -9
  144. data/spec/spec_helper.rb +12 -2
  145. data/spec/support/basic_auth_encode_helpers.rb +1 -1
  146. metadata +102 -101
@@ -5,6 +5,7 @@ require 'spec_helper'
5
5
  describe Grape::Validations::SingleAttributeIterator do
6
6
  describe '#each' do
7
7
  subject(:iterator) { described_class.new(validator, scope, params) }
8
+
8
9
  let(:scope) { Grape::Validations::ParamsScope.new(api: Class.new(Grape::API)) }
9
10
  let(:validator) { double(attrs: %i[first second]) }
10
11
 
@@ -49,7 +50,7 @@ describe Grape::Validations::SingleAttributeIterator do
49
50
  it 'marks params with skipped values' do
50
51
  expect { |b| iterator.each(&b) }.to yield_successive_args(
51
52
  [params[0], :first, false, true], [params[0], :second, false, true],
52
- [params[1], :first, false, false], [params[1], :second, false, false],
53
+ [params[1], :first, false, false], [params[1], :second, false, false]
53
54
  )
54
55
  end
55
56
  end
@@ -3,16 +3,16 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Grape::Validations::Types::PrimitiveCoercer do
6
- let(:strict) { false }
7
-
8
6
  subject { described_class.new(type, strict) }
9
7
 
8
+ let(:strict) { false }
9
+
10
10
  describe '#call' do
11
11
  context 'BigDecimal' do
12
12
  let(:type) { BigDecimal }
13
13
 
14
14
  it 'coerces to BigDecimal' do
15
- expect(subject.call(5)).to eq(BigDecimal(5))
15
+ expect(subject.call(5)).to eq(BigDecimal('5'))
16
16
  end
17
17
 
18
18
  it 'coerces an empty string to nil' do
@@ -127,7 +127,7 @@ describe Grape::Validations::Types::PrimitiveCoercer do
127
127
  end
128
128
 
129
129
  it 'returns a value as it is when the given value is BigDecimal' do
130
- expect(subject.call(BigDecimal(0))).to eq(BigDecimal(0))
130
+ expect(subject.call(BigDecimal('0'))).to eq(BigDecimal('0'))
131
131
  end
132
132
  end
133
133
  end
@@ -20,13 +20,13 @@ describe Grape::Validations::Types do
20
20
  Date, DateTime, Time
21
21
  ].each do |type|
22
22
  it "recognizes #{type} as a primitive" do
23
- expect(described_class.primitive?(type)).to be_truthy
23
+ expect(described_class).to be_primitive(type)
24
24
  end
25
25
  end
26
26
 
27
27
  it 'identifies unknown types' do
28
- expect(described_class.primitive?(Object)).to be_falsy
29
- expect(described_class.primitive?(TypesSpec::FooType)).to be_falsy
28
+ expect(described_class).not_to be_primitive(Object)
29
+ expect(described_class).not_to be_primitive(TypesSpec::FooType)
30
30
  end
31
31
  end
32
32
 
@@ -35,7 +35,7 @@ describe Grape::Validations::Types do
35
35
  Hash, Array, Set
36
36
  ].each do |type|
37
37
  it "recognizes #{type} as a structure" do
38
- expect(described_class.structure?(type)).to be_truthy
38
+ expect(described_class).to be_structure(type)
39
39
  end
40
40
  end
41
41
  end
@@ -45,22 +45,22 @@ describe Grape::Validations::Types do
45
45
  JSON, Array[JSON], File, Rack::Multipart::UploadedFile
46
46
  ].each do |type|
47
47
  it "provides special handling for #{type.inspect}" do
48
- expect(described_class.special?(type)).to be_truthy
48
+ expect(described_class).to be_special(type)
49
49
  end
50
50
  end
51
51
  end
52
52
 
53
53
  describe '::custom?' do
54
54
  it 'returns false if the type does not respond to :parse' do
55
- expect(described_class.custom?(Object)).to be_falsy
55
+ expect(described_class).not_to be_custom(Object)
56
56
  end
57
57
 
58
58
  it 'returns true if the type responds to :parse with one argument' do
59
- expect(described_class.custom?(TypesSpec::FooType)).to be_truthy
59
+ expect(described_class).to be_custom(TypesSpec::FooType)
60
60
  end
61
61
 
62
62
  it 'returns false if the type\'s #parse method takes other than one argument' do
63
- expect(described_class.custom?(TypesSpec::BarType)).to be_falsy
63
+ expect(described_class).not_to be_custom(TypesSpec::BarType)
64
64
  end
65
65
  end
66
66
 
@@ -2,73 +2,67 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Grape::Validations::AllOrNoneOfValidator do
6
- describe '#validate!' do
7
- subject(:validate) { post path, params }
8
-
9
- module ValidationsSpec
10
- module AllOrNoneOfValidatorSpec
11
- class API < Grape::API
12
- rescue_from Grape::Exceptions::ValidationErrors do |e|
13
- error!(e.errors.transform_keys! { |key| key.join(',') }, 400)
14
- end
5
+ describe Grape::Validations::Validators::AllOrNoneOfValidator do
6
+ let_it_be(:app) do
7
+ Class.new(Grape::API) do
8
+ rescue_from Grape::Exceptions::ValidationErrors do |e|
9
+ error!(e.errors.transform_keys! { |key| key.join(',') }, 400)
10
+ end
15
11
 
16
- params do
17
- optional :beer, :wine, type: Boolean
18
- all_or_none_of :beer, :wine
19
- end
20
- post do
21
- end
12
+ params do
13
+ optional :beer, :wine, type: Grape::API::Boolean
14
+ all_or_none_of :beer, :wine
15
+ end
16
+ post do
17
+ end
22
18
 
23
- params do
24
- optional :beer, :wine, :other, type: Boolean
25
- all_or_none_of :beer, :wine
26
- end
27
- post 'mixed-params' do
28
- end
19
+ params do
20
+ optional :beer, :wine, :other, type: Grape::API::Boolean
21
+ all_or_none_of :beer, :wine
22
+ end
23
+ post 'mixed-params' do
24
+ end
29
25
 
30
- params do
31
- optional :beer, :wine, type: Boolean
32
- all_or_none_of :beer, :wine, message: 'choose all or none'
33
- end
34
- post '/custom-message' do
35
- end
26
+ params do
27
+ optional :beer, :wine, type: Grape::API::Boolean
28
+ all_or_none_of :beer, :wine, message: 'choose all or none'
29
+ end
30
+ post '/custom-message' do
31
+ end
36
32
 
37
- params do
38
- requires :item, type: Hash do
39
- optional :beer, :wine, type: Boolean
40
- all_or_none_of :beer, :wine
41
- end
42
- end
43
- post '/nested-hash' do
44
- end
33
+ params do
34
+ requires :item, type: Hash do
35
+ optional :beer, :wine, type: Grape::API::Boolean
36
+ all_or_none_of :beer, :wine
37
+ end
38
+ end
39
+ post '/nested-hash' do
40
+ end
45
41
 
46
- params do
47
- requires :items, type: Array do
48
- optional :beer, :wine, type: Boolean
49
- all_or_none_of :beer, :wine
50
- end
51
- end
52
- post '/nested-array' do
53
- end
42
+ params do
43
+ requires :items, type: Array do
44
+ optional :beer, :wine, type: Grape::API::Boolean
45
+ all_or_none_of :beer, :wine
46
+ end
47
+ end
48
+ post '/nested-array' do
49
+ end
54
50
 
55
- params do
56
- requires :items, type: Array do
57
- requires :nested_items, type: Array do
58
- optional :beer, :wine, type: Boolean
59
- all_or_none_of :beer, :wine
60
- end
61
- end
62
- end
63
- post '/deeply-nested-array' do
51
+ params do
52
+ requires :items, type: Array do
53
+ requires :nested_items, type: Array do
54
+ optional :beer, :wine, type: Grape::API::Boolean
55
+ all_or_none_of :beer, :wine
64
56
  end
65
57
  end
66
58
  end
59
+ post '/deeply-nested-array' do
60
+ end
67
61
  end
62
+ end
68
63
 
69
- def app
70
- ValidationsSpec::AllOrNoneOfValidatorSpec::API
71
- end
64
+ describe '#validate!' do
65
+ subject(:validate) { post path, params }
72
66
 
73
67
  context 'when all restricted params are present' do
74
68
  let(:path) { '/' }
@@ -2,24 +2,139 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Grape::Validations::AllowBlankValidator do
6
- module ValidationsSpec
7
- module AllowBlankValidatorSpec
8
- class API < Grape::API
9
- default_format :json
5
+ describe Grape::Validations::Validators::AllowBlankValidator do
6
+ let_it_be(:app) do
7
+ Class.new(Grape::API) do
8
+ default_format :json
10
9
 
11
- params do
10
+ params do
11
+ requires :name, allow_blank: false
12
+ end
13
+ get '/disallow_blank'
14
+
15
+ params do
16
+ optional :name, type: String, allow_blank: false
17
+ end
18
+ get '/opt_disallow_string_blank'
19
+
20
+ params do
21
+ optional :name, allow_blank: false
22
+ end
23
+ get '/disallow_blank_optional_param'
24
+
25
+ params do
26
+ requires :name, allow_blank: true
27
+ end
28
+ get '/allow_blank'
29
+
30
+ params do
31
+ requires :val, type: DateTime, allow_blank: true
32
+ end
33
+ get '/allow_datetime_blank'
34
+
35
+ params do
36
+ requires :val, type: DateTime, allow_blank: false
37
+ end
38
+ get '/disallow_datetime_blank'
39
+
40
+ params do
41
+ requires :val, type: DateTime
42
+ end
43
+ get '/default_allow_datetime_blank'
44
+
45
+ params do
46
+ requires :val, type: Date, allow_blank: true
47
+ end
48
+ get '/allow_date_blank'
49
+
50
+ params do
51
+ requires :val, type: Integer, allow_blank: true
52
+ end
53
+ get '/allow_integer_blank'
54
+
55
+ params do
56
+ requires :val, type: Float, allow_blank: true
57
+ end
58
+ get '/allow_float_blank'
59
+
60
+ params do
61
+ requires :val, type: Integer, allow_blank: true
62
+ end
63
+ get '/allow_integer_blank'
64
+
65
+ params do
66
+ requires :val, type: Symbol, allow_blank: true
67
+ end
68
+ get '/allow_symbol_blank'
69
+
70
+ params do
71
+ requires :val, type: Grape::API::Boolean, allow_blank: true
72
+ end
73
+ get '/allow_boolean_blank'
74
+
75
+ params do
76
+ requires :val, type: Grape::API::Boolean, allow_blank: false
77
+ end
78
+ get '/disallow_boolean_blank'
79
+
80
+ params do
81
+ optional :user, type: Hash do
82
+ requires :name, allow_blank: false
83
+ end
84
+ end
85
+ get '/disallow_blank_required_param_in_an_optional_group'
86
+
87
+ params do
88
+ optional :user, type: Hash do
89
+ requires :name, type: Date, allow_blank: true
90
+ end
91
+ end
92
+ get '/allow_blank_date_param_in_an_optional_group'
93
+
94
+ params do
95
+ optional :user, type: Hash do
96
+ optional :name, allow_blank: false
97
+ requires :age
98
+ end
99
+ end
100
+ get '/disallow_blank_optional_param_in_an_optional_group'
101
+
102
+ params do
103
+ requires :user, type: Hash do
12
104
  requires :name, allow_blank: false
13
105
  end
14
- get '/disallow_blank'
106
+ end
107
+ get '/disallow_blank_required_param_in_a_required_group'
108
+
109
+ params do
110
+ requires :user, type: Hash do
111
+ requires :name, allow_blank: false
112
+ end
113
+ end
114
+ get '/disallow_string_value_in_a_required_hash_group'
115
+
116
+ params do
117
+ requires :user, type: Hash do
118
+ optional :name, allow_blank: false
119
+ end
120
+ end
121
+ get '/disallow_blank_optional_param_in_a_required_group'
122
+
123
+ params do
124
+ optional :user, type: Hash do
125
+ optional :name, allow_blank: false
126
+ end
127
+ end
128
+ get '/disallow_string_value_in_an_optional_hash_group'
15
129
 
130
+ resources :custom_message do
16
131
  params do
17
- optional :name, type: String, allow_blank: false
132
+ requires :name, allow_blank: { value: false, message: 'has no value' }
18
133
  end
19
- get '/opt_disallow_string_blank'
134
+ get
20
135
 
21
136
  params do
22
- optional :name, allow_blank: false
137
+ optional :name, allow_blank: { value: false, message: 'has no value' }
23
138
  end
24
139
  get '/disallow_blank_optional_param'
25
140
 
@@ -34,7 +149,7 @@ describe Grape::Validations::AllowBlankValidator do
34
149
  get '/allow_datetime_blank'
35
150
 
36
151
  params do
37
- requires :val, type: DateTime, allow_blank: false
152
+ requires :val, type: DateTime, allow_blank: { value: false, message: 'has no value' }
38
153
  end
39
154
  get '/disallow_datetime_blank'
40
155
 
@@ -69,18 +184,18 @@ describe Grape::Validations::AllowBlankValidator do
69
184
  get '/allow_symbol_blank'
70
185
 
71
186
  params do
72
- requires :val, type: Boolean, allow_blank: true
187
+ requires :val, type: Grape::API::Boolean, allow_blank: true
73
188
  end
74
189
  get '/allow_boolean_blank'
75
190
 
76
191
  params do
77
- requires :val, type: Boolean, allow_blank: false
192
+ requires :val, type: Grape::API::Boolean, allow_blank: { value: false, message: 'has no value' }
78
193
  end
79
194
  get '/disallow_boolean_blank'
80
195
 
81
196
  params do
82
197
  optional :user, type: Hash do
83
- requires :name, allow_blank: false
198
+ requires :name, allow_blank: { value: false, message: 'has no value' }
84
199
  end
85
200
  end
86
201
  get '/disallow_blank_required_param_in_an_optional_group'
@@ -94,7 +209,7 @@ describe Grape::Validations::AllowBlankValidator do
94
209
 
95
210
  params do
96
211
  optional :user, type: Hash do
97
- optional :name, allow_blank: false
212
+ optional :name, allow_blank: { value: false, message: 'has no value' }
98
213
  requires :age
99
214
  end
100
215
  end
@@ -102,156 +217,35 @@ describe Grape::Validations::AllowBlankValidator do
102
217
 
103
218
  params do
104
219
  requires :user, type: Hash do
105
- requires :name, allow_blank: false
220
+ requires :name, allow_blank: { value: false, message: 'has no value' }
106
221
  end
107
222
  end
108
223
  get '/disallow_blank_required_param_in_a_required_group'
109
224
 
110
225
  params do
111
226
  requires :user, type: Hash do
112
- requires :name, allow_blank: false
227
+ requires :name, allow_blank: { value: false, message: 'has no value' }
113
228
  end
114
229
  end
115
230
  get '/disallow_string_value_in_a_required_hash_group'
116
231
 
117
232
  params do
118
233
  requires :user, type: Hash do
119
- optional :name, allow_blank: false
234
+ optional :name, allow_blank: { value: false, message: 'has no value' }
120
235
  end
121
236
  end
122
237
  get '/disallow_blank_optional_param_in_a_required_group'
123
238
 
124
239
  params do
125
240
  optional :user, type: Hash do
126
- optional :name, allow_blank: false
127
- end
128
- end
129
- get '/disallow_string_value_in_an_optional_hash_group'
130
-
131
- resources :custom_message do
132
- params do
133
- requires :name, allow_blank: { value: false, message: 'has no value' }
134
- end
135
- get
136
-
137
- params do
138
241
  optional :name, allow_blank: { value: false, message: 'has no value' }
139
242
  end
140
- get '/disallow_blank_optional_param'
141
-
142
- params do
143
- requires :name, allow_blank: true
144
- end
145
- get '/allow_blank'
146
-
147
- params do
148
- requires :val, type: DateTime, allow_blank: true
149
- end
150
- get '/allow_datetime_blank'
151
-
152
- params do
153
- requires :val, type: DateTime, allow_blank: { value: false, message: 'has no value' }
154
- end
155
- get '/disallow_datetime_blank'
156
-
157
- params do
158
- requires :val, type: DateTime
159
- end
160
- get '/default_allow_datetime_blank'
161
-
162
- params do
163
- requires :val, type: Date, allow_blank: true
164
- end
165
- get '/allow_date_blank'
166
-
167
- params do
168
- requires :val, type: Integer, allow_blank: true
169
- end
170
- get '/allow_integer_blank'
171
-
172
- params do
173
- requires :val, type: Float, allow_blank: true
174
- end
175
- get '/allow_float_blank'
176
-
177
- params do
178
- requires :val, type: Integer, allow_blank: true
179
- end
180
- get '/allow_integer_blank'
181
-
182
- params do
183
- requires :val, type: Symbol, allow_blank: true
184
- end
185
- get '/allow_symbol_blank'
186
-
187
- params do
188
- requires :val, type: Boolean, allow_blank: true
189
- end
190
- get '/allow_boolean_blank'
191
-
192
- params do
193
- requires :val, type: Boolean, allow_blank: { value: false, message: 'has no value' }
194
- end
195
- get '/disallow_boolean_blank'
196
-
197
- params do
198
- optional :user, type: Hash do
199
- requires :name, allow_blank: { value: false, message: 'has no value' }
200
- end
201
- end
202
- get '/disallow_blank_required_param_in_an_optional_group'
203
-
204
- params do
205
- optional :user, type: Hash do
206
- requires :name, type: Date, allow_blank: true
207
- end
208
- end
209
- get '/allow_blank_date_param_in_an_optional_group'
210
-
211
- params do
212
- optional :user, type: Hash do
213
- optional :name, allow_blank: { value: false, message: 'has no value' }
214
- requires :age
215
- end
216
- end
217
- get '/disallow_blank_optional_param_in_an_optional_group'
218
-
219
- params do
220
- requires :user, type: Hash do
221
- requires :name, allow_blank: { value: false, message: 'has no value' }
222
- end
223
- end
224
- get '/disallow_blank_required_param_in_a_required_group'
225
-
226
- params do
227
- requires :user, type: Hash do
228
- requires :name, allow_blank: { value: false, message: 'has no value' }
229
- end
230
- end
231
- get '/disallow_string_value_in_a_required_hash_group'
232
-
233
- params do
234
- requires :user, type: Hash do
235
- optional :name, allow_blank: { value: false, message: 'has no value' }
236
- end
237
- end
238
- get '/disallow_blank_optional_param_in_a_required_group'
239
-
240
- params do
241
- optional :user, type: Hash do
242
- optional :name, allow_blank: { value: false, message: 'has no value' }
243
- end
244
- end
245
- get '/disallow_string_value_in_an_optional_hash_group'
246
243
  end
244
+ get '/disallow_string_value_in_an_optional_hash_group'
247
245
  end
248
246
  end
249
247
  end
250
248
 
251
- def app
252
- ValidationsSpec::AllowBlankValidatorSpec::API
253
- end
254
-
255
249
  context 'invalid input' do
256
250
  it 'refuses empty string' do
257
251
  get '/disallow_blank', name: ''
@@ -289,10 +283,12 @@ describe Grape::Validations::AllowBlankValidator do
289
283
  get '/custom_message', name: ''
290
284
  expect(last_response.body).to eq('{"error":"name has no value"}')
291
285
  end
286
+
292
287
  it 'refuses empty string for an optional param' do
293
288
  get '/custom_message/disallow_blank_optional_param', name: ''
294
289
  expect(last_response.body).to eq('{"error":"name has no value"}')
295
290
  end
291
+
296
292
  it 'refuses only whitespaces' do
297
293
  get '/custom_message', name: ' '
298
294
  expect(last_response.body).to eq('{"error":"name has no value"}')