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,23 +1,138 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
describe Grape::Validations::AllowBlankValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
default_format :json
|
3
|
+
describe Grape::Validations::Validators::AllowBlankValidator do
|
4
|
+
let_it_be(:app) do
|
5
|
+
Class.new(Grape::API) do
|
6
|
+
default_format :json
|
8
7
|
|
9
|
-
|
8
|
+
params do
|
9
|
+
requires :name, allow_blank: false
|
10
|
+
end
|
11
|
+
get '/disallow_blank'
|
12
|
+
|
13
|
+
params do
|
14
|
+
optional :name, type: String, allow_blank: false
|
15
|
+
end
|
16
|
+
get '/opt_disallow_string_blank'
|
17
|
+
|
18
|
+
params do
|
19
|
+
optional :name, allow_blank: false
|
20
|
+
end
|
21
|
+
get '/disallow_blank_optional_param'
|
22
|
+
|
23
|
+
params do
|
24
|
+
requires :name, allow_blank: true
|
25
|
+
end
|
26
|
+
get '/allow_blank'
|
27
|
+
|
28
|
+
params do
|
29
|
+
requires :val, type: DateTime, allow_blank: true
|
30
|
+
end
|
31
|
+
get '/allow_datetime_blank'
|
32
|
+
|
33
|
+
params do
|
34
|
+
requires :val, type: DateTime, allow_blank: false
|
35
|
+
end
|
36
|
+
get '/disallow_datetime_blank'
|
37
|
+
|
38
|
+
params do
|
39
|
+
requires :val, type: DateTime
|
40
|
+
end
|
41
|
+
get '/default_allow_datetime_blank'
|
42
|
+
|
43
|
+
params do
|
44
|
+
requires :val, type: Date, allow_blank: true
|
45
|
+
end
|
46
|
+
get '/allow_date_blank'
|
47
|
+
|
48
|
+
params do
|
49
|
+
requires :val, type: Integer, allow_blank: true
|
50
|
+
end
|
51
|
+
get '/allow_integer_blank'
|
52
|
+
|
53
|
+
params do
|
54
|
+
requires :val, type: Float, allow_blank: true
|
55
|
+
end
|
56
|
+
get '/allow_float_blank'
|
57
|
+
|
58
|
+
params do
|
59
|
+
requires :val, type: Integer, allow_blank: true
|
60
|
+
end
|
61
|
+
get '/allow_integer_blank'
|
62
|
+
|
63
|
+
params do
|
64
|
+
requires :val, type: Symbol, allow_blank: true
|
65
|
+
end
|
66
|
+
get '/allow_symbol_blank'
|
67
|
+
|
68
|
+
params do
|
69
|
+
requires :val, type: Grape::API::Boolean, allow_blank: true
|
70
|
+
end
|
71
|
+
get '/allow_boolean_blank'
|
72
|
+
|
73
|
+
params do
|
74
|
+
requires :val, type: Grape::API::Boolean, allow_blank: false
|
75
|
+
end
|
76
|
+
get '/disallow_boolean_blank'
|
77
|
+
|
78
|
+
params do
|
79
|
+
optional :user, type: Hash do
|
80
|
+
requires :name, allow_blank: false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
get '/disallow_blank_required_param_in_an_optional_group'
|
84
|
+
|
85
|
+
params do
|
86
|
+
optional :user, type: Hash do
|
87
|
+
requires :name, type: Date, allow_blank: true
|
88
|
+
end
|
89
|
+
end
|
90
|
+
get '/allow_blank_date_param_in_an_optional_group'
|
91
|
+
|
92
|
+
params do
|
93
|
+
optional :user, type: Hash do
|
94
|
+
optional :name, allow_blank: false
|
95
|
+
requires :age
|
96
|
+
end
|
97
|
+
end
|
98
|
+
get '/disallow_blank_optional_param_in_an_optional_group'
|
99
|
+
|
100
|
+
params do
|
101
|
+
requires :user, type: Hash do
|
10
102
|
requires :name, allow_blank: false
|
11
103
|
end
|
12
|
-
|
104
|
+
end
|
105
|
+
get '/disallow_blank_required_param_in_a_required_group'
|
106
|
+
|
107
|
+
params do
|
108
|
+
requires :user, type: Hash do
|
109
|
+
requires :name, allow_blank: false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
get '/disallow_string_value_in_a_required_hash_group'
|
113
|
+
|
114
|
+
params do
|
115
|
+
requires :user, type: Hash do
|
116
|
+
optional :name, allow_blank: false
|
117
|
+
end
|
118
|
+
end
|
119
|
+
get '/disallow_blank_optional_param_in_a_required_group'
|
120
|
+
|
121
|
+
params do
|
122
|
+
optional :user, type: Hash do
|
123
|
+
optional :name, allow_blank: false
|
124
|
+
end
|
125
|
+
end
|
126
|
+
get '/disallow_string_value_in_an_optional_hash_group'
|
13
127
|
|
128
|
+
resources :custom_message do
|
14
129
|
params do
|
15
|
-
|
130
|
+
requires :name, allow_blank: { value: false, message: 'has no value' }
|
16
131
|
end
|
17
|
-
get
|
132
|
+
get
|
18
133
|
|
19
134
|
params do
|
20
|
-
optional :name, allow_blank: false
|
135
|
+
optional :name, allow_blank: { value: false, message: 'has no value' }
|
21
136
|
end
|
22
137
|
get '/disallow_blank_optional_param'
|
23
138
|
|
@@ -32,7 +147,7 @@ describe Grape::Validations::AllowBlankValidator do
|
|
32
147
|
get '/allow_datetime_blank'
|
33
148
|
|
34
149
|
params do
|
35
|
-
requires :val, type: DateTime, allow_blank: false
|
150
|
+
requires :val, type: DateTime, allow_blank: { value: false, message: 'has no value' }
|
36
151
|
end
|
37
152
|
get '/disallow_datetime_blank'
|
38
153
|
|
@@ -67,18 +182,18 @@ describe Grape::Validations::AllowBlankValidator do
|
|
67
182
|
get '/allow_symbol_blank'
|
68
183
|
|
69
184
|
params do
|
70
|
-
requires :val, type: Boolean, allow_blank: true
|
185
|
+
requires :val, type: Grape::API::Boolean, allow_blank: true
|
71
186
|
end
|
72
187
|
get '/allow_boolean_blank'
|
73
188
|
|
74
189
|
params do
|
75
|
-
requires :val, type: Boolean, allow_blank: false
|
190
|
+
requires :val, type: Grape::API::Boolean, allow_blank: { value: false, message: 'has no value' }
|
76
191
|
end
|
77
192
|
get '/disallow_boolean_blank'
|
78
193
|
|
79
194
|
params do
|
80
195
|
optional :user, type: Hash do
|
81
|
-
requires :name, allow_blank: false
|
196
|
+
requires :name, allow_blank: { value: false, message: 'has no value' }
|
82
197
|
end
|
83
198
|
end
|
84
199
|
get '/disallow_blank_required_param_in_an_optional_group'
|
@@ -92,7 +207,7 @@ describe Grape::Validations::AllowBlankValidator do
|
|
92
207
|
|
93
208
|
params do
|
94
209
|
optional :user, type: Hash do
|
95
|
-
optional :name, allow_blank: false
|
210
|
+
optional :name, allow_blank: { value: false, message: 'has no value' }
|
96
211
|
requires :age
|
97
212
|
end
|
98
213
|
end
|
@@ -100,156 +215,35 @@ describe Grape::Validations::AllowBlankValidator do
|
|
100
215
|
|
101
216
|
params do
|
102
217
|
requires :user, type: Hash do
|
103
|
-
requires :name, allow_blank: false
|
218
|
+
requires :name, allow_blank: { value: false, message: 'has no value' }
|
104
219
|
end
|
105
220
|
end
|
106
221
|
get '/disallow_blank_required_param_in_a_required_group'
|
107
222
|
|
108
223
|
params do
|
109
224
|
requires :user, type: Hash do
|
110
|
-
requires :name, allow_blank: false
|
225
|
+
requires :name, allow_blank: { value: false, message: 'has no value' }
|
111
226
|
end
|
112
227
|
end
|
113
228
|
get '/disallow_string_value_in_a_required_hash_group'
|
114
229
|
|
115
230
|
params do
|
116
231
|
requires :user, type: Hash do
|
117
|
-
optional :name, allow_blank: false
|
232
|
+
optional :name, allow_blank: { value: false, message: 'has no value' }
|
118
233
|
end
|
119
234
|
end
|
120
235
|
get '/disallow_blank_optional_param_in_a_required_group'
|
121
236
|
|
122
237
|
params do
|
123
238
|
optional :user, type: Hash do
|
124
|
-
optional :name, allow_blank: false
|
125
|
-
end
|
126
|
-
end
|
127
|
-
get '/disallow_string_value_in_an_optional_hash_group'
|
128
|
-
|
129
|
-
resources :custom_message do
|
130
|
-
params do
|
131
|
-
requires :name, allow_blank: { value: false, message: 'has no value' }
|
132
|
-
end
|
133
|
-
get
|
134
|
-
|
135
|
-
params do
|
136
239
|
optional :name, allow_blank: { value: false, message: 'has no value' }
|
137
240
|
end
|
138
|
-
get '/disallow_blank_optional_param'
|
139
|
-
|
140
|
-
params do
|
141
|
-
requires :name, allow_blank: true
|
142
|
-
end
|
143
|
-
get '/allow_blank'
|
144
|
-
|
145
|
-
params do
|
146
|
-
requires :val, type: DateTime, allow_blank: true
|
147
|
-
end
|
148
|
-
get '/allow_datetime_blank'
|
149
|
-
|
150
|
-
params do
|
151
|
-
requires :val, type: DateTime, allow_blank: { value: false, message: 'has no value' }
|
152
|
-
end
|
153
|
-
get '/disallow_datetime_blank'
|
154
|
-
|
155
|
-
params do
|
156
|
-
requires :val, type: DateTime
|
157
|
-
end
|
158
|
-
get '/default_allow_datetime_blank'
|
159
|
-
|
160
|
-
params do
|
161
|
-
requires :val, type: Date, allow_blank: true
|
162
|
-
end
|
163
|
-
get '/allow_date_blank'
|
164
|
-
|
165
|
-
params do
|
166
|
-
requires :val, type: Integer, allow_blank: true
|
167
|
-
end
|
168
|
-
get '/allow_integer_blank'
|
169
|
-
|
170
|
-
params do
|
171
|
-
requires :val, type: Float, allow_blank: true
|
172
|
-
end
|
173
|
-
get '/allow_float_blank'
|
174
|
-
|
175
|
-
params do
|
176
|
-
requires :val, type: Integer, allow_blank: true
|
177
|
-
end
|
178
|
-
get '/allow_integer_blank'
|
179
|
-
|
180
|
-
params do
|
181
|
-
requires :val, type: Symbol, allow_blank: true
|
182
|
-
end
|
183
|
-
get '/allow_symbol_blank'
|
184
|
-
|
185
|
-
params do
|
186
|
-
requires :val, type: Boolean, allow_blank: true
|
187
|
-
end
|
188
|
-
get '/allow_boolean_blank'
|
189
|
-
|
190
|
-
params do
|
191
|
-
requires :val, type: Boolean, allow_blank: { value: false, message: 'has no value' }
|
192
|
-
end
|
193
|
-
get '/disallow_boolean_blank'
|
194
|
-
|
195
|
-
params do
|
196
|
-
optional :user, type: Hash do
|
197
|
-
requires :name, allow_blank: { value: false, message: 'has no value' }
|
198
|
-
end
|
199
|
-
end
|
200
|
-
get '/disallow_blank_required_param_in_an_optional_group'
|
201
|
-
|
202
|
-
params do
|
203
|
-
optional :user, type: Hash do
|
204
|
-
requires :name, type: Date, allow_blank: true
|
205
|
-
end
|
206
|
-
end
|
207
|
-
get '/allow_blank_date_param_in_an_optional_group'
|
208
|
-
|
209
|
-
params do
|
210
|
-
optional :user, type: Hash do
|
211
|
-
optional :name, allow_blank: { value: false, message: 'has no value' }
|
212
|
-
requires :age
|
213
|
-
end
|
214
|
-
end
|
215
|
-
get '/disallow_blank_optional_param_in_an_optional_group'
|
216
|
-
|
217
|
-
params do
|
218
|
-
requires :user, type: Hash do
|
219
|
-
requires :name, allow_blank: { value: false, message: 'has no value' }
|
220
|
-
end
|
221
|
-
end
|
222
|
-
get '/disallow_blank_required_param_in_a_required_group'
|
223
|
-
|
224
|
-
params do
|
225
|
-
requires :user, type: Hash do
|
226
|
-
requires :name, allow_blank: { value: false, message: 'has no value' }
|
227
|
-
end
|
228
|
-
end
|
229
|
-
get '/disallow_string_value_in_a_required_hash_group'
|
230
|
-
|
231
|
-
params do
|
232
|
-
requires :user, type: Hash do
|
233
|
-
optional :name, allow_blank: { value: false, message: 'has no value' }
|
234
|
-
end
|
235
|
-
end
|
236
|
-
get '/disallow_blank_optional_param_in_a_required_group'
|
237
|
-
|
238
|
-
params do
|
239
|
-
optional :user, type: Hash do
|
240
|
-
optional :name, allow_blank: { value: false, message: 'has no value' }
|
241
|
-
end
|
242
|
-
end
|
243
|
-
get '/disallow_string_value_in_an_optional_hash_group'
|
244
241
|
end
|
242
|
+
get '/disallow_string_value_in_an_optional_hash_group'
|
245
243
|
end
|
246
244
|
end
|
247
245
|
end
|
248
246
|
|
249
|
-
def app
|
250
|
-
ValidationsSpec::AllowBlankValidatorSpec::API
|
251
|
-
end
|
252
|
-
|
253
247
|
context 'invalid input' do
|
254
248
|
it 'refuses empty string' do
|
255
249
|
get '/disallow_blank', name: ''
|
@@ -287,10 +281,12 @@ describe Grape::Validations::AllowBlankValidator do
|
|
287
281
|
get '/custom_message', name: ''
|
288
282
|
expect(last_response.body).to eq('{"error":"name has no value"}')
|
289
283
|
end
|
284
|
+
|
290
285
|
it 'refuses empty string for an optional param' do
|
291
286
|
get '/custom_message/disallow_blank_optional_param', name: ''
|
292
287
|
expect(last_response.body).to eq('{"error":"name has no value"}')
|
293
288
|
end
|
289
|
+
|
294
290
|
it 'refuses only whitespaces' do
|
295
291
|
get '/custom_message', name: ' '
|
296
292
|
expect(last_response.body).to eq('{"error":"name has no value"}')
|
@@ -1,66 +1,204 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
describe Grape::Validations::AtLeastOneOfValidator do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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'
|
9
35
|
end
|
36
|
+
end
|
37
|
+
post '/nested-hash' do
|
38
|
+
end
|
10
39
|
|
11
|
-
|
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
|
12
58
|
end
|
13
59
|
end
|
14
|
-
|
15
|
-
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#validate!' do
|
63
|
+
subject(:validate) { post path, params }
|
16
64
|
|
17
65
|
context 'when all restricted params are present' do
|
66
|
+
let(:path) { '/' }
|
18
67
|
let(:params) { { beer: true, wine: true, grapefruit: true } }
|
19
68
|
|
20
|
-
it 'does not
|
21
|
-
|
69
|
+
it 'does not return a validation error' do
|
70
|
+
validate
|
71
|
+
expect(last_response.status).to eq 201
|
22
72
|
end
|
23
73
|
|
24
74
|
context 'mixed with other params' do
|
25
|
-
let(:
|
75
|
+
let(:path) { '/mixed-params' }
|
76
|
+
let(:params) { { beer: true, wine: true, grapefruit: true, other: true } }
|
26
77
|
|
27
|
-
it 'does not
|
28
|
-
|
78
|
+
it 'does not return a validation error' do
|
79
|
+
validate
|
80
|
+
expect(last_response.status).to eq 201
|
29
81
|
end
|
30
82
|
end
|
31
83
|
end
|
32
84
|
|
33
85
|
context 'when a subset of restricted params are present' do
|
86
|
+
let(:path) { '/' }
|
34
87
|
let(:params) { { beer: true, grapefruit: true } }
|
35
88
|
|
36
|
-
it 'does not
|
37
|
-
|
89
|
+
it 'does not return a validation error' do
|
90
|
+
validate
|
91
|
+
expect(last_response.status).to eq 201
|
38
92
|
end
|
39
93
|
end
|
40
94
|
|
41
|
-
context 'when
|
42
|
-
let(:
|
95
|
+
context 'when none of the restricted params is selected' do
|
96
|
+
let(:path) { '/' }
|
97
|
+
let(:params) { { other: true } }
|
43
98
|
|
44
|
-
it '
|
45
|
-
|
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
|
+
)
|
46
105
|
end
|
47
|
-
end
|
48
106
|
|
49
|
-
|
50
|
-
|
107
|
+
context 'when custom message is specified' do
|
108
|
+
let(:path) { '/custom-message' }
|
51
109
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
56
117
|
end
|
57
118
|
end
|
58
119
|
|
59
120
|
context 'when exactly one of the restricted params is selected' do
|
60
|
-
let(:
|
121
|
+
let(:path) { '/' }
|
122
|
+
let(:params) { { beer: true } }
|
61
123
|
|
62
|
-
it 'does not
|
63
|
-
|
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
|
64
202
|
end
|
65
203
|
end
|
66
204
|
end
|