From b495ac9952a576a9311f6f737a9107291b3b65e5 Mon Sep 17 00:00:00 2001 From: Ivan Necas Date: Wed, 25 Apr 2012 16:09:53 +0200 Subject: [PATCH] array validator not accepting boolean attributes when having something like: param :recursive, [true, false] the validator fails (undefined method `TrueClass' for Kernel:Module). Simplifying the array validator not to try typecast the values for now. Maybe there could be some option for that in the param to enable something like this explicitly. Implicit conversions don't fit here very well. If I say I expect a param to be one of [1, 2, 3] I might not be happy getting string instead of integer. --- lib/restapi/validator.rb | 12 +----------- spec/controllers/users_controller_spec.rb | 2 +- spec/dummy/app/controllers/users_controller.rb | 1 + 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/lib/restapi/validator.rb b/lib/restapi/validator.rb index 2ca8b53..e438d1d 100644 --- a/lib/restapi/validator.rb +++ b/lib/restapi/validator.rb @@ -116,17 +116,7 @@ def initialize(param_description, argument) end def validate(value) - - @array.find do |expected| - expected_class = expected.class - expected_class = Integer if expected_class == Fixnum - begin - converted_value = Kernel.send(expected_class.to_s, value) - rescue ArgumentError - false - end - converted_value === expected - end + @array.include?(value) end def self.build(param_description, argument, options, proc) diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index c037dc3..795b1b1 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -155,7 +155,7 @@ assert_response :success get :show, :id => 5, :session => "secret_hash", :array_param => 1 assert_response :success - get :show, :id => 5, :session => "secret_hash", :array_param => "2" + get :show, :id => 5, :session => "secret_hash", :boolean_param => false assert_response :success end diff --git a/spec/dummy/app/controllers/users_controller.rb b/spec/dummy/app/controllers/users_controller.rb index d91693e..2db62fe 100644 --- a/spec/dummy/app/controllers/users_controller.rb +++ b/spec/dummy/app/controllers/users_controller.rb @@ -47,6 +47,7 @@ class UsersController < ApplicationController param :float_param, Float, :desc => "float param" param :regexp_param, /^[0-9]* years/, :desc => "regexp param" param :array_param, [100, "one", "two", 1, 2], :desc => "array validator" + param :boolean_param, [true, false], :desc => "array validator with boolean" param :proc_param, lambda { |val| val == "param value" ? true : "The only good value is 'param value'." }, :desc => "proc validator"