diff options
author | Heinrich Lee Yu <hleeyu@gmail.com> | 2018-10-25 22:20:06 +0800 |
---|---|---|
committer | Heinrich Lee Yu <hleeyu@gmail.com> | 2018-10-26 10:32:14 +0800 |
commit | bf1ed85a9d6a932a99d0a5fdf70e72ea36c2600c (patch) | |
tree | 66fabf664fc6e3f29a19322dbf3b7c6c11f1dc06 /lib/api/helpers | |
parent | d4e26636e72ef150a6f4446558c4d24d3bf21e69 (diff) | |
download | gitlab-ce-bf1ed85a9d6a932a99d0a5fdf70e72ea36c2600c.tar.gz |
Refactor api validator to separate class
Diffstat (limited to 'lib/api/helpers')
-rw-r--r-- | lib/api/helpers/custom_validators.rb | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/api/helpers/custom_validators.rb b/lib/api/helpers/custom_validators.rb index 23b1cd1ad45..e4af75f1971 100644 --- a/lib/api/helpers/custom_validators.rb +++ b/lib/api/helpers/custom_validators.rb @@ -10,8 +10,21 @@ module API raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:absence) end end + + class IntegerNoneAny < Grape::Validations::Base + def validate_param!(attr_name, params) + value = params[attr_name] + + return if value.is_a?(Integer) || + [IssuableFinder::FILTER_NONE, IssuableFinder::FILTER_ANY].include?(value) + + raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], + message: "should be an integer, 'None' or 'Any'" + end + end end end end Grape::Validations.register_validator(:absence, ::API::Helpers::CustomValidators::Absence) +Grape::Validations.register_validator(:integer_none_any, ::API::Helpers::CustomValidators::IntegerNoneAny) |