grape 1.5.2 → 1.6.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +47 -0
- data/CONTRIBUTING.md +2 -1
- data/README.md +33 -3
- data/UPGRADING.md +71 -2
- data/grape.gemspec +5 -5
- data/lib/grape/api/instance.rb +13 -17
- data/lib/grape/api.rb +18 -13
- data/lib/grape/cookies.rb +2 -0
- data/lib/grape/dsl/desc.rb +3 -5
- data/lib/grape/dsl/headers.rb +5 -2
- data/lib/grape/dsl/helpers.rb +7 -5
- data/lib/grape/dsl/inside_route.rb +17 -8
- data/lib/grape/dsl/middleware.rb +4 -4
- data/lib/grape/dsl/parameters.rb +3 -3
- data/lib/grape/dsl/request_response.rb +9 -6
- data/lib/grape/dsl/routing.rb +2 -2
- data/lib/grape/dsl/settings.rb +5 -5
- data/lib/grape/endpoint.rb +21 -36
- data/lib/grape/error_formatter/json.rb +2 -6
- data/lib/grape/error_formatter/xml.rb +2 -6
- data/lib/grape/exceptions/empty_message_body.rb +11 -0
- data/lib/grape/exceptions/validation.rb +1 -2
- data/lib/grape/formatter/json.rb +1 -0
- data/lib/grape/formatter/serializable_hash.rb +2 -1
- data/lib/grape/formatter/xml.rb +1 -0
- data/lib/grape/locale/en.yml +1 -1
- data/lib/grape/middleware/auth/dsl.rb +7 -1
- data/lib/grape/middleware/base.rb +3 -1
- data/lib/grape/middleware/formatter.rb +4 -4
- data/lib/grape/middleware/stack.rb +2 -2
- data/lib/grape/middleware/versioner/accept_version_header.rb +3 -5
- data/lib/grape/middleware/versioner/header.rb +6 -4
- data/lib/grape/middleware/versioner/param.rb +1 -0
- data/lib/grape/middleware/versioner/parse_media_type_patch.rb +2 -1
- data/lib/grape/middleware/versioner/path.rb +2 -0
- data/lib/grape/parser/json.rb +1 -1
- data/lib/grape/parser/xml.rb +1 -1
- data/lib/grape/path.rb +1 -0
- data/lib/grape/request.rb +3 -0
- data/lib/grape/router/pattern.rb +1 -1
- data/lib/grape/router/route.rb +2 -2
- data/lib/grape/router.rb +6 -0
- data/lib/grape/util/inheritable_setting.rb +1 -3
- data/lib/grape/util/lazy_value.rb +3 -2
- data/lib/grape/util/strict_hash_configuration.rb +1 -1
- data/lib/grape/validations/params_scope.rb +88 -55
- data/lib/grape/validations/types/custom_type_coercer.rb +1 -2
- data/lib/grape/validations/types/dry_type_coercer.rb +1 -1
- data/lib/grape/validations/types/json.rb +2 -1
- data/lib/grape/validations/types/primitive_coercer.rb +3 -3
- data/lib/grape/validations/validators/all_or_none.rb +8 -5
- data/lib/grape/validations/validators/allow_blank.rb +9 -7
- data/lib/grape/validations/validators/as.rb +6 -8
- data/lib/grape/validations/validators/at_least_one_of.rb +7 -4
- data/lib/grape/validations/validators/base.rb +75 -70
- data/lib/grape/validations/validators/coerce.rb +63 -79
- data/lib/grape/validations/validators/default.rb +37 -34
- data/lib/grape/validations/validators/exactly_one_of.rb +9 -6
- data/lib/grape/validations/validators/except_values.rb +13 -11
- data/lib/grape/validations/validators/multiple_params_base.rb +24 -20
- data/lib/grape/validations/validators/mutual_exclusion.rb +8 -5
- data/lib/grape/validations/validators/presence.rb +7 -4
- data/lib/grape/validations/validators/regexp.rb +8 -5
- data/lib/grape/validations/validators/same_as.rb +18 -15
- data/lib/grape/validations/validators/values.rb +61 -56
- data/lib/grape/validations.rb +6 -0
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +4 -1
- data/spec/grape/api/custom_validations_spec.rb +77 -45
- data/spec/grape/api/deeply_included_options_spec.rb +3 -3
- data/spec/grape/api/defines_boolean_in_params_spec.rb +2 -1
- data/spec/grape/api/invalid_format_spec.rb +2 -0
- data/spec/grape/api/recognize_path_spec.rb +1 -1
- data/spec/grape/api/routes_with_requirements_spec.rb +8 -8
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +9 -15
- data/spec/grape/api_remount_spec.rb +16 -15
- data/spec/grape/api_spec.rb +510 -220
- data/spec/grape/dsl/callbacks_spec.rb +2 -1
- data/spec/grape/dsl/headers_spec.rb +39 -9
- data/spec/grape/dsl/helpers_spec.rb +3 -2
- data/spec/grape/dsl/inside_route_spec.rb +6 -4
- data/spec/grape/dsl/logger_spec.rb +16 -18
- data/spec/grape/dsl/middleware_spec.rb +2 -1
- data/spec/grape/dsl/parameters_spec.rb +2 -0
- data/spec/grape/dsl/request_response_spec.rb +1 -0
- data/spec/grape/dsl/routing_spec.rb +10 -7
- data/spec/grape/endpoint/declared_spec.rb +259 -12
- data/spec/grape/endpoint_spec.rb +77 -55
- data/spec/grape/entity_spec.rb +22 -22
- data/spec/grape/exceptions/body_parse_errors_spec.rb +3 -0
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +61 -22
- data/spec/grape/exceptions/validation_errors_spec.rb +13 -10
- data/spec/grape/exceptions/validation_spec.rb +5 -3
- data/spec/grape/extensions/param_builders/hash_spec.rb +7 -7
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +8 -8
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +8 -8
- data/spec/grape/integration/rack_sendfile_spec.rb +1 -1
- data/spec/grape/loading_spec.rb +8 -8
- data/spec/grape/middleware/auth/dsl_spec.rb +15 -6
- data/spec/grape/middleware/auth/strategies_spec.rb +60 -20
- data/spec/grape/middleware/base_spec.rb +24 -15
- data/spec/grape/middleware/error_spec.rb +2 -2
- data/spec/grape/middleware/exception_spec.rb +111 -161
- data/spec/grape/middleware/formatter_spec.rb +27 -6
- data/spec/grape/middleware/globals_spec.rb +7 -4
- data/spec/grape/middleware/stack_spec.rb +14 -12
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +2 -1
- data/spec/grape/middleware/versioner/header_spec.rb +14 -13
- data/spec/grape/middleware/versioner/param_spec.rb +7 -1
- data/spec/grape/middleware/versioner/path_spec.rb +5 -1
- data/spec/grape/middleware/versioner_spec.rb +1 -1
- data/spec/grape/parser_spec.rb +4 -0
- data/spec/grape/path_spec.rb +52 -52
- data/spec/grape/presenters/presenter_spec.rb +7 -6
- data/spec/grape/request_spec.rb +6 -4
- data/spec/grape/util/inheritable_setting_spec.rb +7 -7
- data/spec/grape/util/inheritable_values_spec.rb +3 -2
- data/spec/grape/util/reverse_stackable_values_spec.rb +3 -1
- data/spec/grape/util/stackable_values_spec.rb +7 -5
- data/spec/grape/validations/instance_behaivour_spec.rb +9 -10
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +1 -0
- data/spec/grape/validations/params_scope_spec.rb +46 -10
- data/spec/grape/validations/single_attribute_iterator_spec.rb +2 -1
- data/spec/grape/validations/types/primitive_coercer_spec.rb +4 -4
- data/spec/grape/validations/types_spec.rb +8 -8
- data/spec/grape/validations/validators/all_or_none_spec.rb +50 -56
- data/spec/grape/validations/validators/allow_blank_spec.rb +136 -140
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +50 -56
- data/spec/grape/validations/validators/coerce_spec.rb +99 -22
- data/spec/grape/validations/validators/default_spec.rb +72 -78
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +71 -77
- data/spec/grape/validations/validators/except_values_spec.rb +3 -3
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +71 -77
- data/spec/grape/validations/validators/presence_spec.rb +16 -1
- data/spec/grape/validations/validators/regexp_spec.rb +25 -31
- data/spec/grape/validations/validators/same_as_spec.rb +14 -20
- data/spec/grape/validations/validators/values_spec.rb +183 -178
- data/spec/grape/validations_spec.rb +99 -58
- data/spec/integration/eager_load/eager_load_spec.rb +2 -2
- data/spec/integration/multi_json/json_spec.rb +1 -1
- data/spec/integration/multi_xml/xml_spec.rb +1 -1
- data/spec/shared/versioning_examples.rb +12 -9
- data/spec/spec_helper.rb +12 -2
- data/spec/support/basic_auth_encode_helpers.rb +1 -1
- metadata +102 -101
@@ -2,95 +2,89 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Grape::Validations::ExactlyOneOfValidator do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
class API < Grape::API
|
12
|
-
rescue_from Grape::Exceptions::ValidationErrors do |e|
|
13
|
-
error!(e.errors.transform_keys! { |key| key.join(',') }, 400)
|
14
|
-
end
|
5
|
+
describe Grape::Validations::Validators::ExactlyOneOfValidator do
|
6
|
+
let_it_be(:app) do
|
7
|
+
Class.new(Grape::API) do
|
8
|
+
rescue_from Grape::Exceptions::ValidationErrors do |e|
|
9
|
+
error!(e.errors.transform_keys! { |key| key.join(',') }, 400)
|
10
|
+
end
|
15
11
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
12
|
+
params do
|
13
|
+
optional :beer
|
14
|
+
optional :wine
|
15
|
+
optional :grapefruit
|
16
|
+
exactly_one_of :beer, :wine, :grapefruit
|
17
|
+
end
|
18
|
+
post do
|
19
|
+
end
|
24
20
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
21
|
+
params do
|
22
|
+
optional :beer
|
23
|
+
optional :wine
|
24
|
+
optional :grapefruit
|
25
|
+
optional :other
|
26
|
+
exactly_one_of :beer, :wine, :grapefruit
|
27
|
+
end
|
28
|
+
post 'mixed-params' do
|
29
|
+
end
|
34
30
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
31
|
+
params do
|
32
|
+
optional :beer
|
33
|
+
optional :wine
|
34
|
+
optional :grapefruit
|
35
|
+
exactly_one_of :beer, :wine, :grapefruit, message: 'you should choose one'
|
36
|
+
end
|
37
|
+
post '/custom-message' do
|
38
|
+
end
|
43
39
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
40
|
+
params do
|
41
|
+
requires :item, type: Hash do
|
42
|
+
optional :beer
|
43
|
+
optional :wine
|
44
|
+
optional :grapefruit
|
45
|
+
exactly_one_of :beer, :wine, :grapefruit
|
46
|
+
end
|
47
|
+
end
|
48
|
+
post '/nested-hash' do
|
49
|
+
end
|
54
50
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
51
|
+
params do
|
52
|
+
optional :item, type: Hash do
|
53
|
+
optional :beer
|
54
|
+
optional :wine
|
55
|
+
optional :grapefruit
|
56
|
+
exactly_one_of :beer, :wine, :grapefruit
|
57
|
+
end
|
58
|
+
end
|
59
|
+
post '/nested-optional-hash' do
|
60
|
+
end
|
65
61
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
62
|
+
params do
|
63
|
+
requires :items, type: Array do
|
64
|
+
optional :beer
|
65
|
+
optional :wine
|
66
|
+
optional :grapefruit
|
67
|
+
exactly_one_of :beer, :wine, :grapefruit
|
68
|
+
end
|
69
|
+
end
|
70
|
+
post '/nested-array' do
|
71
|
+
end
|
76
72
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
post '/deeply-nested-array' do
|
73
|
+
params do
|
74
|
+
requires :items, type: Array do
|
75
|
+
requires :nested_items, type: Array do
|
76
|
+
optional :beer, :wine, :grapefruit, type: Grape::API::Boolean
|
77
|
+
exactly_one_of :beer, :wine, :grapefruit
|
86
78
|
end
|
87
79
|
end
|
88
80
|
end
|
81
|
+
post '/deeply-nested-array' do
|
82
|
+
end
|
89
83
|
end
|
84
|
+
end
|
90
85
|
|
91
|
-
|
92
|
-
|
93
|
-
end
|
86
|
+
describe '#validate!' do
|
87
|
+
subject(:validate) { post path, params }
|
94
88
|
|
95
89
|
context 'when all params are present' do
|
96
90
|
let(:path) { '/' }
|
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Grape::Validations::ExceptValuesValidator do
|
5
|
+
describe Grape::Validations::Validators::ExceptValuesValidator do
|
6
6
|
module ValidationsSpec
|
7
7
|
class ExceptValuesModel
|
8
|
-
DEFAULT_EXCEPTS = [
|
8
|
+
DEFAULT_EXCEPTS = %w[invalid-type1 invalid-type2 invalid-type3].freeze
|
9
9
|
class << self
|
10
10
|
attr_accessor :excepts
|
11
11
|
|
@@ -170,7 +170,7 @@ describe Grape::Validations::ExceptValuesValidator do
|
|
170
170
|
it 'raises IncompatibleOptionValues when type is incompatible with values array' do
|
171
171
|
subject = Class.new(Grape::API)
|
172
172
|
expect do
|
173
|
-
subject.params { optional :type, except_values: [
|
173
|
+
subject.params { optional :type, except_values: %w[valid-type1 valid-type2 valid-type3], type: Symbol }
|
174
174
|
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
|
175
175
|
end
|
176
176
|
|
@@ -2,95 +2,89 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Grape::Validations::MutualExclusionValidator do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
class API < Grape::API
|
12
|
-
rescue_from Grape::Exceptions::ValidationErrors do |e|
|
13
|
-
error!(e.errors.transform_keys! { |key| key.join(',') }, 400)
|
14
|
-
end
|
5
|
+
describe Grape::Validations::Validators::MutualExclusionValidator do
|
6
|
+
let_it_be(:app) do
|
7
|
+
Class.new(Grape::API) do
|
8
|
+
rescue_from Grape::Exceptions::ValidationErrors do |e|
|
9
|
+
error!(e.errors.transform_keys! { |key| key.join(',') }, 400)
|
10
|
+
end
|
15
11
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
12
|
+
params do
|
13
|
+
optional :beer
|
14
|
+
optional :wine
|
15
|
+
optional :grapefruit
|
16
|
+
mutually_exclusive :beer, :wine, :grapefruit
|
17
|
+
end
|
18
|
+
post do
|
19
|
+
end
|
24
20
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
21
|
+
params do
|
22
|
+
optional :beer
|
23
|
+
optional :wine
|
24
|
+
optional :grapefruit
|
25
|
+
optional :other
|
26
|
+
mutually_exclusive :beer, :wine, :grapefruit
|
27
|
+
end
|
28
|
+
post 'mixed-params' do
|
29
|
+
end
|
34
30
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
31
|
+
params do
|
32
|
+
optional :beer
|
33
|
+
optional :wine
|
34
|
+
optional :grapefruit
|
35
|
+
mutually_exclusive :beer, :wine, :grapefruit, message: 'you should not mix beer and wine'
|
36
|
+
end
|
37
|
+
post '/custom-message' do
|
38
|
+
end
|
43
39
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
40
|
+
params do
|
41
|
+
requires :item, type: Hash do
|
42
|
+
optional :beer
|
43
|
+
optional :wine
|
44
|
+
optional :grapefruit
|
45
|
+
mutually_exclusive :beer, :wine, :grapefruit
|
46
|
+
end
|
47
|
+
end
|
48
|
+
post '/nested-hash' do
|
49
|
+
end
|
54
50
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
51
|
+
params do
|
52
|
+
optional :item, type: Hash do
|
53
|
+
optional :beer
|
54
|
+
optional :wine
|
55
|
+
optional :grapefruit
|
56
|
+
mutually_exclusive :beer, :wine, :grapefruit
|
57
|
+
end
|
58
|
+
end
|
59
|
+
post '/nested-optional-hash' do
|
60
|
+
end
|
65
61
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
62
|
+
params do
|
63
|
+
requires :items, type: Array do
|
64
|
+
optional :beer
|
65
|
+
optional :wine
|
66
|
+
optional :grapefruit
|
67
|
+
mutually_exclusive :beer, :wine, :grapefruit
|
68
|
+
end
|
69
|
+
end
|
70
|
+
post '/nested-array' do
|
71
|
+
end
|
76
72
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
post '/deeply-nested-array' do
|
73
|
+
params do
|
74
|
+
requires :items, type: Array do
|
75
|
+
requires :nested_items, type: Array do
|
76
|
+
optional :beer, :wine, :grapefruit, type: Grape::API::Boolean
|
77
|
+
mutually_exclusive :beer, :wine, :grapefruit
|
86
78
|
end
|
87
79
|
end
|
88
80
|
end
|
81
|
+
post '/deeply-nested-array' do
|
82
|
+
end
|
89
83
|
end
|
84
|
+
end
|
90
85
|
|
91
|
-
|
92
|
-
|
93
|
-
end
|
86
|
+
describe '#validate!' do
|
87
|
+
subject(:validate) { post path, params }
|
94
88
|
|
95
89
|
context 'when all mutually exclusive params are present' do
|
96
90
|
let(:path) { '/' }
|
@@ -2,12 +2,13 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Grape::Validations::PresenceValidator do
|
5
|
+
describe Grape::Validations::Validators::PresenceValidator do
|
6
6
|
subject do
|
7
7
|
Class.new(Grape::API) do
|
8
8
|
format :json
|
9
9
|
end
|
10
10
|
end
|
11
|
+
|
11
12
|
def app
|
12
13
|
subject
|
13
14
|
end
|
@@ -20,6 +21,7 @@ describe Grape::Validations::PresenceValidator do
|
|
20
21
|
end
|
21
22
|
end
|
22
23
|
end
|
24
|
+
|
23
25
|
it 'does not validate for any params' do
|
24
26
|
get '/bacons'
|
25
27
|
expect(last_response.status).to eq(200)
|
@@ -38,15 +40,18 @@ describe Grape::Validations::PresenceValidator do
|
|
38
40
|
end
|
39
41
|
end
|
40
42
|
end
|
43
|
+
|
41
44
|
it 'requires when missing' do
|
42
45
|
get '/requires'
|
43
46
|
expect(last_response.status).to eq(400)
|
44
47
|
expect(last_response.body).to eq('{"error":"email is required, email has no value"}')
|
45
48
|
end
|
49
|
+
|
46
50
|
it 'requires when empty' do
|
47
51
|
get '/requires', email: ''
|
48
52
|
expect(last_response.body).to eq('{"error":"email has no value, email format is invalid"}')
|
49
53
|
end
|
54
|
+
|
50
55
|
it 'valid when set' do
|
51
56
|
get '/requires', email: 'bob@example.com'
|
52
57
|
expect(last_response.status).to eq(200)
|
@@ -64,6 +69,7 @@ describe Grape::Validations::PresenceValidator do
|
|
64
69
|
{ ret: params[:id] }
|
65
70
|
end
|
66
71
|
end
|
72
|
+
|
67
73
|
it 'validates id' do
|
68
74
|
post '/'
|
69
75
|
expect(last_response.status).to eq(400)
|
@@ -90,16 +96,19 @@ describe Grape::Validations::PresenceValidator do
|
|
90
96
|
'Hello'
|
91
97
|
end
|
92
98
|
end
|
99
|
+
|
93
100
|
it 'requires when missing' do
|
94
101
|
get '/'
|
95
102
|
expect(last_response.status).to eq(400)
|
96
103
|
expect(last_response.body).to eq('{"error":"email is missing, email is empty"}')
|
97
104
|
end
|
105
|
+
|
98
106
|
it 'requires when empty' do
|
99
107
|
get '/', email: ''
|
100
108
|
expect(last_response.status).to eq(400)
|
101
109
|
expect(last_response.body).to eq('{"error":"email is empty, email is invalid"}')
|
102
110
|
end
|
111
|
+
|
103
112
|
it 'valid when set' do
|
104
113
|
get '/', email: 'bob@example.com'
|
105
114
|
expect(last_response.status).to eq(200)
|
@@ -124,6 +133,7 @@ describe Grape::Validations::PresenceValidator do
|
|
124
133
|
'Hello'
|
125
134
|
end
|
126
135
|
end
|
136
|
+
|
127
137
|
it 'validates for all defined params' do
|
128
138
|
get '/single-requires'
|
129
139
|
expect(last_response.status).to eq(400)
|
@@ -144,6 +154,7 @@ describe Grape::Validations::PresenceValidator do
|
|
144
154
|
'Hello'
|
145
155
|
end
|
146
156
|
end
|
157
|
+
|
147
158
|
it 'validates name, company' do
|
148
159
|
get '/'
|
149
160
|
expect(last_response.status).to eq(400)
|
@@ -171,6 +182,7 @@ describe Grape::Validations::PresenceValidator do
|
|
171
182
|
'Nested'
|
172
183
|
end
|
173
184
|
end
|
185
|
+
|
174
186
|
it 'validates nested parameters' do
|
175
187
|
get '/nested'
|
176
188
|
expect(last_response.status).to eq(400)
|
@@ -203,6 +215,7 @@ describe Grape::Validations::PresenceValidator do
|
|
203
215
|
'Nested triple'
|
204
216
|
end
|
205
217
|
end
|
218
|
+
|
206
219
|
it 'validates triple nested parameters' do
|
207
220
|
get '/nested_triple'
|
208
221
|
expect(last_response.status).to eq(400)
|
@@ -252,6 +265,7 @@ describe Grape::Validations::PresenceValidator do
|
|
252
265
|
'Hello optional'
|
253
266
|
end
|
254
267
|
end
|
268
|
+
|
255
269
|
it 'works with required' do
|
256
270
|
get '/required'
|
257
271
|
expect(last_response.status).to eq(400)
|
@@ -261,6 +275,7 @@ describe Grape::Validations::PresenceValidator do
|
|
261
275
|
expect(last_response.status).to eq(200)
|
262
276
|
expect(last_response.body).to eq('Hello required'.to_json)
|
263
277
|
end
|
278
|
+
|
264
279
|
it 'works with optional' do
|
265
280
|
get '/optional'
|
266
281
|
expect(last_response.status).to eq(200)
|
@@ -2,53 +2,47 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Grape::Validations::RegexpValidator do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
default_format :json
|
10
|
-
|
11
|
-
resources :custom_message do
|
12
|
-
params do
|
13
|
-
requires :name, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
|
14
|
-
end
|
15
|
-
get do
|
16
|
-
end
|
17
|
-
|
18
|
-
params do
|
19
|
-
requires :names, type: { value: Array[String], message: 'can\'t be nil' }, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
|
20
|
-
end
|
21
|
-
get 'regexp_with_array' do
|
22
|
-
end
|
23
|
-
end
|
5
|
+
describe Grape::Validations::Validators::RegexpValidator do
|
6
|
+
let_it_be(:app) do
|
7
|
+
Class.new(Grape::API) do
|
8
|
+
default_format :json
|
24
9
|
|
10
|
+
resources :custom_message do
|
25
11
|
params do
|
26
|
-
requires :name, regexp: /^[a-z]
|
12
|
+
requires :name, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
|
27
13
|
end
|
28
14
|
get do
|
29
15
|
end
|
30
16
|
|
31
17
|
params do
|
32
|
-
requires :names, type: Array[String], regexp: /^[a-z]
|
18
|
+
requires :names, type: { value: Array[String], message: 'can\'t be nil' }, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
|
33
19
|
end
|
34
20
|
get 'regexp_with_array' do
|
35
21
|
end
|
22
|
+
end
|
36
23
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
24
|
+
params do
|
25
|
+
requires :name, regexp: /^[a-z]+$/
|
26
|
+
end
|
27
|
+
get do
|
28
|
+
end
|
29
|
+
|
30
|
+
params do
|
31
|
+
requires :names, type: Array[String], regexp: /^[a-z]+$/
|
32
|
+
end
|
33
|
+
get 'regexp_with_array' do
|
34
|
+
end
|
35
|
+
|
36
|
+
params do
|
37
|
+
requires :people, type: Hash do
|
38
|
+
requires :names, type: Array[String], regexp: /^[a-z]+$/
|
43
39
|
end
|
44
40
|
end
|
41
|
+
get 'nested_regexp_with_array' do
|
42
|
+
end
|
45
43
|
end
|
46
44
|
end
|
47
45
|
|
48
|
-
def app
|
49
|
-
ValidationsSpec::RegexpValidatorSpec::API
|
50
|
-
end
|
51
|
-
|
52
46
|
context 'custom validation message' do
|
53
47
|
context 'with invalid input' do
|
54
48
|
it 'refuses inapppopriate' do
|
@@ -2,31 +2,25 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Grape::Validations::SameAsValidator do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
5
|
+
describe Grape::Validations::Validators::SameAsValidator do
|
6
|
+
let_it_be(:app) do
|
7
|
+
Class.new(Grape::API) do
|
8
|
+
params do
|
9
|
+
requires :password
|
10
|
+
requires :password_confirmation, same_as: :password
|
11
|
+
end
|
12
|
+
post do
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
15
|
+
params do
|
16
|
+
requires :password
|
17
|
+
requires :password_confirmation, same_as: { value: :password, message: 'not match' }
|
18
|
+
end
|
19
|
+
post '/custom-message' do
|
22
20
|
end
|
23
21
|
end
|
24
22
|
end
|
25
23
|
|
26
|
-
def app
|
27
|
-
ValidationsSpec::SameAsValidatorSpec::API
|
28
|
-
end
|
29
|
-
|
30
24
|
describe '/' do
|
31
25
|
context 'is the same' do
|
32
26
|
it do
|