grape 1.1.0 → 1.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +370 -44
- data/CONTRIBUTING.md +32 -1
- data/LICENSE +1 -1
- data/README.md +683 -87
- data/UPGRADING.md +481 -17
- data/grape.gemspec +15 -4
- data/lib/grape/api/helpers.rb +2 -0
- data/lib/grape/api/instance.rb +279 -0
- data/lib/grape/api.rb +144 -176
- data/lib/grape/config.rb +34 -0
- data/lib/grape/content_types.rb +34 -0
- data/lib/grape/cookies.rb +4 -0
- data/lib/grape/dry_types.rb +12 -0
- data/lib/grape/dsl/api.rb +1 -1
- data/lib/grape/dsl/callbacks.rb +21 -1
- data/lib/grape/dsl/configuration.rb +1 -1
- data/lib/grape/dsl/desc.rb +41 -23
- data/lib/grape/dsl/headers.rb +7 -2
- data/lib/grape/dsl/helpers.rb +10 -7
- data/lib/grape/dsl/inside_route.rb +118 -62
- data/lib/grape/dsl/logger.rb +2 -0
- data/lib/grape/dsl/middleware.rb +11 -4
- data/lib/grape/dsl/parameters.rb +33 -19
- data/lib/grape/dsl/request_response.rb +12 -9
- data/lib/grape/dsl/routing.rb +22 -13
- data/lib/grape/dsl/settings.rb +10 -6
- data/lib/grape/dsl/validations.rb +19 -14
- data/lib/grape/eager_load.rb +20 -0
- data/lib/grape/endpoint.rb +67 -58
- data/lib/grape/error_formatter/base.rb +2 -0
- data/lib/grape/error_formatter/json.rb +11 -7
- data/lib/grape/error_formatter/txt.rb +2 -0
- data/lib/grape/error_formatter/xml.rb +4 -6
- data/lib/grape/error_formatter.rb +4 -2
- data/lib/grape/exceptions/base.rb +23 -16
- data/lib/grape/exceptions/empty_message_body.rb +11 -0
- data/lib/grape/exceptions/incompatible_option_values.rb +2 -0
- data/lib/grape/exceptions/invalid_accept_header.rb +2 -0
- data/lib/grape/exceptions/invalid_formatter.rb +2 -0
- data/lib/grape/exceptions/invalid_message_body.rb +2 -0
- data/lib/grape/exceptions/invalid_response.rb +11 -0
- data/lib/grape/exceptions/invalid_version_header.rb +2 -0
- data/lib/grape/exceptions/invalid_versioner_option.rb +2 -0
- data/lib/grape/exceptions/invalid_with_option_for_represent.rb +2 -0
- data/lib/grape/exceptions/method_not_allowed.rb +2 -0
- data/lib/grape/exceptions/missing_group_type.rb +10 -1
- data/lib/grape/exceptions/missing_mime_type.rb +2 -0
- data/lib/grape/exceptions/missing_option.rb +2 -0
- data/lib/grape/exceptions/missing_vendor_option.rb +2 -0
- data/lib/grape/exceptions/too_many_multipart_files.rb +11 -0
- data/lib/grape/exceptions/unknown_options.rb +2 -0
- data/lib/grape/exceptions/unknown_parameter.rb +2 -0
- data/lib/grape/exceptions/unknown_validator.rb +2 -0
- data/lib/grape/exceptions/unsupported_group_type.rb +10 -1
- data/lib/grape/exceptions/validation.rb +5 -8
- data/lib/grape/exceptions/validation_array_errors.rb +2 -0
- data/lib/grape/exceptions/validation_errors.rb +16 -13
- data/lib/grape/extensions/active_support/hash_with_indifferent_access.rb +4 -3
- data/lib/grape/extensions/deep_mergeable_hash.rb +2 -0
- data/lib/grape/extensions/deep_symbolize_hash.rb +2 -0
- data/lib/grape/extensions/hash.rb +2 -0
- data/lib/grape/extensions/hashie/mash.rb +2 -0
- data/lib/grape/formatter/json.rb +3 -0
- data/lib/grape/formatter/serializable_hash.rb +4 -1
- data/lib/grape/formatter/txt.rb +2 -0
- data/lib/grape/formatter/xml.rb +3 -0
- data/lib/grape/formatter.rb +5 -3
- data/lib/grape/http/headers.rb +50 -18
- data/lib/grape/locale/en.yml +11 -8
- data/lib/grape/middleware/auth/base.rb +7 -7
- data/lib/grape/middleware/auth/dsl.rb +9 -2
- data/lib/grape/middleware/auth/strategies.rb +2 -0
- data/lib/grape/middleware/auth/strategy_info.rb +2 -0
- data/lib/grape/middleware/base.rb +13 -8
- data/lib/grape/middleware/error.rb +22 -17
- data/lib/grape/middleware/filter.rb +2 -0
- data/lib/grape/middleware/formatter.rb +12 -10
- data/lib/grape/middleware/globals.rb +2 -0
- data/lib/grape/middleware/helpers.rb +12 -0
- data/lib/grape/middleware/stack.rb +16 -6
- data/lib/grape/middleware/versioner/accept_version_header.rb +5 -5
- data/lib/grape/middleware/versioner/header.rb +13 -9
- data/lib/grape/middleware/versioner/param.rb +4 -1
- data/lib/grape/middleware/versioner/parse_media_type_patch.rb +5 -1
- data/lib/grape/middleware/versioner/path.rb +5 -1
- data/lib/grape/middleware/versioner.rb +2 -0
- data/lib/grape/namespace.rb +14 -2
- data/lib/grape/parser/json.rb +3 -1
- data/lib/grape/parser/xml.rb +3 -1
- data/lib/grape/parser.rb +4 -2
- data/lib/grape/path.rb +16 -3
- data/lib/grape/presenters/presenter.rb +2 -0
- data/lib/grape/request.rb +21 -9
- data/lib/grape/router/attribute_translator.rb +41 -8
- data/lib/grape/router/pattern.rb +21 -17
- data/lib/grape/router/route.rb +15 -29
- data/lib/grape/router.rb +36 -29
- data/lib/grape/{serve_file → serve_stream}/file_body.rb +3 -1
- data/lib/grape/{serve_file → serve_stream}/sendfile_response.rb +3 -1
- data/lib/grape/{serve_file/file_response.rb → serve_stream/stream_response.rb} +10 -8
- data/lib/grape/types/invalid_value.rb +8 -0
- data/lib/grape/util/base_inheritable.rb +43 -0
- data/lib/grape/util/cache.rb +20 -0
- data/lib/grape/util/endpoint_configuration.rb +8 -0
- data/lib/grape/util/env.rb +19 -17
- data/lib/grape/util/inheritable_setting.rb +3 -3
- data/lib/grape/util/inheritable_values.rb +7 -25
- data/lib/grape/util/json.rb +4 -0
- data/lib/grape/util/lazy_block.rb +27 -0
- data/lib/grape/util/lazy_object.rb +43 -0
- data/lib/grape/util/lazy_value.rb +99 -0
- data/lib/grape/util/registrable.rb +2 -0
- data/lib/grape/util/reverse_stackable_values.rb +10 -35
- data/lib/grape/util/stackable_values.rb +21 -34
- data/lib/grape/util/strict_hash_configuration.rb +3 -1
- data/lib/grape/util/xml.rb +2 -0
- data/lib/grape/validations/attributes_doc.rb +58 -0
- data/lib/grape/validations/attributes_iterator.rb +16 -6
- data/lib/grape/validations/multiple_attributes_iterator.rb +13 -0
- data/lib/grape/validations/params_scope.rb +174 -94
- data/lib/grape/validations/single_attribute_iterator.rb +24 -0
- data/lib/grape/validations/types/array_coercer.rb +63 -0
- data/lib/grape/validations/types/build_coercer.rb +47 -49
- data/lib/grape/validations/types/custom_type_coercer.rb +30 -51
- data/lib/grape/validations/types/custom_type_collection_coercer.rb +10 -25
- data/lib/grape/validations/types/dry_type_coercer.rb +72 -0
- data/lib/grape/validations/types/file.rb +22 -18
- data/lib/grape/validations/types/invalid_value.rb +17 -0
- data/lib/grape/validations/types/json.rb +47 -39
- data/lib/grape/validations/types/multiple_type_coercer.rb +14 -33
- data/lib/grape/validations/types/primitive_coercer.rb +75 -0
- data/lib/grape/validations/types/set_coercer.rb +38 -0
- data/lib/grape/validations/types/variant_collection_coercer.rb +5 -13
- data/lib/grape/validations/types.rb +106 -63
- data/lib/grape/validations/validator_factory.rb +8 -11
- data/lib/grape/validations/validators/all_or_none_of_validator.rb +16 -0
- data/lib/grape/validations/validators/allow_blank_validator.rb +20 -0
- data/lib/grape/validations/validators/as_validator.rb +14 -0
- data/lib/grape/validations/validators/at_least_one_of_validator.rb +15 -0
- data/lib/grape/validations/validators/base.rb +84 -68
- data/lib/grape/validations/validators/coerce_validator.rb +75 -0
- data/lib/grape/validations/validators/default_validator.rb +51 -0
- data/lib/grape/validations/validators/exactly_one_of_validator.rb +17 -0
- data/lib/grape/validations/validators/except_values_validator.rb +24 -0
- data/lib/grape/validations/validators/multiple_params_base.rb +27 -16
- data/lib/grape/validations/validators/mutual_exclusion_validator.rb +16 -0
- data/lib/grape/validations/validators/presence_validator.rb +15 -0
- data/lib/grape/validations/validators/regexp_validator.rb +16 -0
- data/lib/grape/validations/validators/same_as_validator.rb +29 -0
- data/lib/grape/validations/validators/values_validator.rb +88 -0
- data/lib/grape/validations.rb +18 -6
- data/lib/grape/version.rb +3 -1
- data/lib/grape.rb +175 -94
- data/spec/grape/api/custom_validations_spec.rb +117 -44
- data/spec/grape/api/deeply_included_options_spec.rb +4 -4
- data/spec/grape/api/defines_boolean_in_params_spec.rb +38 -0
- data/spec/grape/api/documentation_spec.rb +59 -0
- data/spec/grape/api/inherited_helpers_spec.rb +1 -1
- data/spec/grape/api/instance_spec.rb +103 -0
- data/spec/grape/api/invalid_format_spec.rb +3 -1
- data/spec/grape/api/namespace_parameters_in_route_spec.rb +1 -1
- data/spec/grape/api/nested_helpers_spec.rb +1 -1
- data/spec/grape/api/optional_parameters_in_route_spec.rb +1 -1
- data/spec/grape/api/parameters_modification_spec.rb +2 -2
- data/spec/grape/api/patch_method_helpers_spec.rb +1 -1
- data/spec/grape/api/recognize_path_spec.rb +2 -2
- data/spec/grape/api/required_parameters_in_route_spec.rb +1 -1
- data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +1 -1
- data/spec/grape/api/routes_with_requirements_spec.rb +59 -0
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +10 -16
- data/spec/grape/api/shared_helpers_spec.rb +1 -1
- data/spec/grape/api_remount_spec.rb +473 -0
- data/spec/grape/api_spec.rb +995 -231
- data/spec/grape/config_spec.rb +17 -0
- data/spec/grape/dsl/callbacks_spec.rb +3 -2
- data/spec/grape/dsl/desc_spec.rb +43 -17
- data/spec/grape/dsl/headers_spec.rb +40 -10
- data/spec/grape/dsl/helpers_spec.rb +6 -5
- data/spec/grape/dsl/inside_route_spec.rb +189 -38
- data/spec/grape/dsl/logger_spec.rb +17 -19
- data/spec/grape/dsl/middleware_spec.rb +11 -2
- data/spec/grape/dsl/parameters_spec.rb +3 -1
- data/spec/grape/dsl/request_response_spec.rb +8 -7
- data/spec/grape/dsl/routing_spec.rb +22 -9
- data/spec/grape/dsl/settings_spec.rb +1 -1
- data/spec/grape/dsl/validations_spec.rb +1 -16
- data/spec/grape/endpoint/declared_spec.rb +846 -0
- data/spec/grape/endpoint_spec.rb +136 -577
- data/spec/grape/entity_spec.rb +31 -24
- data/spec/grape/exceptions/base_spec.rb +81 -0
- data/spec/grape/exceptions/body_parse_errors_spec.rb +4 -1
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +65 -23
- data/spec/grape/exceptions/invalid_formatter_spec.rb +1 -1
- data/spec/grape/exceptions/invalid_response_spec.rb +11 -0
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +2 -2
- data/spec/grape/exceptions/missing_group_type_spec.rb +21 -0
- data/spec/grape/exceptions/missing_mime_type_spec.rb +1 -1
- data/spec/grape/exceptions/missing_option_spec.rb +2 -2
- data/spec/grape/exceptions/unknown_options_spec.rb +1 -1
- data/spec/grape/exceptions/unknown_validator_spec.rb +1 -1
- data/spec/grape/exceptions/unsupported_group_type_spec.rb +23 -0
- data/spec/grape/exceptions/validation_errors_spec.rb +21 -15
- data/spec/grape/exceptions/validation_spec.rb +6 -4
- data/spec/grape/extensions/param_builders/hash_spec.rb +8 -8
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +9 -9
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +9 -9
- data/spec/grape/integration/global_namespace_function_spec.rb +2 -2
- data/spec/grape/integration/rack_sendfile_spec.rb +14 -10
- data/spec/grape/integration/rack_spec.rb +25 -8
- data/spec/grape/loading_spec.rb +9 -9
- data/spec/grape/middleware/auth/base_spec.rb +2 -1
- data/spec/grape/middleware/auth/dsl_spec.rb +19 -10
- data/spec/grape/middleware/auth/strategies_spec.rb +62 -22
- data/spec/grape/middleware/base_spec.rb +36 -17
- data/spec/grape/middleware/error_spec.rb +11 -4
- data/spec/grape/middleware/exception_spec.rb +112 -162
- data/spec/grape/middleware/formatter_spec.rb +65 -29
- data/spec/grape/middleware/globals_spec.rb +8 -5
- data/spec/grape/middleware/stack_spec.rb +25 -13
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +3 -2
- data/spec/grape/middleware/versioner/header_spec.rb +37 -14
- data/spec/grape/middleware/versioner/param_spec.rb +8 -2
- data/spec/grape/middleware/versioner/path_spec.rb +6 -2
- data/spec/grape/middleware/versioner_spec.rb +2 -2
- data/spec/grape/named_api_spec.rb +19 -0
- data/spec/grape/parser_spec.rb +10 -6
- data/spec/grape/path_spec.rb +53 -53
- data/spec/grape/presenters/presenter_spec.rb +8 -7
- data/spec/grape/request_spec.rb +29 -3
- data/spec/grape/util/inheritable_setting_spec.rb +9 -8
- data/spec/grape/util/inheritable_values_spec.rb +5 -3
- data/spec/grape/util/reverse_stackable_values_spec.rb +5 -2
- data/spec/grape/util/stackable_values_spec.rb +10 -7
- data/spec/grape/util/strict_hash_configuration_spec.rb +2 -1
- data/spec/grape/validations/attributes_doc_spec.rb +153 -0
- data/spec/grape/validations/instance_behaivour_spec.rb +13 -14
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +40 -0
- data/spec/grape/validations/params_scope_spec.rb +568 -99
- data/spec/grape/validations/single_attribute_iterator_spec.rb +57 -0
- data/spec/grape/validations/types/array_coercer_spec.rb +33 -0
- data/spec/grape/validations/types/primitive_coercer_spec.rb +150 -0
- data/spec/grape/validations/types/set_coercer_spec.rb +32 -0
- data/spec/grape/validations/types_spec.rb +44 -45
- data/spec/grape/validations/validators/all_or_none_spec.rb +134 -32
- data/spec/grape/validations/validators/allow_blank_spec.rb +137 -141
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +169 -31
- data/spec/grape/validations/validators/coerce_spec.rb +491 -151
- data/spec/grape/validations/validators/default_spec.rb +242 -78
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +198 -40
- data/spec/grape/validations/validators/except_values_spec.rb +6 -5
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +181 -30
- data/spec/grape/validations/validators/presence_spec.rb +45 -2
- data/spec/grape/validations/validators/regexp_spec.rb +27 -33
- data/spec/grape/validations/validators/same_as_spec.rb +57 -0
- data/spec/grape/validations/validators/values_spec.rb +227 -180
- data/spec/grape/validations_spec.rb +502 -72
- data/spec/integration/eager_load/eager_load_spec.rb +15 -0
- data/spec/integration/multi_json/json_spec.rb +2 -2
- data/spec/integration/multi_xml/xml_spec.rb +2 -2
- data/spec/shared/versioning_examples.rb +34 -29
- data/spec/spec_helper.rb +31 -5
- data/spec/support/basic_auth_encode_helpers.rb +3 -1
- data/spec/support/chunks.rb +14 -0
- data/spec/support/content_type_helpers.rb +2 -0
- data/spec/support/endpoint_faker.rb +2 -0
- data/spec/support/file_streamer.rb +2 -0
- data/spec/support/integer_helpers.rb +2 -0
- data/spec/support/versioned_helpers.rb +8 -8
- metadata +111 -61
- data/Appraisals +0 -32
- data/Dangerfile +0 -2
- data/Gemfile +0 -33
- data/Gemfile.lock +0 -231
- data/Guardfile +0 -10
- data/RELEASING.md +0 -111
- data/Rakefile +0 -25
- data/benchmark/simple.rb +0 -27
- data/benchmark/simple_with_type_coercer.rb +0 -22
- data/gemfiles/multi_json.gemfile +0 -35
- data/gemfiles/multi_xml.gemfile +0 -35
- data/gemfiles/rack_1.5.2.gemfile +0 -35
- data/gemfiles/rack_edge.gemfile +0 -35
- data/gemfiles/rails_3.gemfile +0 -36
- data/gemfiles/rails_4.gemfile +0 -35
- data/gemfiles/rails_5.gemfile +0 -35
- data/gemfiles/rails_edge.gemfile +0 -35
- data/lib/grape/extensions/deep_hash_with_indifferent_access.rb +0 -18
- data/lib/grape/util/content_types.rb +0 -26
- data/lib/grape/validations/types/virtus_collection_patch.rb +0 -16
- data/lib/grape/validations/validators/all_or_none.rb +0 -20
- data/lib/grape/validations/validators/allow_blank.rb +0 -16
- data/lib/grape/validations/validators/as.rb +0 -15
- data/lib/grape/validations/validators/at_least_one_of.rb +0 -20
- data/lib/grape/validations/validators/coerce.rb +0 -74
- data/lib/grape/validations/validators/default.rb +0 -48
- data/lib/grape/validations/validators/exactly_one_of.rb +0 -29
- data/lib/grape/validations/validators/except_values.rb +0 -20
- data/lib/grape/validations/validators/mutual_exclusion.rb +0 -25
- data/lib/grape/validations/validators/presence.rb +0 -10
- data/lib/grape/validations/validators/regexp.rb +0 -11
- data/lib/grape/validations/validators/values.rb +0 -71
- data/pkg/grape-0.17.0.gem +0 -0
- data/pkg/grape-0.19.0.gem +0 -0
- data/spec/grape/dsl/configuration_spec.rb +0 -14
- data/spec/grape/validations/attributes_iterator_spec.rb +0 -4
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
describe Grape::Validations::ParamsScope do
|
4
4
|
subject do
|
@@ -9,86 +9,6 @@ describe Grape::Validations::ParamsScope do
|
|
9
9
|
subject
|
10
10
|
end
|
11
11
|
|
12
|
-
context 'setting a default' do
|
13
|
-
let(:documentation) { subject.routes.first.params }
|
14
|
-
|
15
|
-
context 'when the default value is truthy' do
|
16
|
-
before do
|
17
|
-
subject.params do
|
18
|
-
optional :int, type: Integer, default: 42
|
19
|
-
end
|
20
|
-
subject.get
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'adds documentation about the default value' do
|
24
|
-
expect(documentation).to have_key('int')
|
25
|
-
expect(documentation['int']).to have_key(:default)
|
26
|
-
expect(documentation['int'][:default]).to eq(42)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'when the default value is false' do
|
31
|
-
before do
|
32
|
-
subject.params do
|
33
|
-
optional :bool, type: Virtus::Attribute::Boolean, default: false
|
34
|
-
end
|
35
|
-
subject.get
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'adds documentation about the default value' do
|
39
|
-
expect(documentation).to have_key('bool')
|
40
|
-
expect(documentation['bool']).to have_key(:default)
|
41
|
-
expect(documentation['bool'][:default]).to eq(false)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context 'when the default value is nil' do
|
46
|
-
before do
|
47
|
-
subject.params do
|
48
|
-
optional :object, type: Object, default: nil
|
49
|
-
end
|
50
|
-
subject.get
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'adds documentation about the default value' do
|
54
|
-
expect(documentation).to have_key('object')
|
55
|
-
expect(documentation['object']).to have_key(:default)
|
56
|
-
expect(documentation['object'][:default]).to eq(nil)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
context 'without a default' do
|
62
|
-
before do
|
63
|
-
subject.params do
|
64
|
-
optional :object, type: Object
|
65
|
-
end
|
66
|
-
subject.get
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'does not add documentation for the default value' do
|
70
|
-
documentation = subject.routes.first.params
|
71
|
-
expect(documentation).to have_key('object')
|
72
|
-
expect(documentation['object']).not_to have_key(:default)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
context 'setting description' do
|
77
|
-
%i[desc description].each do |description_type|
|
78
|
-
it "allows setting #{description_type}" do
|
79
|
-
subject.params do
|
80
|
-
requires :int, type: Integer, description_type => 'My very nice integer'
|
81
|
-
end
|
82
|
-
subject.get '/single' do
|
83
|
-
'int works'
|
84
|
-
end
|
85
|
-
get '/single', int: 420
|
86
|
-
expect(last_response.status).to eq(200)
|
87
|
-
expect(last_response.body).to eq('int works')
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
12
|
context 'when using custom types' do
|
93
13
|
module ParamsScopeSpec
|
94
14
|
class CustomType
|
@@ -96,6 +16,7 @@ describe Grape::Validations::ParamsScope do
|
|
96
16
|
|
97
17
|
def self.parse(value)
|
98
18
|
raise if value == 'invalid'
|
19
|
+
|
99
20
|
new(value)
|
100
21
|
end
|
101
22
|
|
@@ -121,14 +42,14 @@ describe Grape::Validations::ParamsScope do
|
|
121
42
|
end
|
122
43
|
end
|
123
44
|
|
124
|
-
context 'param
|
45
|
+
context 'param renaming' do
|
125
46
|
it do
|
126
47
|
subject.params do
|
127
48
|
requires :foo, as: :bar
|
128
49
|
optional :super, as: :hiper
|
129
50
|
end
|
130
|
-
subject.get('/
|
131
|
-
get '/
|
51
|
+
subject.get('/renaming') { "#{declared(params)['bar']}-#{declared(params)['hiper']}" }
|
52
|
+
get '/renaming', foo: 'any', super: 'any2'
|
132
53
|
|
133
54
|
expect(last_response.status).to eq(200)
|
134
55
|
expect(last_response.body).to eq('any-any2')
|
@@ -138,23 +59,68 @@ describe Grape::Validations::ParamsScope do
|
|
138
59
|
subject.params do
|
139
60
|
requires :foo, as: :bar, type: String, coerce_with: ->(c) { c.strip }
|
140
61
|
end
|
141
|
-
subject.get('/
|
142
|
-
get '/
|
62
|
+
subject.get('/renaming-coerced') { "#{params['bar']}-#{params['foo']}" }
|
63
|
+
get '/renaming-coerced', foo: ' there we go '
|
143
64
|
|
144
65
|
expect(last_response.status).to eq(200)
|
145
|
-
expect(last_response.body).to eq('there we go
|
66
|
+
expect(last_response.body).to eq('-there we go')
|
146
67
|
end
|
147
68
|
|
148
69
|
it do
|
149
70
|
subject.params do
|
150
71
|
requires :foo, as: :bar, allow_blank: false
|
151
72
|
end
|
152
|
-
subject.get('/
|
153
|
-
get '/
|
73
|
+
subject.get('/renaming-not-blank') {}
|
74
|
+
get '/renaming-not-blank', foo: ''
|
154
75
|
|
155
76
|
expect(last_response.status).to eq(400)
|
156
77
|
expect(last_response.body).to eq('foo is empty')
|
157
78
|
end
|
79
|
+
|
80
|
+
it do
|
81
|
+
subject.params do
|
82
|
+
requires :foo, as: :bar, allow_blank: false
|
83
|
+
end
|
84
|
+
subject.get('/renaming-not-blank-with-value') {}
|
85
|
+
get '/renaming-not-blank-with-value', foo: 'any'
|
86
|
+
|
87
|
+
expect(last_response.status).to eq(200)
|
88
|
+
end
|
89
|
+
|
90
|
+
it do
|
91
|
+
subject.params do
|
92
|
+
requires :foo, as: :baz, type: Hash do
|
93
|
+
requires :bar, as: :qux
|
94
|
+
end
|
95
|
+
end
|
96
|
+
subject.get('/nested-renaming') { declared(params).to_json }
|
97
|
+
get '/nested-renaming', foo: { bar: 'any' }
|
98
|
+
|
99
|
+
expect(last_response.status).to eq(200)
|
100
|
+
expect(last_response.body).to eq('{"baz":{"qux":"any"}}')
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'renaming can be defined before default' do
|
104
|
+
subject.params do
|
105
|
+
optional :foo, as: :bar, default: 'before'
|
106
|
+
end
|
107
|
+
subject.get('/rename-before-default') { declared(params)[:bar] }
|
108
|
+
get '/rename-before-default'
|
109
|
+
|
110
|
+
expect(last_response.status).to eq(200)
|
111
|
+
expect(last_response.body).to eq('before')
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'renaming can be defined after default' do
|
115
|
+
subject.params do
|
116
|
+
optional :foo, default: 'after', as: :bar
|
117
|
+
end
|
118
|
+
subject.get('/rename-after-default') { declared(params)[:bar] }
|
119
|
+
get '/rename-after-default'
|
120
|
+
|
121
|
+
expect(last_response.status).to eq(200)
|
122
|
+
expect(last_response.body).to eq('after')
|
123
|
+
end
|
158
124
|
end
|
159
125
|
|
160
126
|
context 'array without coerce type explicitly given' do
|
@@ -244,7 +210,7 @@ describe Grape::Validations::ParamsScope do
|
|
244
210
|
it 'does not raise an exception' do
|
245
211
|
expect do
|
246
212
|
subject.params { optional :numbers, type: Array[Integer], values: 0..2, default: 0..2 }
|
247
|
-
end.
|
213
|
+
end.not_to raise_error
|
248
214
|
end
|
249
215
|
end
|
250
216
|
|
@@ -252,7 +218,7 @@ describe Grape::Validations::ParamsScope do
|
|
252
218
|
it 'does not raise an exception' do
|
253
219
|
expect do
|
254
220
|
subject.params { optional :numbers, type: Array[Integer], values: [0, 1, 2], default: [1, 0] }
|
255
|
-
end.
|
221
|
+
end.not_to raise_error
|
256
222
|
end
|
257
223
|
end
|
258
224
|
end
|
@@ -290,7 +256,7 @@ describe Grape::Validations::ParamsScope do
|
|
290
256
|
requires :b
|
291
257
|
end
|
292
258
|
end
|
293
|
-
end.to raise_error Grape::Exceptions::
|
259
|
+
end.to raise_error Grape::Exceptions::MissingGroupType
|
294
260
|
|
295
261
|
expect do
|
296
262
|
subject.params do
|
@@ -298,7 +264,7 @@ describe Grape::Validations::ParamsScope do
|
|
298
264
|
requires :b
|
299
265
|
end
|
300
266
|
end
|
301
|
-
end.to raise_error Grape::Exceptions::
|
267
|
+
end.to raise_error Grape::Exceptions::MissingGroupType
|
302
268
|
end
|
303
269
|
|
304
270
|
it 'allows Hash as type' do
|
@@ -358,7 +324,7 @@ describe Grape::Validations::ParamsScope do
|
|
358
324
|
requires :b
|
359
325
|
end
|
360
326
|
end
|
361
|
-
end.to raise_error Grape::Exceptions::
|
327
|
+
end.to raise_error Grape::Exceptions::UnsupportedGroupType
|
362
328
|
|
363
329
|
expect do
|
364
330
|
subject.params do
|
@@ -366,7 +332,7 @@ describe Grape::Validations::ParamsScope do
|
|
366
332
|
requires :b
|
367
333
|
end
|
368
334
|
end
|
369
|
-
end.to raise_error Grape::Exceptions::
|
335
|
+
end.to raise_error Grape::Exceptions::UnsupportedGroupType
|
370
336
|
end
|
371
337
|
end
|
372
338
|
|
@@ -476,10 +442,53 @@ describe Grape::Validations::ParamsScope do
|
|
476
442
|
requires :c
|
477
443
|
end
|
478
444
|
end
|
479
|
-
end.
|
445
|
+
end.not_to raise_error
|
446
|
+
end
|
447
|
+
|
448
|
+
it 'does not raise an error if when using nested given' do
|
449
|
+
expect do
|
450
|
+
subject.params do
|
451
|
+
optional :a, type: Hash do
|
452
|
+
requires :b
|
453
|
+
end
|
454
|
+
given :a do
|
455
|
+
requires :c
|
456
|
+
given :c do
|
457
|
+
requires :d
|
458
|
+
end
|
459
|
+
end
|
460
|
+
end
|
461
|
+
end.not_to raise_error
|
462
|
+
end
|
463
|
+
|
464
|
+
it 'allows nested dependent parameters' do
|
465
|
+
subject.params do
|
466
|
+
optional :a
|
467
|
+
given a: ->(val) { val == 'a' } do
|
468
|
+
optional :b
|
469
|
+
given b: ->(val) { val == 'b' } do
|
470
|
+
optional :c
|
471
|
+
given c: ->(val) { val == 'c' } do
|
472
|
+
requires :d
|
473
|
+
end
|
474
|
+
end
|
475
|
+
end
|
476
|
+
end
|
477
|
+
subject.get('/') { declared(params).to_json }
|
478
|
+
|
479
|
+
get '/'
|
480
|
+
expect(last_response.status).to eq 200
|
481
|
+
|
482
|
+
get '/', a: 'a', b: 'b', c: 'c'
|
483
|
+
expect(last_response.status).to eq 400
|
484
|
+
expect(last_response.body).to eq 'd is missing'
|
485
|
+
|
486
|
+
get '/', a: 'a', b: 'b', c: 'c', d: 'd'
|
487
|
+
expect(last_response.status).to eq 200
|
488
|
+
expect(last_response.body).to eq({ a: 'a', b: 'b', c: 'c', d: 'd' }.to_json)
|
480
489
|
end
|
481
490
|
|
482
|
-
it 'allows
|
491
|
+
it 'allows renaming of dependent parameters' do
|
483
492
|
subject.params do
|
484
493
|
optional :a
|
485
494
|
given :a do
|
@@ -494,7 +503,46 @@ describe Grape::Validations::ParamsScope do
|
|
494
503
|
body = JSON.parse(last_response.body)
|
495
504
|
|
496
505
|
expect(body.keys).to include('c')
|
497
|
-
expect(body.keys).
|
506
|
+
expect(body.keys).not_to include('b')
|
507
|
+
end
|
508
|
+
|
509
|
+
it 'allows renaming of dependent on parameter' do
|
510
|
+
subject.params do
|
511
|
+
optional :a, as: :b
|
512
|
+
given a: ->(val) { val == 'x' } do
|
513
|
+
requires :c
|
514
|
+
end
|
515
|
+
end
|
516
|
+
subject.get('/') { declared(params) }
|
517
|
+
|
518
|
+
get '/', a: 'x'
|
519
|
+
expect(last_response.status).to eq 400
|
520
|
+
expect(last_response.body).to eq 'c is missing'
|
521
|
+
|
522
|
+
get '/', a: 'y'
|
523
|
+
expect(last_response.status).to eq 200
|
524
|
+
end
|
525
|
+
|
526
|
+
it 'does not raise if the dependent parameter is not the renamed one' do
|
527
|
+
expect do
|
528
|
+
subject.params do
|
529
|
+
optional :a, as: :b
|
530
|
+
given :a do
|
531
|
+
requires :c
|
532
|
+
end
|
533
|
+
end
|
534
|
+
end.not_to raise_error
|
535
|
+
end
|
536
|
+
|
537
|
+
it 'raises an error if the dependent parameter is the renamed one' do
|
538
|
+
expect do
|
539
|
+
subject.params do
|
540
|
+
optional :a, as: :b
|
541
|
+
given :b do
|
542
|
+
requires :c
|
543
|
+
end
|
544
|
+
end
|
545
|
+
end.to raise_error(Grape::Exceptions::UnknownParameter)
|
498
546
|
end
|
499
547
|
|
500
548
|
it 'does not validate nested requires when given is false' do
|
@@ -537,6 +585,32 @@ describe Grape::Validations::ParamsScope do
|
|
537
585
|
expect(last_response.status).to eq(200)
|
538
586
|
end
|
539
587
|
|
588
|
+
it 'detect unmet nested dependency' do
|
589
|
+
subject.params do
|
590
|
+
requires :a, type: String, allow_blank: false, values: %w[x y z]
|
591
|
+
given a: ->(val) { val == 'z' } do
|
592
|
+
requires :inner3, type: Array, allow_blank: false do
|
593
|
+
requires :bar, type: String, allow_blank: false
|
594
|
+
given bar: ->(val) { val == 'b' } do
|
595
|
+
requires :baz, type: Array do
|
596
|
+
optional :baz_category, type: String
|
597
|
+
end
|
598
|
+
end
|
599
|
+
given bar: ->(val) { val == 'c' } do
|
600
|
+
requires :baz, type: Array do
|
601
|
+
requires :baz_category, type: String
|
602
|
+
end
|
603
|
+
end
|
604
|
+
end
|
605
|
+
end
|
606
|
+
end
|
607
|
+
subject.get('/nested-dependency') { declared(params).to_json }
|
608
|
+
|
609
|
+
get '/nested-dependency', a: 'z', inner3: [{ bar: 'c', baz: [{ unrelated: 'nope' }] }]
|
610
|
+
expect(last_response.status).to eq(400)
|
611
|
+
expect(last_response.body).to eq 'inner3[0][baz][0][baz_category] is missing'
|
612
|
+
end
|
613
|
+
|
540
614
|
it 'includes the parameter within #declared(params)' do
|
541
615
|
get '/test', a: true, b: true
|
542
616
|
|
@@ -590,9 +664,366 @@ describe Grape::Validations::ParamsScope do
|
|
590
664
|
get '/nested', bar: { a: true, c: { b: 'yes' } }
|
591
665
|
expect(JSON.parse(last_response.body)).to eq('bar' => { 'a' => 'true', 'c' => { 'b' => 'yes' } })
|
592
666
|
end
|
667
|
+
|
668
|
+
context 'when the dependent parameter is not present #declared(params)' do
|
669
|
+
context 'lateral parameter' do
|
670
|
+
before do
|
671
|
+
[true, false].each do |evaluate_given|
|
672
|
+
subject.params do
|
673
|
+
optional :a
|
674
|
+
given :a do
|
675
|
+
optional :b
|
676
|
+
end
|
677
|
+
end
|
678
|
+
subject.get("/evaluate_given_#{evaluate_given}") { declared(params, evaluate_given: evaluate_given).to_json }
|
679
|
+
end
|
680
|
+
end
|
681
|
+
|
682
|
+
it 'evaluate_given_false' do
|
683
|
+
get '/evaluate_given_false', b: 'b'
|
684
|
+
expect(JSON.parse(last_response.body)).to eq('a' => nil, 'b' => 'b')
|
685
|
+
end
|
686
|
+
|
687
|
+
it 'evaluate_given_true' do
|
688
|
+
get '/evaluate_given_true', b: 'b'
|
689
|
+
expect(JSON.parse(last_response.body)).to eq('a' => nil)
|
690
|
+
end
|
691
|
+
end
|
692
|
+
|
693
|
+
context 'lateral hash parameter' do
|
694
|
+
before do
|
695
|
+
[true, false].each do |evaluate_given|
|
696
|
+
subject.params do
|
697
|
+
optional :a, values: %w[x y]
|
698
|
+
given a: ->(a) { a == 'x' } do
|
699
|
+
optional :b, type: Hash do
|
700
|
+
optional :c
|
701
|
+
end
|
702
|
+
optional :e
|
703
|
+
end
|
704
|
+
given a: ->(a) { a == 'y' } do
|
705
|
+
optional :b, type: Hash do
|
706
|
+
optional :d
|
707
|
+
end
|
708
|
+
optional :f
|
709
|
+
end
|
710
|
+
end
|
711
|
+
subject.get("/evaluate_given_#{evaluate_given}") { declared(params, evaluate_given: evaluate_given).to_json }
|
712
|
+
end
|
713
|
+
end
|
714
|
+
|
715
|
+
it 'evaluate_given_false' do
|
716
|
+
get '/evaluate_given_false', a: 'x'
|
717
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'x', 'b' => { 'd' => nil }, 'e' => nil, 'f' => nil)
|
718
|
+
|
719
|
+
get '/evaluate_given_false', a: 'y'
|
720
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'y', 'b' => { 'd' => nil }, 'e' => nil, 'f' => nil)
|
721
|
+
end
|
722
|
+
|
723
|
+
it 'evaluate_given_true' do
|
724
|
+
get '/evaluate_given_true', a: 'x'
|
725
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'x', 'b' => { 'c' => nil }, 'e' => nil)
|
726
|
+
|
727
|
+
get '/evaluate_given_true', a: 'y'
|
728
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'y', 'b' => { 'd' => nil }, 'f' => nil)
|
729
|
+
end
|
730
|
+
end
|
731
|
+
|
732
|
+
context 'lateral parameter within lateral hash parameter' do
|
733
|
+
before do
|
734
|
+
[true, false].each do |evaluate_given|
|
735
|
+
subject.params do
|
736
|
+
optional :a, values: %w[x y]
|
737
|
+
given a: ->(a) { a == 'x' } do
|
738
|
+
optional :b, type: Hash do
|
739
|
+
optional :c
|
740
|
+
given :c do
|
741
|
+
optional :g
|
742
|
+
optional :e, type: Hash do
|
743
|
+
optional :h
|
744
|
+
end
|
745
|
+
end
|
746
|
+
end
|
747
|
+
end
|
748
|
+
given a: ->(a) { a == 'y' } do
|
749
|
+
optional :b, type: Hash do
|
750
|
+
optional :d
|
751
|
+
given :d do
|
752
|
+
optional :f
|
753
|
+
optional :e, type: Hash do
|
754
|
+
optional :i
|
755
|
+
end
|
756
|
+
end
|
757
|
+
end
|
758
|
+
end
|
759
|
+
end
|
760
|
+
subject.get("/evaluate_given_#{evaluate_given}") { declared(params, evaluate_given: evaluate_given).to_json }
|
761
|
+
end
|
762
|
+
end
|
763
|
+
|
764
|
+
it 'evaluate_given_false' do
|
765
|
+
get '/evaluate_given_false', a: 'x'
|
766
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'x', 'b' => { 'd' => nil, 'f' => nil, 'e' => { 'i' => nil } })
|
767
|
+
|
768
|
+
get '/evaluate_given_false', a: 'x', b: { c: 'c' }
|
769
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'x', 'b' => { 'd' => nil, 'f' => nil, 'e' => { 'i' => nil } })
|
770
|
+
|
771
|
+
get '/evaluate_given_false', a: 'y'
|
772
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'y', 'b' => { 'd' => nil, 'f' => nil, 'e' => { 'i' => nil } })
|
773
|
+
|
774
|
+
get '/evaluate_given_false', a: 'y', b: { d: 'd' }
|
775
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'y', 'b' => { 'd' => 'd', 'f' => nil, 'e' => { 'i' => nil } })
|
776
|
+
end
|
777
|
+
|
778
|
+
it 'evaluate_given_true' do
|
779
|
+
get '/evaluate_given_true', a: 'x'
|
780
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'x', 'b' => { 'c' => nil })
|
781
|
+
|
782
|
+
get '/evaluate_given_true', a: 'x', b: { c: 'c' }
|
783
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'x', 'b' => { 'c' => 'c', 'g' => nil, 'e' => { 'h' => nil } })
|
784
|
+
|
785
|
+
get '/evaluate_given_true', a: 'y'
|
786
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'y', 'b' => { 'd' => nil })
|
787
|
+
|
788
|
+
get '/evaluate_given_true', a: 'y', b: { d: 'd' }
|
789
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'y', 'b' => { 'd' => 'd', 'f' => nil, 'e' => { 'i' => nil } })
|
790
|
+
end
|
791
|
+
end
|
792
|
+
|
793
|
+
context 'lateral parameter within an array param' do
|
794
|
+
before do
|
795
|
+
[true, false].each do |evaluate_given|
|
796
|
+
subject.params do
|
797
|
+
optional :array, type: Array do
|
798
|
+
optional :a
|
799
|
+
given :a do
|
800
|
+
optional :b
|
801
|
+
end
|
802
|
+
end
|
803
|
+
end
|
804
|
+
subject.post("/evaluate_given_#{evaluate_given}") do
|
805
|
+
declared(params, evaluate_given: evaluate_given).to_json
|
806
|
+
end
|
807
|
+
end
|
808
|
+
end
|
809
|
+
|
810
|
+
it 'evaluate_given_false' do
|
811
|
+
post '/evaluate_given_false', { array: [{ b: 'b' }, { a: 'a', b: 'b' }] }.to_json, 'CONTENT_TYPE' => 'application/json'
|
812
|
+
expect(JSON.parse(last_response.body)).to eq('array' => [{ 'a' => nil, 'b' => 'b' }, { 'a' => 'a', 'b' => 'b' }])
|
813
|
+
end
|
814
|
+
|
815
|
+
it 'evaluate_given_true' do
|
816
|
+
post '/evaluate_given_true', { array: [{ b: 'b' }, { a: 'a', b: 'b' }] }.to_json, 'CONTENT_TYPE' => 'application/json'
|
817
|
+
expect(JSON.parse(last_response.body)).to eq('array' => [{ 'a' => nil }, { 'a' => 'a', 'b' => 'b' }])
|
818
|
+
end
|
819
|
+
end
|
820
|
+
|
821
|
+
context 'nested given parameter' do
|
822
|
+
before do
|
823
|
+
[true, false].each do |evaluate_given|
|
824
|
+
subject.params do
|
825
|
+
optional :a
|
826
|
+
optional :c
|
827
|
+
given :a do
|
828
|
+
given :c do
|
829
|
+
optional :b
|
830
|
+
end
|
831
|
+
end
|
832
|
+
end
|
833
|
+
subject.post("/evaluate_given_#{evaluate_given}") do
|
834
|
+
declared(params, evaluate_given: evaluate_given).to_json
|
835
|
+
end
|
836
|
+
end
|
837
|
+
end
|
838
|
+
|
839
|
+
it 'evaluate_given_false' do
|
840
|
+
post '/evaluate_given_false', { a: 'a', b: 'b' }.to_json, 'CONTENT_TYPE' => 'application/json'
|
841
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'a', 'b' => 'b', 'c' => nil)
|
842
|
+
|
843
|
+
post '/evaluate_given_false', { c: 'c', b: 'b' }.to_json, 'CONTENT_TYPE' => 'application/json'
|
844
|
+
expect(JSON.parse(last_response.body)).to eq('a' => nil, 'b' => 'b', 'c' => 'c')
|
845
|
+
|
846
|
+
post '/evaluate_given_false', { a: 'a', c: 'c', b: 'b' }.to_json, 'CONTENT_TYPE' => 'application/json'
|
847
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'a', 'b' => 'b', 'c' => 'c')
|
848
|
+
end
|
849
|
+
|
850
|
+
it 'evaluate_given_true' do
|
851
|
+
post '/evaluate_given_true', { a: 'a', b: 'b' }.to_json, 'CONTENT_TYPE' => 'application/json'
|
852
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'a', 'c' => nil)
|
853
|
+
|
854
|
+
post '/evaluate_given_true', { c: 'c', b: 'b' }.to_json, 'CONTENT_TYPE' => 'application/json'
|
855
|
+
expect(JSON.parse(last_response.body)).to eq('a' => nil, 'c' => 'c')
|
856
|
+
|
857
|
+
post '/evaluate_given_true', { a: 'a', c: 'c', b: 'b' }.to_json, 'CONTENT_TYPE' => 'application/json'
|
858
|
+
expect(JSON.parse(last_response.body)).to eq('a' => 'a', 'b' => 'b', 'c' => 'c')
|
859
|
+
end
|
860
|
+
end
|
861
|
+
|
862
|
+
context 'nested given parameter within an array param' do
|
863
|
+
before do
|
864
|
+
[true, false].each do |evaluate_given|
|
865
|
+
subject.params do
|
866
|
+
optional :array, type: Array do
|
867
|
+
optional :a
|
868
|
+
optional :c
|
869
|
+
given :a do
|
870
|
+
given :c do
|
871
|
+
optional :b
|
872
|
+
end
|
873
|
+
end
|
874
|
+
end
|
875
|
+
end
|
876
|
+
subject.post("/evaluate_given_#{evaluate_given}") do
|
877
|
+
declared(params, evaluate_given: evaluate_given).to_json
|
878
|
+
end
|
879
|
+
end
|
880
|
+
end
|
881
|
+
|
882
|
+
let :evaluate_given_params do
|
883
|
+
{
|
884
|
+
array: [
|
885
|
+
{ a: 'a', b: 'b' },
|
886
|
+
{ c: 'c', b: 'b' },
|
887
|
+
{ a: 'a', c: 'c', b: 'b' }
|
888
|
+
]
|
889
|
+
}
|
890
|
+
end
|
891
|
+
|
892
|
+
it 'evaluate_given_false' do
|
893
|
+
post '/evaluate_given_false', evaluate_given_params.to_json, 'CONTENT_TYPE' => 'application/json'
|
894
|
+
expect(JSON.parse(last_response.body)).to eq('array' => [{ 'a' => 'a', 'b' => 'b', 'c' => nil }, { 'a' => nil, 'b' => 'b', 'c' => 'c' }, { 'a' => 'a', 'b' => 'b', 'c' => 'c' }])
|
895
|
+
end
|
896
|
+
|
897
|
+
it 'evaluate_given_true' do
|
898
|
+
post '/evaluate_given_true', evaluate_given_params.to_json, 'CONTENT_TYPE' => 'application/json'
|
899
|
+
expect(JSON.parse(last_response.body)).to eq('array' => [{ 'a' => 'a', 'c' => nil }, { 'a' => nil, 'c' => 'c' }, { 'a' => 'a', 'b' => 'b', 'c' => 'c' }])
|
900
|
+
end
|
901
|
+
end
|
902
|
+
|
903
|
+
context 'nested given parameter within a nested given parameter within an array param' do
|
904
|
+
before do
|
905
|
+
[true, false].each do |evaluate_given|
|
906
|
+
subject.params do
|
907
|
+
optional :array, type: Array do
|
908
|
+
optional :a
|
909
|
+
optional :c
|
910
|
+
given :a do
|
911
|
+
given :c do
|
912
|
+
optional :array, type: Array do
|
913
|
+
optional :a
|
914
|
+
optional :c
|
915
|
+
given :a do
|
916
|
+
given :c do
|
917
|
+
optional :b
|
918
|
+
end
|
919
|
+
end
|
920
|
+
end
|
921
|
+
end
|
922
|
+
end
|
923
|
+
end
|
924
|
+
end
|
925
|
+
subject.post("/evaluate_given_#{evaluate_given}") do
|
926
|
+
declared(params, evaluate_given: evaluate_given).to_json
|
927
|
+
end
|
928
|
+
end
|
929
|
+
end
|
930
|
+
|
931
|
+
let :evaluate_given_params do
|
932
|
+
{
|
933
|
+
array: [{
|
934
|
+
a: 'a',
|
935
|
+
c: 'c',
|
936
|
+
array: [
|
937
|
+
{ a: 'a', b: 'b' },
|
938
|
+
{ c: 'c', b: 'b' },
|
939
|
+
{ a: 'a', c: 'c', b: 'b' }
|
940
|
+
]
|
941
|
+
}]
|
942
|
+
}
|
943
|
+
end
|
944
|
+
|
945
|
+
it 'evaluate_given_false' do
|
946
|
+
expected_response_hash = {
|
947
|
+
'array' => [{
|
948
|
+
'a' => 'a',
|
949
|
+
'c' => 'c',
|
950
|
+
'array' => [
|
951
|
+
{ 'a' => 'a', 'b' => 'b', 'c' => nil },
|
952
|
+
{ 'a' => nil, 'c' => 'c', 'b' => 'b' },
|
953
|
+
{ 'a' => 'a', 'c' => 'c', 'b' => 'b' }
|
954
|
+
]
|
955
|
+
}]
|
956
|
+
}
|
957
|
+
post '/evaluate_given_false', evaluate_given_params.to_json, 'CONTENT_TYPE' => 'application/json'
|
958
|
+
expect(JSON.parse(last_response.body)).to eq(expected_response_hash)
|
959
|
+
end
|
960
|
+
|
961
|
+
it 'evaluate_given_true' do
|
962
|
+
expected_response_hash = {
|
963
|
+
'array' => [{
|
964
|
+
'a' => 'a',
|
965
|
+
'c' => 'c',
|
966
|
+
'array' => [
|
967
|
+
{ 'a' => 'a', 'c' => nil },
|
968
|
+
{ 'a' => nil, 'c' => 'c' },
|
969
|
+
{ 'a' => 'a', 'b' => 'b', 'c' => 'c' }
|
970
|
+
]
|
971
|
+
}]
|
972
|
+
}
|
973
|
+
post '/evaluate_given_true', evaluate_given_params.to_json, 'CONTENT_TYPE' => 'application/json'
|
974
|
+
expect(JSON.parse(last_response.body)).to eq(expected_response_hash)
|
975
|
+
end
|
976
|
+
end
|
977
|
+
end
|
978
|
+
end
|
979
|
+
|
980
|
+
context 'default value in given block' do
|
981
|
+
before do
|
982
|
+
subject.params do
|
983
|
+
optional :a, values: %w[a b]
|
984
|
+
given a: ->(val) { val == 'a' } do
|
985
|
+
optional :b, default: 'default'
|
986
|
+
end
|
987
|
+
end
|
988
|
+
subject.get('/') { params.to_json }
|
989
|
+
end
|
990
|
+
|
991
|
+
context 'when dependency meets' do
|
992
|
+
it 'sets default value for dependent parameter' do
|
993
|
+
get '/', a: 'a'
|
994
|
+
expect(last_response.body).to eq({ a: 'a', b: 'default' }.to_json)
|
995
|
+
end
|
996
|
+
end
|
997
|
+
|
998
|
+
context 'when dependency does not meet' do
|
999
|
+
it 'does not set default value for dependent parameter' do
|
1000
|
+
get '/', a: 'b'
|
1001
|
+
expect(last_response.body).to eq({ a: 'b' }.to_json)
|
1002
|
+
end
|
1003
|
+
end
|
593
1004
|
end
|
594
1005
|
|
595
1006
|
context 'when validations are dependent on a parameter within an array param' do
|
1007
|
+
before do
|
1008
|
+
subject.params do
|
1009
|
+
requires :foos, type: Array do
|
1010
|
+
optional :foo
|
1011
|
+
given :foo do
|
1012
|
+
requires :bar
|
1013
|
+
end
|
1014
|
+
end
|
1015
|
+
end
|
1016
|
+
subject.get('/test') { 'ok' }
|
1017
|
+
end
|
1018
|
+
|
1019
|
+
it 'passes none Hash params' do
|
1020
|
+
get '/test', foos: ['']
|
1021
|
+
expect(last_response.status).to eq(200)
|
1022
|
+
expect(last_response.body).to eq('ok')
|
1023
|
+
end
|
1024
|
+
end
|
1025
|
+
|
1026
|
+
context 'when validations are dependent on a parameter within an array param within #declared(params).to_json' do
|
596
1027
|
before do
|
597
1028
|
subject.params do
|
598
1029
|
requires :foos, type: Array do
|
@@ -761,6 +1192,7 @@ describe Grape::Validations::ParamsScope do
|
|
761
1192
|
expect(last_response.body).to eq('one is missing, two is missing, three is missing')
|
762
1193
|
end
|
763
1194
|
end
|
1195
|
+
|
764
1196
|
context 'when fail_fast is defined it stops the validation' do
|
765
1197
|
it 'of other params' do
|
766
1198
|
subject.params do
|
@@ -773,6 +1205,7 @@ describe Grape::Validations::ParamsScope do
|
|
773
1205
|
expect(last_response.status).to eq(400)
|
774
1206
|
expect(last_response.body).to eq('one is missing')
|
775
1207
|
end
|
1208
|
+
|
776
1209
|
it 'for a single param' do
|
777
1210
|
subject.params do
|
778
1211
|
requires :one, allow_blank: false, regexp: /[0-9]+/, fail_fast: true
|
@@ -823,7 +1256,7 @@ describe Grape::Validations::ParamsScope do
|
|
823
1256
|
end
|
824
1257
|
|
825
1258
|
it 'prioritizes parameter validation over group validation' do
|
826
|
-
expect(last_response.body).
|
1259
|
+
expect(last_response.body).not_to include('address is empty')
|
827
1260
|
end
|
828
1261
|
end
|
829
1262
|
end
|
@@ -948,4 +1381,40 @@ describe Grape::Validations::ParamsScope do
|
|
948
1381
|
end
|
949
1382
|
end
|
950
1383
|
end
|
1384
|
+
|
1385
|
+
context 'with exactly_one_of validation for optional parameters within an Hash param' do
|
1386
|
+
before do
|
1387
|
+
subject.params do
|
1388
|
+
optional :memo, type: Hash do
|
1389
|
+
optional :text, type: String
|
1390
|
+
optional :custom_body, type: Hash, coerce_with: JSON
|
1391
|
+
exactly_one_of :text, :custom_body
|
1392
|
+
end
|
1393
|
+
end
|
1394
|
+
subject.get('test')
|
1395
|
+
end
|
1396
|
+
|
1397
|
+
context 'when correct data is provided' do
|
1398
|
+
it 'returns a successful response' do
|
1399
|
+
get 'test', memo: {}
|
1400
|
+
expect(last_response.status).to eq(200)
|
1401
|
+
|
1402
|
+
get 'test', memo: { text: 'HOGEHOGE' }
|
1403
|
+
expect(last_response.status).to eq(200)
|
1404
|
+
|
1405
|
+
get 'test', memo: { custom_body: '{ "xxx": "yyy" }' }
|
1406
|
+
expect(last_response.status).to eq(200)
|
1407
|
+
end
|
1408
|
+
end
|
1409
|
+
|
1410
|
+
context 'when invalid data is provided' do
|
1411
|
+
it 'returns a failure response' do
|
1412
|
+
get 'test', memo: { text: 'HOGEHOGE', custom_body: '{ "xxx": "yyy" }' }
|
1413
|
+
expect(last_response.status).to eq(400)
|
1414
|
+
|
1415
|
+
get 'test', memo: '{ "custom_body": "HOGE" }'
|
1416
|
+
expect(last_response.status).to eq(400)
|
1417
|
+
end
|
1418
|
+
end
|
1419
|
+
end
|
951
1420
|
end
|