grape 1.8.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +19 -22
  4. data/UPGRADING.md +35 -0
  5. data/grape.gemspec +1 -4
  6. data/lib/grape/dsl/desc.rb +1 -1
  7. data/lib/grape/dsl/inside_route.rb +9 -9
  8. data/lib/grape/endpoint.rb +9 -1
  9. data/lib/grape/exceptions/missing_group_type.rb +1 -1
  10. data/lib/grape/exceptions/unsupported_group_type.rb +1 -1
  11. data/lib/grape/http/headers.rb +12 -2
  12. data/lib/grape/middleware/auth/strategies.rb +1 -2
  13. data/lib/grape/middleware/error.rb +4 -4
  14. data/lib/grape/middleware/formatter.rb +5 -5
  15. data/lib/grape/railtie.rb +9 -0
  16. data/lib/grape/request.rb +8 -2
  17. data/lib/grape/router/route.rb +1 -1
  18. data/lib/grape/validations/validators/base.rb +1 -1
  19. data/lib/grape/validations/validators/values_validator.rb +2 -2
  20. data/lib/grape/version.rb +1 -1
  21. data/lib/grape.rb +15 -2
  22. metadata +8 -243
  23. data/spec/grape/api/custom_validations_spec.rb +0 -213
  24. data/spec/grape/api/deeply_included_options_spec.rb +0 -56
  25. data/spec/grape/api/defines_boolean_in_params_spec.rb +0 -38
  26. data/spec/grape/api/documentation_spec.rb +0 -59
  27. data/spec/grape/api/inherited_helpers_spec.rb +0 -114
  28. data/spec/grape/api/instance_spec.rb +0 -103
  29. data/spec/grape/api/invalid_format_spec.rb +0 -45
  30. data/spec/grape/api/namespace_parameters_in_route_spec.rb +0 -38
  31. data/spec/grape/api/nested_helpers_spec.rb +0 -50
  32. data/spec/grape/api/optional_parameters_in_route_spec.rb +0 -43
  33. data/spec/grape/api/parameters_modification_spec.rb +0 -41
  34. data/spec/grape/api/patch_method_helpers_spec.rb +0 -79
  35. data/spec/grape/api/recognize_path_spec.rb +0 -21
  36. data/spec/grape/api/required_parameters_in_route_spec.rb +0 -37
  37. data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +0 -26
  38. data/spec/grape/api/routes_with_requirements_spec.rb +0 -59
  39. data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +0 -41
  40. data/spec/grape/api/shared_helpers_spec.rb +0 -36
  41. data/spec/grape/api_remount_spec.rb +0 -509
  42. data/spec/grape/api_spec.rb +0 -4356
  43. data/spec/grape/dsl/callbacks_spec.rb +0 -45
  44. data/spec/grape/dsl/desc_spec.rb +0 -98
  45. data/spec/grape/dsl/headers_spec.rb +0 -62
  46. data/spec/grape/dsl/helpers_spec.rb +0 -100
  47. data/spec/grape/dsl/inside_route_spec.rb +0 -531
  48. data/spec/grape/dsl/logger_spec.rb +0 -24
  49. data/spec/grape/dsl/middleware_spec.rb +0 -60
  50. data/spec/grape/dsl/parameters_spec.rb +0 -180
  51. data/spec/grape/dsl/request_response_spec.rb +0 -225
  52. data/spec/grape/dsl/routing_spec.rb +0 -275
  53. data/spec/grape/dsl/settings_spec.rb +0 -261
  54. data/spec/grape/dsl/validations_spec.rb +0 -55
  55. data/spec/grape/endpoint/declared_spec.rb +0 -846
  56. data/spec/grape/endpoint_spec.rb +0 -1085
  57. data/spec/grape/entity_spec.rb +0 -336
  58. data/spec/grape/exceptions/base_spec.rb +0 -81
  59. data/spec/grape/exceptions/body_parse_errors_spec.rb +0 -185
  60. data/spec/grape/exceptions/invalid_accept_header_spec.rb +0 -358
  61. data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -15
  62. data/spec/grape/exceptions/invalid_response_spec.rb +0 -11
  63. data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -15
  64. data/spec/grape/exceptions/missing_group_type_spec.rb +0 -17
  65. data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -17
  66. data/spec/grape/exceptions/missing_option_spec.rb +0 -15
  67. data/spec/grape/exceptions/unknown_options_spec.rb +0 -15
  68. data/spec/grape/exceptions/unknown_validator_spec.rb +0 -15
  69. data/spec/grape/exceptions/unsupported_group_type_spec.rb +0 -19
  70. data/spec/grape/exceptions/validation_errors_spec.rb +0 -92
  71. data/spec/grape/exceptions/validation_spec.rb +0 -19
  72. data/spec/grape/extensions/param_builders/hash_spec.rb +0 -83
  73. data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +0 -105
  74. data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +0 -79
  75. data/spec/grape/grape_spec.rb +0 -9
  76. data/spec/grape/integration/global_namespace_function_spec.rb +0 -29
  77. data/spec/grape/integration/rack_sendfile_spec.rb +0 -48
  78. data/spec/grape/integration/rack_spec.rb +0 -51
  79. data/spec/grape/loading_spec.rb +0 -44
  80. data/spec/grape/middleware/auth/base_spec.rb +0 -31
  81. data/spec/grape/middleware/auth/dsl_spec.rb +0 -60
  82. data/spec/grape/middleware/auth/strategies_spec.rb +0 -120
  83. data/spec/grape/middleware/base_spec.rb +0 -221
  84. data/spec/grape/middleware/error_spec.rb +0 -85
  85. data/spec/grape/middleware/exception_spec.rb +0 -294
  86. data/spec/grape/middleware/formatter_spec.rb +0 -461
  87. data/spec/grape/middleware/globals_spec.rb +0 -30
  88. data/spec/grape/middleware/stack_spec.rb +0 -155
  89. data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -122
  90. data/spec/grape/middleware/versioner/header_spec.rb +0 -345
  91. data/spec/grape/middleware/versioner/param_spec.rb +0 -171
  92. data/spec/grape/middleware/versioner/path_spec.rb +0 -62
  93. data/spec/grape/middleware/versioner_spec.rb +0 -21
  94. data/spec/grape/named_api_spec.rb +0 -19
  95. data/spec/grape/parser_spec.rb +0 -86
  96. data/spec/grape/path_spec.rb +0 -252
  97. data/spec/grape/presenters/presenter_spec.rb +0 -71
  98. data/spec/grape/request_spec.rb +0 -126
  99. data/spec/grape/util/inheritable_setting_spec.rb +0 -242
  100. data/spec/grape/util/inheritable_values_spec.rb +0 -79
  101. data/spec/grape/util/reverse_stackable_values_spec.rb +0 -134
  102. data/spec/grape/util/stackable_values_spec.rb +0 -128
  103. data/spec/grape/util/strict_hash_configuration_spec.rb +0 -38
  104. data/spec/grape/validations/attributes_doc_spec.rb +0 -153
  105. data/spec/grape/validations/instance_behaivour_spec.rb +0 -43
  106. data/spec/grape/validations/multiple_attributes_iterator_spec.rb +0 -38
  107. data/spec/grape/validations/params_scope_spec.rb +0 -1420
  108. data/spec/grape/validations/single_attribute_iterator_spec.rb +0 -56
  109. data/spec/grape/validations/types/array_coercer_spec.rb +0 -33
  110. data/spec/grape/validations/types/primitive_coercer_spec.rb +0 -150
  111. data/spec/grape/validations/types/set_coercer_spec.rb +0 -32
  112. data/spec/grape/validations/types_spec.rb +0 -111
  113. data/spec/grape/validations/validators/all_or_none_spec.rb +0 -162
  114. data/spec/grape/validations/validators/allow_blank_spec.rb +0 -575
  115. data/spec/grape/validations/validators/at_least_one_of_spec.rb +0 -205
  116. data/spec/grape/validations/validators/base_spec.rb +0 -38
  117. data/spec/grape/validations/validators/coerce_spec.rb +0 -1261
  118. data/spec/grape/validations/validators/default_spec.rb +0 -463
  119. data/spec/grape/validations/validators/exactly_one_of_spec.rb +0 -233
  120. data/spec/grape/validations/validators/except_values_spec.rb +0 -192
  121. data/spec/grape/validations/validators/mutual_exclusion_spec.rb +0 -214
  122. data/spec/grape/validations/validators/presence_spec.rb +0 -315
  123. data/spec/grape/validations/validators/regexp_spec.rb +0 -161
  124. data/spec/grape/validations/validators/same_as_spec.rb +0 -57
  125. data/spec/grape/validations/validators/values_spec.rb +0 -733
  126. data/spec/grape/validations/validators/zh-CN.yml +0 -10
  127. data/spec/grape/validations_spec.rb +0 -2030
  128. data/spec/integration/eager_load/eager_load_spec.rb +0 -15
  129. data/spec/integration/multi_json/json_spec.rb +0 -7
  130. data/spec/integration/multi_xml/xml_spec.rb +0 -7
  131. data/spec/shared/deprecated_class_examples.rb +0 -16
  132. data/spec/shared/versioning_examples.rb +0 -215
  133. data/spec/spec_helper.rb +0 -52
  134. data/spec/support/basic_auth_encode_helpers.rb +0 -11
  135. data/spec/support/chunks.rb +0 -14
  136. data/spec/support/content_type_helpers.rb +0 -15
  137. data/spec/support/endpoint_faker.rb +0 -25
  138. data/spec/support/file_streamer.rb +0 -13
  139. data/spec/support/integer_helpers.rb +0 -13
  140. data/spec/support/versioned_helpers.rb +0 -55
