diff options
Diffstat (limited to 'lib/api/validations')
-rw-r--r-- | lib/api/validations/validators/absence.rb | 2 | ||||
-rw-r--r-- | lib/api/validations/validators/array_none_any.rb | 6 | ||||
-rw-r--r-- | lib/api/validations/validators/check_assignees_count.rb | 7 | ||||
-rw-r--r-- | lib/api/validations/validators/email_or_email_list.rb | 3 | ||||
-rw-r--r-- | lib/api/validations/validators/file_path.rb | 6 | ||||
-rw-r--r-- | lib/api/validations/validators/git_ref.rb | 6 | ||||
-rw-r--r-- | lib/api/validations/validators/git_sha.rb | 6 | ||||
-rw-r--r-- | lib/api/validations/validators/integer_none_any.rb | 12 | ||||
-rw-r--r-- | lib/api/validations/validators/integer_or_custom_value.rb | 33 | ||||
-rw-r--r-- | lib/api/validations/validators/limit.rb | 3 | ||||
-rw-r--r-- | lib/api/validations/validators/untrusted_regexp.rb | 2 |
11 files changed, 63 insertions, 23 deletions
diff --git a/lib/api/validations/validators/absence.rb b/lib/api/validations/validators/absence.rb index 1f43f3ab126..7858ce7140b 100644 --- a/lib/api/validations/validators/absence.rb +++ b/lib/api/validations/validators/absence.rb @@ -7,7 +7,7 @@ module API def validate_param!(attr_name, params) return if params.respond_to?(:key?) && !params.key?(attr_name) - raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:absence) + raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:absence)) end end end diff --git a/lib/api/validations/validators/array_none_any.rb b/lib/api/validations/validators/array_none_any.rb index 7efb8e6ccee..3732c1f575c 100644 --- a/lib/api/validations/validators/array_none_any.rb +++ b/lib/api/validations/validators/array_none_any.rb @@ -10,8 +10,10 @@ module API return if value.is_a?(Array) || [IssuableFinder::Params::FILTER_NONE, IssuableFinder::Params::FILTER_ANY].include?(value.to_s.downcase) - raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], - message: "should be an array, 'None' or 'Any'" + raise Grape::Exceptions::Validation.new( + params: [@scope.full_name(attr_name)], + message: "should be an array, 'None' or 'Any'" + ) end end end diff --git a/lib/api/validations/validators/check_assignees_count.rb b/lib/api/validations/validators/check_assignees_count.rb index b614058e325..92ada159b46 100644 --- a/lib/api/validations/validators/check_assignees_count.rb +++ b/lib/api/validations/validators/check_assignees_count.rb @@ -18,9 +18,10 @@ module API def validate_param!(attr_name, params) return if param_allowed?(attr_name, params) - raise Grape::Exceptions::Validation, - params: [@scope.full_name(attr_name)], - message: "allows one value, but found #{params[attr_name].size}: #{params[attr_name].join(", ")}" + raise Grape::Exceptions::Validation.new( + params: [@scope.full_name(attr_name)], + message: "allows one value, but found #{params[attr_name].size}: #{params[attr_name].join(", ")}" + ) end private diff --git a/lib/api/validations/validators/email_or_email_list.rb b/lib/api/validations/validators/email_or_email_list.rb index b7f2a0cd443..da665f39130 100644 --- a/lib/api/validations/validators/email_or_email_list.rb +++ b/lib/api/validations/validators/email_or_email_list.rb @@ -11,9 +11,10 @@ module API return if value.split(',').map { |v| ValidateEmail.valid?(v) }.all? - raise Grape::Exceptions::Validation, + raise Grape::Exceptions::Validation.new( params: [@scope.full_name(attr_name)], message: "contains an invalid email address" + ) end end end diff --git a/lib/api/validations/validators/file_path.rb b/lib/api/validations/validators/file_path.rb index 8a815c3b2b8..a6a3c692fd6 100644 --- a/lib/api/validations/validators/file_path.rb +++ b/lib/api/validations/validators/file_path.rb @@ -11,8 +11,10 @@ module API path = Gitlab::Utils.check_path_traversal!(path) Gitlab::Utils.check_allowed_absolute_path!(path, path_allowlist) rescue - raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], - message: "should be a valid file path" + raise Grape::Exceptions::Validation.new( + params: [@scope.full_name(attr_name)], + message: "should be a valid file path" + ) end end end diff --git a/lib/api/validations/validators/git_ref.rb b/lib/api/validations/validators/git_ref.rb index 1dda9d758a7..dcb1db6ca33 100644 --- a/lib/api/validations/validators/git_ref.rb +++ b/lib/api/validations/validators/git_ref.rb @@ -17,8 +17,10 @@ module API return unless invalid_character?(revision) - raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], - message: 'should be a valid reference path' + raise Grape::Exceptions::Validation.new( + params: [@scope.full_name(attr_name)], + message: 'should be a valid reference path' + ) end private diff --git a/lib/api/validations/validators/git_sha.rb b/lib/api/validations/validators/git_sha.rb index 657307db1df..665d1878b4c 100644 --- a/lib/api/validations/validators/git_sha.rb +++ b/lib/api/validations/validators/git_sha.rb @@ -9,8 +9,10 @@ module API return if Commit::EXACT_COMMIT_SHA_PATTERN.match?(sha) - raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], - message: "should be a valid sha" + raise Grape::Exceptions::Validation.new( + params: [@scope.full_name(attr_name)], + message: "should be a valid sha" + ) end end end diff --git a/lib/api/validations/validators/integer_none_any.rb b/lib/api/validations/validators/integer_none_any.rb index aa8c137a6ab..32ab6e19b98 100644 --- a/lib/api/validations/validators/integer_none_any.rb +++ b/lib/api/validations/validators/integer_none_any.rb @@ -3,15 +3,11 @@ module API module Validations module Validators - class IntegerNoneAny < Grape::Validations::Base - def validate_param!(attr_name, params) - value = params[attr_name] + class IntegerNoneAny < IntegerOrCustomValue + private - return if value.is_a?(Integer) || - [IssuableFinder::Params::FILTER_NONE, IssuableFinder::Params::FILTER_ANY].include?(value.to_s.downcase) - - raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], - message: "should be an integer, 'None' or 'Any'" + def extract_custom_values(_options) + [IssuableFinder::Params::FILTER_NONE, IssuableFinder::Params::FILTER_ANY] end end end diff --git a/lib/api/validations/validators/integer_or_custom_value.rb b/lib/api/validations/validators/integer_or_custom_value.rb new file mode 100644 index 00000000000..d2352495948 --- /dev/null +++ b/lib/api/validations/validators/integer_or_custom_value.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module API + module Validations + module Validators + class IntegerOrCustomValue < Grape::Validations::Base + def initialize(attrs, options, required, scope, **opts) + @custom_values = extract_custom_values(options) + super + end + + def validate_param!(attr_name, params) + value = params[attr_name] + + return if value.is_a?(Integer) + return if @custom_values.map(&:downcase).include?(value.to_s.downcase) + + valid_options = Gitlab::Utils.to_exclusive_sentence(['an integer'] + @custom_values) + raise Grape::Exceptions::Validation.new( + params: [@scope.full_name(attr_name)], + message: "should be #{valid_options}, however got #{value}" + ) + end + + private + + def extract_custom_values(options) + options.is_a?(Hash) ? options[:values] : options + end + end + end + end +end diff --git a/lib/api/validations/validators/limit.rb b/lib/api/validations/validators/limit.rb index 3bb4cee1d75..e8f894849a5 100644 --- a/lib/api/validations/validators/limit.rb +++ b/lib/api/validations/validators/limit.rb @@ -9,9 +9,10 @@ module API return if value.size <= @option - raise Grape::Exceptions::Validation, + raise Grape::Exceptions::Validation.new( params: [@scope.full_name(attr_name)], message: "#{@scope.full_name(attr_name)} must be less than #{@option} characters" + ) end end end diff --git a/lib/api/validations/validators/untrusted_regexp.rb b/lib/api/validations/validators/untrusted_regexp.rb index ec623684e67..3ddea2bd9de 100644 --- a/lib/api/validations/validators/untrusted_regexp.rb +++ b/lib/api/validations/validators/untrusted_regexp.rb @@ -11,7 +11,7 @@ module API Gitlab::UntrustedRegexp.new(value) rescue RegexpError => e message = "is an invalid regexp: #{e.message}" - raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message + raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message) end end end |