Class: Grape::Validations::ValuesValidator
- Defined in:
- lib/grape/validations/validators/values.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(attrs, options, required, scope, opts = {}) ⇒ ValuesValidator
constructor
A new instance of ValuesValidator.
- #validate_param!(attr_name, params) ⇒ Object
Methods inherited from Base
convert_to_short_name, #fail_fast?, inherited, #message, #options_key?, #validate, #validate!
Constructor Details
#initialize(attrs, options, required, scope, opts = {}) ⇒ ValuesValidator
Returns a new instance of ValuesValidator.
4 5 6 7 8 9 10 |
# File 'lib/grape/validations/validators/values.rb', line 4 def initialize(attrs, , required, scope, opts = {}) @excepts = ((:except, ) ? [:except] : []) @values = ((:value, ) ? [:value] : []) @values = if @excepts == [] && @values == [] super end |
Instance Method Details
#validate_param!(attr_name, params) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/grape/validations/validators/values.rb', line 12 def validate_param!(attr_name, params) return unless params.is_a?(Hash) return unless params[attr_name] || required_for_root_scope? values = @values.is_a?(Proc) ? @values.call : @values excepts = @excepts.is_a?(Proc) ? @excepts.call : @excepts param_array = params[attr_name].nil? ? [nil] : Array.wrap(params[attr_name]) if param_array.all? { |param| excepts.include?(param) } raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: end return if (values.is_a?(Array) && values.empty?) || param_array.all? { |param| values.include?(param) } raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: (:values) end |