@@ -1,205 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Validations::Validators::AtLeastOneOfValidator do
4
- let_it_be(:app) do
5
- Class.new(Grape::API) do
6
- rescue_from Grape::Exceptions::ValidationErrors do |e|
7
- error!(e.errors.transform_keys! { |key| key.join(',') }, 400)
8
- end
9
-
10
- params do
11
- optional :beer, :wine, :grapefruit
12
- at_least_one_of :beer, :wine, :grapefruit
13
- end
14
- post do
15
- end
16
-
17
- params do
18
- optional :beer, :wine, :grapefruit, :other
19
- at_least_one_of :beer, :wine, :grapefruit
20
- end
21
- post 'mixed-params' do
22
- end
23
-
24
- params do
25
- optional :beer, :wine, :grapefruit
26
- at_least_one_of :beer, :wine, :grapefruit, message: 'you should choose something'
27
- end
28
- post '/custom-message' do
29
- end
30
-
31
- params do
32
- requires :item, type: Hash do
33
- optional :beer, :wine, :grapefruit
34
- at_least_one_of :beer, :wine, :grapefruit, message: 'fail'
35
- end
36
- end
37
- post '/nested-hash' do
38
- end
39
-
40
- params do
41
- requires :items, type: Array do
42
- optional :beer, :wine, :grapefruit
43
- at_least_one_of :beer, :wine, :grapefruit, message: 'fail'
44
- end
45
- end
46
- post '/nested-array' do
47
- end
48
-
49
- params do
50
- requires :items, type: Array do
51
- requires :nested_items, type: Array do
52
- optional :beer, :wine, :grapefruit
53
- at_least_one_of :beer, :wine, :grapefruit, message: 'fail'
54
- end
55
- end
56
- end
57
- post '/deeply-nested-array' do
58
- end
59
- end
60
- end
61
-
62
- describe '#validate!' do
63
- subject(:validate) { post path, params }
64
-
65
- context 'when all restricted params are present' do
66
- let(:path) { '/' }
67
- let(:params) { { beer: true, wine: true, grapefruit: true } }
68
-
69
- it 'does not return a validation error' do
70
- validate
71
- expect(last_response.status).to eq 201
72
- end
73
-
74
- context 'mixed with other params' do
75
- let(:path) { '/mixed-params' }
76
- let(:params) { { beer: true, wine: true, grapefruit: true, other: true } }
77
-
78
- it 'does not return a validation error' do
79
- validate
80
- expect(last_response.status).to eq 201
81
- end
82
- end
83
- end
84
-
85
- context 'when a subset of restricted params are present' do
86
- let(:path) { '/' }
87
- let(:params) { { beer: true, grapefruit: true } }
88
-
89
- it 'does not return a validation error' do
90
- validate
91
- expect(last_response.status).to eq 201
92
- end
93
- end
94
-
95
- context 'when none of the restricted params is selected' do
96
- let(:path) { '/' }
97
- let(:params) { { other: true } }
98
-
99
- it 'returns a validation error' do
100
- validate
101
- expect(last_response.status).to eq 400
102
- expect(JSON.parse(last_response.body)).to eq(
103
- 'beer,wine,grapefruit' => ['are missing, at least one parameter must be provided']
104
- )
105
- end
106
-
107
- context 'when custom message is specified' do
108
- let(:path) { '/custom-message' }
109
-
110
- it 'returns a validation error' do
111
- validate
112
- expect(last_response.status).to eq 400
113
- expect(JSON.parse(last_response.body)).to eq(
114
- 'beer,wine,grapefruit' => ['you should choose something']
115
- )
116
- end
117
- end
118
- end
119
-
120
- context 'when exactly one of the restricted params is selected' do
121
- let(:path) { '/' }
122
- let(:params) { { beer: true } }
123
-
124
- it 'does not return a validation error' do
125
- validate
126
- expect(last_response.status).to eq 201
127
- end
128
- end
129
-
130
- context 'when restricted params are nested inside hash' do
131
- let(:path) { '/nested-hash' }
132
-
133
- context 'when at least one of them is present' do
134
- let(:params) { { item: { beer: true, wine: true } } }
135
-
136
- it 'does not return a validation error' do
137
- validate
138
- expect(last_response.status).to eq 201
139
- end
140
- end
141
-
142
- context 'when none of them are present' do
143
- let(:params) { { item: { other: true } } }
144
-
145
- it 'returns a validation error with full names of the params' do
146
- validate
147
- expect(last_response.status).to eq 400
148
- expect(JSON.parse(last_response.body)).to eq(
149
- 'item[beer],item[wine],item[grapefruit]' => ['fail']
150
- )
151
- end
152
- end
153
- end
154
-
155
- context 'when restricted params are nested inside array' do
156
- let(:path) { '/nested-array' }
157
-
158
- context 'when at least one of them is present' do
159
- let(:params) { { items: [{ beer: true, wine: true }, { grapefruit: true }] } }
160
-
161
- it 'does not return a validation error' do
162
- validate
163
- expect(last_response.status).to eq 201
164
- end
165
- end
166
-
167
- context 'when none of them are present' do
168
- let(:params) { { items: [{ beer: true, other: true }, { other: true }] } }
169
-
170
- it 'returns a validation error with full names of the params' do
171
- validate
172
- expect(last_response.status).to eq 400
173
- expect(JSON.parse(last_response.body)).to eq(
174
- 'items[1][beer],items[1][wine],items[1][grapefruit]' => ['fail']
175
- )
176
- end
177
- end
178
- end
179
-
180
- context 'when restricted params are deeply nested' do
181
- let(:path) { '/deeply-nested-array' }
182
-
183
- context 'when at least one of them is present' do
184
- let(:params) { { items: [{ nested_items: [{ wine: true }] }] } }
185
-
186
- it 'does not return a validation error' do
187
- validate
188
- expect(last_response.status).to eq 201
189
- end
190
- end
191
-
192
- context 'when none of them are present' do
193
- let(:params) { { items: [{ nested_items: [{ other: true }] }] } }
194
-
195
- it 'returns a validation error with full names of the params' do
196
- validate
197
- expect(last_response.status).to eq 400
198
- expect(JSON.parse(last_response.body)).to eq(
199
- 'items[0][nested_items][0][beer],items[0][nested_items][0][wine],items[0][nested_items][0][grapefruit]' => ['fail']
200
- )
201
- end
202
- end
203
- end
204
- end
205
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Grape::Validations::Validators::Base do
4
- describe '#inherited' do
5
- context 'when validator is anonymous' do
6
- subject(:custom_validator) { Class.new(described_class) }
7
-
8
- it 'does not register the validator' do
9
- expect(Grape::Validations).not_to receive(:register_validator)
10
- custom_validator
11
- end
12
- end
13
-
14
- # Anonymous class does not have a name and class A < B would leak.
15
- # Simulates inherited callback
16
- context "when validator's underscored name does not end with _validator" do
17
- subject(:custom_validator) { described_class.inherited(TestModule::CustomValidatorABC) }
18
-
19
- before { stub_const('TestModule::CustomValidatorABC', Class.new) }
20
-
21
- it 'registers the custom validator with a short name' do
22
- expect(Grape::Validations).to receive(:register_validator).with('custom_validator_abc', TestModule::CustomValidatorABC)
23
- custom_validator
24
- end
25
- end
26
-
27
- context "when validator's underscored name ends with _validator" do
28
- subject(:custom_validator) { described_class.inherited(TestModule::CustomValidator) }
29
-
30
- before { stub_const('TestModule::CustomValidator', Class.new) }
31
-
32
- it 'registers the custom validator with short name not ending with validator' do
33
- expect(Grape::Validations).to receive(:register_validator).with('custom', TestModule::CustomValidator)
34
- custom_validator
35
- end
36
- end
37
- end
38
- end