grape 1.6.0 → 1.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -0
- data/CONTRIBUTING.md +2 -1
- data/README.md +9 -1
- data/UPGRADING.md +4 -4
- data/lib/grape/api.rb +12 -0
- data/lib/grape/dsl/headers.rb +5 -2
- data/lib/grape/dsl/helpers.rb +1 -1
- data/lib/grape/middleware/auth/dsl.rb +7 -1
- data/lib/grape/middleware/base.rb +1 -1
- data/lib/grape/util/strict_hash_configuration.rb +1 -1
- data/lib/grape/validations/validators/all_or_none.rb +7 -5
- data/lib/grape/validations/validators/allow_blank.rb +9 -7
- data/lib/grape/validations/validators/as.rb +7 -5
- data/lib/grape/validations/validators/at_least_one_of.rb +6 -4
- data/lib/grape/validations/validators/base.rb +73 -71
- data/lib/grape/validations/validators/coerce.rb +63 -75
- data/lib/grape/validations/validators/default.rb +36 -34
- data/lib/grape/validations/validators/exactly_one_of.rb +8 -6
- data/lib/grape/validations/validators/except_values.rb +13 -11
- data/lib/grape/validations/validators/multiple_params_base.rb +24 -22
- data/lib/grape/validations/validators/mutual_exclusion.rb +7 -5
- data/lib/grape/validations/validators/presence.rb +6 -4
- data/lib/grape/validations/validators/regexp.rb +7 -5
- data/lib/grape/validations/validators/same_as.rb +17 -15
- data/lib/grape/validations/validators/values.rb +59 -57
- data/lib/grape/validations.rb +6 -0
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +2 -0
- data/spec/grape/api/custom_validations_spec.rb +77 -46
- 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/shared_helpers_exactly_one_of_spec.rb +9 -15
- data/spec/grape/api_remount_spec.rb +16 -15
- data/spec/grape/api_spec.rb +317 -193
- data/spec/grape/dsl/callbacks_spec.rb +1 -0
- 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 +1 -0
- data/spec/grape/dsl/parameters_spec.rb +1 -0
- data/spec/grape/dsl/request_response_spec.rb +1 -0
- data/spec/grape/dsl/routing_spec.rb +9 -6
- data/spec/grape/endpoint/declared_spec.rb +12 -12
- data/spec/grape/endpoint_spec.rb +59 -50
- data/spec/grape/entity_spec.rb +13 -13
- 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 +14 -5
- 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 +1 -0
- data/spec/grape/middleware/exception_spec.rb +111 -161
- data/spec/grape/middleware/formatter_spec.rb +25 -4
- data/spec/grape/middleware/globals_spec.rb +7 -4
- data/spec/grape/middleware/stack_spec.rb +11 -11
- 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 +9 -7
- data/spec/grape/validations/single_attribute_iterator_spec.rb +1 -0
- data/spec/grape/validations/types/primitive_coercer_spec.rb +2 -2
- 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 +10 -12
- 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 +1 -1
- 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 +172 -171
- data/spec/grape/validations_spec.rb +45 -16
- 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 +10 -7
- data/spec/spec_helper.rb +11 -1
- metadata +102 -102
@@ -2,47 +2,49 @@
|
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module Validations
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
module Validators
|
6
|
+
class DefaultValidator < Base
|
7
|
+
def initialize(attrs, options, required, scope, **opts)
|
8
|
+
@default = options
|
9
|
+
super
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
def validate_param!(attr_name, params)
|
13
|
+
params[attr_name] = if @default.is_a? Proc
|
14
|
+
@default.call
|
15
|
+
elsif @default.frozen? || !duplicatable?(@default)
|
16
|
+
@default
|
17
|
+
else
|
18
|
+
duplicate(@default)
|
19
|
+
end
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
def validate!(params)
|
23
|
+
attrs = SingleAttributeIterator.new(self, @scope, params)
|
24
|
+
attrs.each do |resource_params, attr_name|
|
25
|
+
next unless @scope.meets_dependency?(resource_params, params)
|
25
26
|
|
26
|
-
|
27
|
+
validate_param!(attr_name, resource_params) if resource_params.is_a?(Hash) && resource_params[attr_name].nil?
|
28
|
+
end
|
27
29
|
end
|
28
|
-
end
|
29
30
|
|
30
|
-
|
31
|
+
private
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
33
|
+
# return true if we might be able to dup this object
|
34
|
+
def duplicatable?(obj)
|
35
|
+
!obj.nil? &&
|
36
|
+
obj != true &&
|
37
|
+
obj != false &&
|
38
|
+
!obj.is_a?(Symbol) &&
|
39
|
+
!obj.is_a?(Numeric)
|
40
|
+
end
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
# make a best effort to dup the object
|
43
|
+
def duplicate(obj)
|
44
|
+
obj.dup
|
45
|
+
rescue TypeError
|
46
|
+
obj
|
47
|
+
end
|
46
48
|
end
|
47
49
|
end
|
48
50
|
end
|
@@ -4,13 +4,15 @@ require 'grape/validations/validators/multiple_params_base'
|
|
4
4
|
|
5
5
|
module Grape
|
6
6
|
module Validations
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
module Validators
|
8
|
+
class ExactlyOneOfValidator < MultipleParamsBase
|
9
|
+
def validate_params!(params)
|
10
|
+
keys = keys_in_common(params)
|
11
|
+
return if keys.length == 1
|
12
|
+
raise Grape::Exceptions::Validation.new(params: all_keys, message: message(:exactly_one)) if keys.length.zero?
|
12
13
|
|
13
|
-
|
14
|
+
raise Grape::Exceptions::Validation.new(params: keys, message: message(:mutual_exclusion))
|
15
|
+
end
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
@@ -2,20 +2,22 @@
|
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module Validations
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
module Validators
|
6
|
+
class ExceptValuesValidator < Base
|
7
|
+
def initialize(attrs, options, required, scope, **opts)
|
8
|
+
@except = options.is_a?(Hash) ? options[:value] : options
|
9
|
+
super
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
def validate_param!(attr_name, params)
|
13
|
+
return unless params.respond_to?(:key?) && params.key?(attr_name)
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
excepts = @except.is_a?(Proc) ? @except.call : @except
|
16
|
+
return if excepts.nil?
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
param_array = params[attr_name].nil? ? [nil] : Array.wrap(params[attr_name])
|
19
|
+
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:except_values)) if param_array.any? { |param| excepts.include?(param) }
|
20
|
+
end
|
19
21
|
end
|
20
22
|
end
|
21
23
|
end
|
@@ -2,34 +2,36 @@
|
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module Validations
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
5
|
+
module Validators
|
6
|
+
class MultipleParamsBase < Base
|
7
|
+
def validate!(params)
|
8
|
+
attributes = MultipleAttributesIterator.new(self, @scope, params)
|
9
|
+
array_errors = []
|
10
|
+
|
11
|
+
attributes.each do |resource_params, skip_value|
|
12
|
+
next if skip_value
|
13
|
+
|
14
|
+
begin
|
15
|
+
validate_params!(resource_params)
|
16
|
+
rescue Grape::Exceptions::Validation => e
|
17
|
+
array_errors << e
|
18
|
+
end
|
17
19
|
end
|
18
|
-
end
|
19
20
|
|
20
|
-
|
21
|
-
|
21
|
+
raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any?
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
+
private
|
24
25
|
|
25
|
-
|
26
|
-
|
26
|
+
def keys_in_common(resource_params)
|
27
|
+
return [] unless resource_params.is_a?(Hash)
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
all_keys & resource_params.keys.map! { |attr| @scope.full_name(attr) }
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
32
|
+
def all_keys
|
33
|
+
attrs.map { |attr| @scope.full_name(attr) }
|
34
|
+
end
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
@@ -4,12 +4,14 @@ require 'grape/validations/validators/multiple_params_base'
|
|
4
4
|
|
5
5
|
module Grape
|
6
6
|
module Validations
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
module Validators
|
8
|
+
class MutualExclusionValidator < MultipleParamsBase
|
9
|
+
def validate_params!(params)
|
10
|
+
keys = keys_in_common(params)
|
11
|
+
return if keys.length <= 1
|
11
12
|
|
12
|
-
|
13
|
+
raise Grape::Exceptions::Validation.new(params: keys, message: message(:mutual_exclusion))
|
14
|
+
end
|
13
15
|
end
|
14
16
|
end
|
15
17
|
end
|
@@ -2,11 +2,13 @@
|
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module Validations
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
module Validators
|
6
|
+
class PresenceValidator < Base
|
7
|
+
def validate_param!(attr_name, params)
|
8
|
+
return if params.respond_to?(:key?) && params.key?(attr_name)
|
8
9
|
|
9
|
-
|
10
|
+
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:presence))
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
12
14
|
end
|
@@ -2,12 +2,14 @@
|
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module Validations
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
module Validators
|
6
|
+
class RegexpValidator < Base
|
7
|
+
def validate_param!(attr_name, params)
|
8
|
+
return unless params.respond_to?(:key?) && params.key?(attr_name)
|
9
|
+
return if Array.wrap(params[attr_name]).all? { |param| param.nil? || param.to_s.match?((options_key?(:value) ? @option[:value] : @option)) }
|
9
10
|
|
10
|
-
|
11
|
+
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:regexp))
|
12
|
+
end
|
11
13
|
end
|
12
14
|
end
|
13
15
|
end
|
@@ -2,24 +2,26 @@
|
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module Validations
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
module Validators
|
6
|
+
class SameAsValidator < Base
|
7
|
+
def validate_param!(attr_name, params)
|
8
|
+
confirmation = options_key?(:value) ? @option[:value] : @option
|
9
|
+
return if params[attr_name] == params[confirmation]
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
raise Grape::Exceptions::Validation.new(
|
12
|
+
params: [@scope.full_name(attr_name)],
|
13
|
+
message: build_message
|
14
|
+
)
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
+
private
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
def build_message
|
20
|
+
if options_key?(:message)
|
21
|
+
@option[:message]
|
22
|
+
else
|
23
|
+
format I18n.t(:same_as, scope: 'grape.errors.messages'), parameter: @option
|
24
|
+
end
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -2,84 +2,86 @@
|
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module Validations
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
5
|
+
module Validators
|
6
|
+
class ValuesValidator < Base
|
7
|
+
def initialize(attrs, options, required, scope, **opts)
|
8
|
+
if options.is_a?(Hash)
|
9
|
+
@excepts = options[:except]
|
10
|
+
@values = options[:value]
|
11
|
+
@proc = options[:proc]
|
12
|
+
|
13
|
+
warn '[DEPRECATION] The values validator except option is deprecated. ' \
|
14
|
+
'Use the except validator instead.' if @excepts
|
15
|
+
|
16
|
+
raise ArgumentError, 'proc must be a Proc' if @proc && !@proc.is_a?(Proc)
|
17
|
+
|
18
|
+
warn '[DEPRECATION] The values validator proc option is deprecated. ' \
|
19
|
+
'The lambda expression can now be assigned directly to values.' if @proc
|
20
|
+
else
|
21
|
+
@excepts = nil
|
22
|
+
@values = nil
|
23
|
+
@proc = nil
|
24
|
+
@values = options
|
25
|
+
end
|
26
|
+
super
|
24
27
|
end
|
25
|
-
super
|
26
|
-
end
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
def validate_param!(attr_name, params)
|
30
|
+
return unless params.is_a?(Hash)
|
30
31
|
|
31
|
-
|
32
|
+
val = params[attr_name]
|
32
33
|
|
33
|
-
|
34
|
+
return if val.nil? && !required_for_root_scope?
|
34
35
|
|
35
|
-
|
36
|
-
|
36
|
+
# don't forget that +false.blank?+ is true
|
37
|
+
return if val != false && val.blank? && @allow_blank
|
37
38
|
|
38
|
-
|
39
|
+
param_array = val.nil? ? [nil] : Array.wrap(val)
|
39
40
|
|
40
|
-
|
41
|
+
raise validation_exception(attr_name, except_message) \
|
41
42
|
unless check_excepts(param_array)
|
42
43
|
|
43
|
-
|
44
|
+
raise validation_exception(attr_name, message(:values)) \
|
44
45
|
unless check_values(param_array, attr_name)
|
45
46
|
|
46
|
-
|
47
|
+
raise validation_exception(attr_name, message(:values)) \
|
47
48
|
if @proc && !param_array.all? { |param| @proc.call(param) }
|
48
|
-
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
+
private
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
def check_values(param_array, attr_name)
|
54
|
+
values = @values.is_a?(Proc) && @values.arity.zero? ? @values.call : @values
|
55
|
+
return true if values.nil?
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
57
|
+
begin
|
58
|
+
return param_array.all? { |param| values.call(param) } if values.is_a? Proc
|
59
|
+
rescue StandardError => e
|
60
|
+
warn "Error '#{e}' raised while validating attribute '#{attr_name}'"
|
61
|
+
return false
|
62
|
+
end
|
63
|
+
param_array.all? { |param| values.include?(param) }
|
61
64
|
end
|
62
|
-
param_array.all? { |param| values.include?(param) }
|
63
|
-
end
|
64
65
|
|
65
|
-
|
66
|
-
|
67
|
-
|
66
|
+
def check_excepts(param_array)
|
67
|
+
excepts = @excepts.is_a?(Proc) ? @excepts.call : @excepts
|
68
|
+
return true if excepts.nil?
|
68
69
|
|
69
|
-
|
70
|
-
|
70
|
+
param_array.none? { |param| excepts.include?(param) }
|
71
|
+
end
|
71
72
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
73
|
+
def except_message
|
74
|
+
options = instance_variable_get(:@option)
|
75
|
+
options_key?(:except_message) ? options[:except_message] : message(:except_values)
|
76
|
+
end
|
76
77
|
|
77
|
-
|
78
|
-
|
79
|
-
|
78
|
+
def required_for_root_scope?
|
79
|
+
@required && @scope.root?
|
80
|
+
end
|
80
81
|
|
81
|
-
|
82
|
-
|
82
|
+
def validation_exception(attr_name, message)
|
83
|
+
Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
|
84
|
+
end
|
83
85
|
end
|
84
86
|
end
|
85
87
|
end
|
data/lib/grape/validations.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'grape/validations/attributes_iterator'
|
4
|
+
require 'grape/validations/single_attribute_iterator'
|
5
|
+
require 'grape/validations/multiple_attributes_iterator'
|
6
|
+
require 'grape/validations/params_scope'
|
7
|
+
require 'grape/validations/types'
|
8
|
+
|
3
9
|
module Grape
|
4
10
|
# Registry to store and locate known Validators.
|
5
11
|
module Validations
|
data/lib/grape/version.rb
CHANGED
data/lib/grape.rb
CHANGED
@@ -7,7 +7,9 @@ require 'rack/accept'
|
|
7
7
|
require 'rack/auth/basic'
|
8
8
|
require 'rack/auth/digest/md5'
|
9
9
|
require 'set'
|
10
|
+
require 'active_support'
|
10
11
|
require 'active_support/version'
|
12
|
+
require 'active_support/isolated_execution_state' if ActiveSupport::VERSION::MAJOR > 6
|
11
13
|
require 'active_support/core_ext/hash/indifferent_access'
|
12
14
|
require 'active_support/core_ext/object/blank'
|
13
15
|
require 'active_support/core_ext/array/extract_options'
|