2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-08 01:09:18 +00:00
|
|
|
module API
|
|
|
|
module Helpers
|
|
|
|
module CustomValidators
|
|
|
|
class Absence < Grape::Validations::Base
|
|
|
|
def validate_param!(attr_name, params)
|
|
|
|
return if params.respond_to?(:key?) && !params.key?(attr_name)
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2016-12-08 01:09:18 +00:00
|
|
|
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:absence)
|
|
|
|
end
|
|
|
|
end
|
2018-10-25 14:20:06 +00:00
|
|
|
|
|
|
|
class IntegerNoneAny < Grape::Validations::Base
|
|
|
|
def validate_param!(attr_name, params)
|
|
|
|
value = params[attr_name]
|
|
|
|
|
|
|
|
return if value.is_a?(Integer) ||
|
2018-10-26 02:47:14 +00:00
|
|
|
[IssuableFinder::FILTER_NONE, IssuableFinder::FILTER_ANY].include?(value.to_s.downcase)
|
2018-10-25 14:20:06 +00:00
|
|
|
|
|
|
|
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)],
|
|
|
|
message: "should be an integer, 'None' or 'Any'"
|
|
|
|
end
|
|
|
|
end
|
2016-12-08 01:09:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Grape::Validations.register_validator(:absence, ::API::Helpers::CustomValidators::Absence)
|
2018-10-25 14:20:06 +00:00
|
|
|
Grape::Validations.register_validator(:integer_none_any, ::API::Helpers::CustomValidators::IntegerNoneAny)
|