From bf1ed85a9d6a932a99d0a5fdf70e72ea36c2600c Mon Sep 17 00:00:00 2001 From: Heinrich Lee Yu Date: Thu, 25 Oct 2018 22:20:06 +0800 Subject: Refactor api validator to separate class --- lib/api/helpers/custom_validators.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib/api/helpers') 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) -- cgit v1.2.1 From e0f0c29b0cfe3c0c97191eeb96eae1299f3983d1 Mon Sep 17 00:00:00 2001 From: Heinrich Lee Yu Date: Fri, 26 Oct 2018 10:47:14 +0800 Subject: Support lowercase none / any --- lib/api/helpers/custom_validators.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/api/helpers') diff --git a/lib/api/helpers/custom_validators.rb b/lib/api/helpers/custom_validators.rb index e4af75f1971..1058f4e8a5e 100644 --- a/lib/api/helpers/custom_validators.rb +++ b/lib/api/helpers/custom_validators.rb @@ -16,7 +16,7 @@ module API value = params[attr_name] return if value.is_a?(Integer) || - [IssuableFinder::FILTER_NONE, IssuableFinder::FILTER_ANY].include?(value) + [IssuableFinder::FILTER_NONE, IssuableFinder::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'" -- cgit v1.2.1