lib/grape/validations/validators/values_validator.rb in grape-1.7.1 vs lib/grape/validations/validators/values_validator.rb in grape-1.8.0
- old
+ new
@@ -8,17 +8,15 @@
if options.is_a?(Hash)
@excepts = options[:except]
@values = options[:value]
@proc = options[:proc]
- warn '[DEPRECATION] The values validator except option is deprecated. ' \
- 'Use the except validator instead.' if @excepts
+ ActiveSupport::Deprecation.warn('The values validator except option is deprecated. Use the except validator instead.') if @excepts
raise ArgumentError, 'proc must be a Proc' if @proc && !@proc.is_a?(Proc)
- warn '[DEPRECATION] The values validator proc option is deprecated. ' \
- 'The lambda expression can now be assigned directly to values.' if @proc
+ ActiveSupport::Deprecation.warn('The values validator proc option is deprecated. The lambda expression can now be assigned directly to values.') if @proc
else
@excepts = nil
@values = nil
@proc = nil
@values = options
@@ -43,11 +41,11 @@
raise validation_exception(attr_name, message(:values)) \
unless check_values(param_array, attr_name)
raise validation_exception(attr_name, message(:values)) \
- if @proc && !param_array.all? { |param| @proc.call(param) }
+ if @proc && !validate_proc(@proc, param_array)
end
private
def check_values(param_array, attr_name)
@@ -66,9 +64,20 @@
def check_excepts(param_array)
excepts = @excepts.is_a?(Proc) ? @excepts.call : @excepts
return true if excepts.nil?
param_array.none? { |param| excepts.include?(param) }
+ end
+
+ def validate_proc(proc, param_array)
+ case proc.arity
+ when 0
+ param_array.all? { |_param| proc.call }
+ when 1
+ param_array.all? { |param| proc.call(param) }
+ else
+ raise ArgumentError, 'proc arity must be 0 or 1'
+ end
end
def except_message
options = instance_variable_get(:@option)
options_key?(:except_message) ? options[:except_message] : message(:except_values)