grape 1.6.0 → 1.6.1
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 +18 -0
- data/CONTRIBUTING.md +1 -0
- data/README.md +9 -1
- data/lib/grape/api.rb +12 -0
- data/lib/grape/dry_types.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/json.rb +2 -0
- data/lib/grape/util/strict_hash_configuration.rb +1 -1
- data/lib/grape/validations/types/array_coercer.rb +0 -2
- data/lib/grape/validations/types/dry_type_coercer.rb +1 -10
- data/lib/grape/validations/types/json.rb +0 -2
- data/lib/grape/validations/types/primitive_coercer.rb +5 -7
- data/lib/grape/validations/types/set_coercer.rb +0 -3
- data/lib/grape/validations/types.rb +83 -9
- 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 +73 -71
- 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 +24 -22
- 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/version.rb +1 -1
- data/lib/grape.rb +59 -24
- 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 +116 -116
- data/lib/grape/validations/types/build_coercer.rb +0 -94
- data/lib/grape/validations/validators/all_or_none.rb +0 -16
- data/lib/grape/validations/validators/allow_blank.rb +0 -18
- data/lib/grape/validations/validators/as.rb +0 -12
- data/lib/grape/validations/validators/at_least_one_of.rb +0 -15
- data/lib/grape/validations/validators/coerce.rb +0 -87
- data/lib/grape/validations/validators/default.rb +0 -49
- data/lib/grape/validations/validators/exactly_one_of.rb +0 -17
- data/lib/grape/validations/validators/except_values.rb +0 -22
- data/lib/grape/validations/validators/mutual_exclusion.rb +0 -16
- data/lib/grape/validations/validators/presence.rb +0 -13
- data/lib/grape/validations/validators/regexp.rb +0 -14
- data/lib/grape/validations/validators/same_as.rb +0 -27
- data/lib/grape/validations/validators/values.rb +0 -86
@@ -1,94 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'array_coercer'
|
4
|
-
require_relative 'set_coercer'
|
5
|
-
require_relative 'primitive_coercer'
|
6
|
-
|
7
|
-
module Grape
|
8
|
-
module Validations
|
9
|
-
module Types
|
10
|
-
# Chooses the best coercer for the given type. For example, if the type
|
11
|
-
# is Integer, it will return a coercer which will be able to coerce a value
|
12
|
-
# to the integer.
|
13
|
-
#
|
14
|
-
# There are a few very special coercers which might be returned.
|
15
|
-
#
|
16
|
-
# +Grape::Types::MultipleTypeCoercer+ is a coercer which is returned when
|
17
|
-
# the given type implies values in an array with different types.
|
18
|
-
# For example, +[Integer, String]+ allows integer and string values in
|
19
|
-
# an array.
|
20
|
-
#
|
21
|
-
# +Grape::Types::CustomTypeCoercer+ is a coercer which is returned when
|
22
|
-
# a method is specified by a user with +coerce_with+ option or the user
|
23
|
-
# specifies a custom type which implements requirments of
|
24
|
-
# +Grape::Types::CustomTypeCoercer+.
|
25
|
-
#
|
26
|
-
# +Grape::Types::CustomTypeCollectionCoercer+ is a very similar to the
|
27
|
-
# previous one, but it expects an array or set of values having a custom
|
28
|
-
# type implemented by the user.
|
29
|
-
#
|
30
|
-
# There is also a group of custom types implemented by Grape, check
|
31
|
-
# +Grape::Validations::Types::SPECIAL+ to get the full list.
|
32
|
-
#
|
33
|
-
# @param type [Class] the type to which input strings
|
34
|
-
# should be coerced
|
35
|
-
# @param method [Class,#call] the coercion method to use
|
36
|
-
# @return [Object] object to be used
|
37
|
-
# for coercion and type validation
|
38
|
-
def self.build_coercer(type, method: nil, strict: false)
|
39
|
-
cache_instance(type, method, strict) do
|
40
|
-
create_coercer_instance(type, method, strict)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def self.create_coercer_instance(type, method, strict)
|
45
|
-
# Maps a custom type provided by Grape, it doesn't map types wrapped by collections!!!
|
46
|
-
type = Types.map_special(type)
|
47
|
-
|
48
|
-
# Use a special coercer for multiply-typed parameters.
|
49
|
-
if Types.multiple?(type)
|
50
|
-
MultipleTypeCoercer.new(type, method)
|
51
|
-
|
52
|
-
# Use a special coercer for custom types and coercion methods.
|
53
|
-
elsif method || Types.custom?(type)
|
54
|
-
CustomTypeCoercer.new(type, method)
|
55
|
-
|
56
|
-
# Special coercer for collections of types that implement a parse method.
|
57
|
-
# CustomTypeCoercer (above) already handles such types when an explicit coercion
|
58
|
-
# method is supplied.
|
59
|
-
elsif Types.collection_of_custom?(type)
|
60
|
-
Types::CustomTypeCollectionCoercer.new(
|
61
|
-
Types.map_special(type.first), type.is_a?(Set)
|
62
|
-
)
|
63
|
-
else
|
64
|
-
DryTypeCoercer.coercer_instance_for(type, strict)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def self.cache_instance(type, method, strict, &_block)
|
69
|
-
key = cache_key(type, method, strict)
|
70
|
-
|
71
|
-
return @__cache[key] if @__cache.key?(key)
|
72
|
-
|
73
|
-
instance = yield
|
74
|
-
|
75
|
-
@__cache_write_lock.synchronize do
|
76
|
-
@__cache[key] = instance
|
77
|
-
end
|
78
|
-
|
79
|
-
instance
|
80
|
-
end
|
81
|
-
|
82
|
-
def self.cache_key(type, method, strict)
|
83
|
-
[type, method, strict].each_with_object(+'_') do |val, memo|
|
84
|
-
next if val.nil?
|
85
|
-
|
86
|
-
memo << '_' << val.to_s
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
instance_variable_set(:@__cache, {})
|
91
|
-
instance_variable_set(:@__cache_write_lock, Mutex.new)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'grape/validations/validators/multiple_params_base'
|
4
|
-
|
5
|
-
module Grape
|
6
|
-
module Validations
|
7
|
-
class AllOrNoneOfValidator < MultipleParamsBase
|
8
|
-
def validate_params!(params)
|
9
|
-
keys = keys_in_common(params)
|
10
|
-
return if keys.empty? || keys.length == all_keys.length
|
11
|
-
|
12
|
-
raise Grape::Exceptions::Validation.new(params: all_keys, message: message(:all_or_none))
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grape
|
4
|
-
module Validations
|
5
|
-
class AllowBlankValidator < Base
|
6
|
-
def validate_param!(attr_name, params)
|
7
|
-
return if (options_key?(:value) ? @option[:value] : @option) || !params.is_a?(Hash)
|
8
|
-
|
9
|
-
value = params[attr_name]
|
10
|
-
value = value.strip if value.respond_to?(:strip)
|
11
|
-
|
12
|
-
return if value == false || value.present?
|
13
|
-
|
14
|
-
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:blank))
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grape
|
4
|
-
module Validations
|
5
|
-
class AsValidator < Base
|
6
|
-
# We use a validator for renaming parameters. This is just a marker for
|
7
|
-
# the parameter scope to handle the renaming. No actual validation
|
8
|
-
# happens here.
|
9
|
-
def validate_param!(*); end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'grape/validations/validators/multiple_params_base'
|
4
|
-
|
5
|
-
module Grape
|
6
|
-
module Validations
|
7
|
-
class AtLeastOneOfValidator < MultipleParamsBase
|
8
|
-
def validate_params!(params)
|
9
|
-
return unless keys_in_common(params).empty?
|
10
|
-
|
11
|
-
raise Grape::Exceptions::Validation.new(params: all_keys, message: message(:at_least_one))
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,87 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grape
|
4
|
-
class API
|
5
|
-
class Boolean
|
6
|
-
def self.build(val)
|
7
|
-
return nil if val != true && val != false
|
8
|
-
|
9
|
-
new
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class Instance
|
14
|
-
Boolean = Grape::API::Boolean
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
module Validations
|
19
|
-
class CoerceValidator < Base
|
20
|
-
def initialize(attrs, options, required, scope, **opts)
|
21
|
-
super
|
22
|
-
|
23
|
-
@converter = if type.is_a?(Grape::Validations::Types::VariantCollectionCoercer)
|
24
|
-
type
|
25
|
-
else
|
26
|
-
Types.build_coercer(type, method: @option[:method])
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def validate_param!(attr_name, params)
|
31
|
-
raise validation_exception(attr_name) unless params.is_a? Hash
|
32
|
-
|
33
|
-
new_value = coerce_value(params[attr_name])
|
34
|
-
|
35
|
-
raise validation_exception(attr_name, new_value.message) unless valid_type?(new_value)
|
36
|
-
|
37
|
-
# Don't assign a value if it is identical. It fixes a problem with Hashie::Mash
|
38
|
-
# which looses wrappers for hashes and arrays after reassigning values
|
39
|
-
#
|
40
|
-
# h = Hashie::Mash.new(list: [1, 2, 3, 4])
|
41
|
-
# => #<Hashie::Mash list=#<Hashie::Array [1, 2, 3, 4]>>
|
42
|
-
# list = h.list
|
43
|
-
# h[:list] = list
|
44
|
-
# h
|
45
|
-
# => #<Hashie::Mash list=[1, 2, 3, 4]>
|
46
|
-
return if params[attr_name].instance_of?(new_value.class) && params[attr_name] == new_value
|
47
|
-
|
48
|
-
params[attr_name] = new_value
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
# @!attribute [r] converter
|
54
|
-
# Object that will be used for parameter coercion and type checking.
|
55
|
-
#
|
56
|
-
# See {Types.build_coercer}
|
57
|
-
#
|
58
|
-
# @return [Object]
|
59
|
-
attr_reader :converter
|
60
|
-
|
61
|
-
def valid_type?(val)
|
62
|
-
!val.is_a?(Types::InvalidValue)
|
63
|
-
end
|
64
|
-
|
65
|
-
def coerce_value(val)
|
66
|
-
converter.call(val)
|
67
|
-
# Some custom types might fail, so it should be treated as an invalid value
|
68
|
-
rescue StandardError
|
69
|
-
Types::InvalidValue.new
|
70
|
-
end
|
71
|
-
|
72
|
-
# Type to which the parameter will be coerced.
|
73
|
-
#
|
74
|
-
# @return [Class]
|
75
|
-
def type
|
76
|
-
@option[:type].is_a?(Hash) ? @option[:type][:value] : @option[:type]
|
77
|
-
end
|
78
|
-
|
79
|
-
def validation_exception(attr_name, custom_msg = nil)
|
80
|
-
Grape::Exceptions::Validation.new(
|
81
|
-
params: [@scope.full_name(attr_name)],
|
82
|
-
message: custom_msg || message(:coerce)
|
83
|
-
)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grape
|
4
|
-
module Validations
|
5
|
-
class DefaultValidator < Base
|
6
|
-
def initialize(attrs, options, required, scope, **opts)
|
7
|
-
@default = options
|
8
|
-
super
|
9
|
-
end
|
10
|
-
|
11
|
-
def validate_param!(attr_name, params)
|
12
|
-
params[attr_name] = if @default.is_a? Proc
|
13
|
-
@default.call
|
14
|
-
elsif @default.frozen? || !duplicatable?(@default)
|
15
|
-
@default
|
16
|
-
else
|
17
|
-
duplicate(@default)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def validate!(params)
|
22
|
-
attrs = SingleAttributeIterator.new(self, @scope, params)
|
23
|
-
attrs.each do |resource_params, attr_name|
|
24
|
-
next unless @scope.meets_dependency?(resource_params, params)
|
25
|
-
|
26
|
-
validate_param!(attr_name, resource_params) if resource_params.is_a?(Hash) && resource_params[attr_name].nil?
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
# return true if we might be able to dup this object
|
33
|
-
def duplicatable?(obj)
|
34
|
-
!obj.nil? &&
|
35
|
-
obj != true &&
|
36
|
-
obj != false &&
|
37
|
-
!obj.is_a?(Symbol) &&
|
38
|
-
!obj.is_a?(Numeric)
|
39
|
-
end
|
40
|
-
|
41
|
-
# make a best effort to dup the object
|
42
|
-
def duplicate(obj)
|
43
|
-
obj.dup
|
44
|
-
rescue TypeError
|
45
|
-
obj
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'grape/validations/validators/multiple_params_base'
|
4
|
-
|
5
|
-
module Grape
|
6
|
-
module Validations
|
7
|
-
class ExactlyOneOfValidator < MultipleParamsBase
|
8
|
-
def validate_params!(params)
|
9
|
-
keys = keys_in_common(params)
|
10
|
-
return if keys.length == 1
|
11
|
-
raise Grape::Exceptions::Validation.new(params: all_keys, message: message(:exactly_one)) if keys.length.zero?
|
12
|
-
|
13
|
-
raise Grape::Exceptions::Validation.new(params: keys, message: message(:mutual_exclusion))
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grape
|
4
|
-
module Validations
|
5
|
-
class ExceptValuesValidator < Base
|
6
|
-
def initialize(attrs, options, required, scope, **opts)
|
7
|
-
@except = options.is_a?(Hash) ? options[:value] : options
|
8
|
-
super
|
9
|
-
end
|
10
|
-
|
11
|
-
def validate_param!(attr_name, params)
|
12
|
-
return unless params.respond_to?(:key?) && params.key?(attr_name)
|
13
|
-
|
14
|
-
excepts = @except.is_a?(Proc) ? @except.call : @except
|
15
|
-
return if excepts.nil?
|
16
|
-
|
17
|
-
param_array = params[attr_name].nil? ? [nil] : Array.wrap(params[attr_name])
|
18
|
-
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:except_values)) if param_array.any? { |param| excepts.include?(param) }
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'grape/validations/validators/multiple_params_base'
|
4
|
-
|
5
|
-
module Grape
|
6
|
-
module Validations
|
7
|
-
class MutualExclusionValidator < MultipleParamsBase
|
8
|
-
def validate_params!(params)
|
9
|
-
keys = keys_in_common(params)
|
10
|
-
return if keys.length <= 1
|
11
|
-
|
12
|
-
raise Grape::Exceptions::Validation.new(params: keys, message: message(:mutual_exclusion))
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grape
|
4
|
-
module Validations
|
5
|
-
class PresenceValidator < Base
|
6
|
-
def validate_param!(attr_name, params)
|
7
|
-
return if params.respond_to?(:key?) && params.key?(attr_name)
|
8
|
-
|
9
|
-
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:presence))
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grape
|
4
|
-
module Validations
|
5
|
-
class RegexpValidator < Base
|
6
|
-
def validate_param!(attr_name, params)
|
7
|
-
return unless params.respond_to?(:key?) && params.key?(attr_name)
|
8
|
-
return if Array.wrap(params[attr_name]).all? { |param| param.nil? || param.to_s.match?((options_key?(:value) ? @option[:value] : @option)) }
|
9
|
-
|
10
|
-
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:regexp))
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grape
|
4
|
-
module Validations
|
5
|
-
class SameAsValidator < Base
|
6
|
-
def validate_param!(attr_name, params)
|
7
|
-
confirmation = options_key?(:value) ? @option[:value] : @option
|
8
|
-
return if params[attr_name] == params[confirmation]
|
9
|
-
|
10
|
-
raise Grape::Exceptions::Validation.new(
|
11
|
-
params: [@scope.full_name(attr_name)],
|
12
|
-
message: build_message
|
13
|
-
)
|
14
|
-
end
|
15
|
-
|
16
|
-
private
|
17
|
-
|
18
|
-
def build_message
|
19
|
-
if options_key?(:message)
|
20
|
-
@option[:message]
|
21
|
-
else
|
22
|
-
format I18n.t(:same_as, scope: 'grape.errors.messages'), parameter: @option
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,86 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grape
|
4
|
-
module Validations
|
5
|
-
class ValuesValidator < Base
|
6
|
-
def initialize(attrs, options, required, scope, **opts)
|
7
|
-
if options.is_a?(Hash)
|
8
|
-
@excepts = options[:except]
|
9
|
-
@values = options[:value]
|
10
|
-
@proc = options[:proc]
|
11
|
-
|
12
|
-
warn '[DEPRECATION] The values validator except option is deprecated. ' \
|
13
|
-
'Use the except validator instead.' if @excepts
|
14
|
-
|
15
|
-
raise ArgumentError, 'proc must be a Proc' if @proc && !@proc.is_a?(Proc)
|
16
|
-
|
17
|
-
warn '[DEPRECATION] The values validator proc option is deprecated. ' \
|
18
|
-
'The lambda expression can now be assigned directly to values.' if @proc
|
19
|
-
else
|
20
|
-
@excepts = nil
|
21
|
-
@values = nil
|
22
|
-
@proc = nil
|
23
|
-
@values = options
|
24
|
-
end
|
25
|
-
super
|
26
|
-
end
|
27
|
-
|
28
|
-
def validate_param!(attr_name, params)
|
29
|
-
return unless params.is_a?(Hash)
|
30
|
-
|
31
|
-
val = params[attr_name]
|
32
|
-
|
33
|
-
return if val.nil? && !required_for_root_scope?
|
34
|
-
|
35
|
-
# don't forget that +false.blank?+ is true
|
36
|
-
return if val != false && val.blank? && @allow_blank
|
37
|
-
|
38
|
-
param_array = val.nil? ? [nil] : Array.wrap(val)
|
39
|
-
|
40
|
-
raise validation_exception(attr_name, except_message) \
|
41
|
-
unless check_excepts(param_array)
|
42
|
-
|
43
|
-
raise validation_exception(attr_name, message(:values)) \
|
44
|
-
unless check_values(param_array, attr_name)
|
45
|
-
|
46
|
-
raise validation_exception(attr_name, message(:values)) \
|
47
|
-
if @proc && !param_array.all? { |param| @proc.call(param) }
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
|
52
|
-
def check_values(param_array, attr_name)
|
53
|
-
values = @values.is_a?(Proc) && @values.arity.zero? ? @values.call : @values
|
54
|
-
return true if values.nil?
|
55
|
-
|
56
|
-
begin
|
57
|
-
return param_array.all? { |param| values.call(param) } if values.is_a? Proc
|
58
|
-
rescue StandardError => e
|
59
|
-
warn "Error '#{e}' raised while validating attribute '#{attr_name}'"
|
60
|
-
return false
|
61
|
-
end
|
62
|
-
param_array.all? { |param| values.include?(param) }
|
63
|
-
end
|
64
|
-
|
65
|
-
def check_excepts(param_array)
|
66
|
-
excepts = @excepts.is_a?(Proc) ? @excepts.call : @excepts
|
67
|
-
return true if excepts.nil?
|
68
|
-
|
69
|
-
param_array.none? { |param| excepts.include?(param) }
|
70
|
-
end
|
71
|
-
|
72
|
-
def except_message
|
73
|
-
options = instance_variable_get(:@option)
|
74
|
-
options_key?(:except_message) ? options[:except_message] : message(:except_values)
|
75
|
-
end
|
76
|
-
|
77
|
-
def required_for_root_scope?
|
78
|
-
@required && @scope.root?
|
79
|
-
end
|
80
|
-
|
81
|
-
def validation_exception(attr_name, message)
|
82
|
-
Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|