Permalink
Browse files

don't typecast arguments implicitly

If I specify the I expect Float I might be not happy with string "1.1". It's
possible to use RegexpValidator instead. The typecasting also might cause
problems (it works only for some types).
  • Loading branch information...
1 parent de25697 commit e070573a3c9c3bb94a1fbeb7984ccf96a8bc8b10 @iNecas iNecas committed Apr 27, 2012
View
@@ -62,11 +62,7 @@ def initialize(param_description, argument)
def validate(value)
return false if value.nil?
- begin
- Kernel.send(@type.to_s, value)
- rescue ArgumentError
- return false
- end
+ value.is_a? @type
end
def self.build(param_description, argument, options, block)
@@ -68,12 +68,6 @@
param.validator.class.should be(Restapi::Validator::TypeValidator)
param.validator.instance_variable_get("@type").should eq(String)
- param = a.params[:float_param]
- param.required.should eq(false)
- param.desc.should eq("\n<p>float param</p>\n")
- param.validator.class.should be(Restapi::Validator::TypeValidator)
- param.validator.instance_variable_get("@type").should eq(Float)
-
param = a.params[:id]
param.required.should eq(true)
param.desc.should eq("\n<p>user id</p>\n")
@@ -108,29 +102,6 @@
}.should raise_error(ArgumentError)
end
- it "should yell ArgumentError if float_param is not a float" do
- lambda {
- get :show,
- :id => 5,
- :session => "secret_hash",
- :float_param => "234.2.34"
- }.should raise_error(ArgumentError)
-
- lambda {
- get :show,
- :id => 5,
- :session => "secret_hash",
- :float_param => "no float here"
- }.should raise_error(ArgumentError)
- end
-
- it "should understand float validator" do
- get :show, :id => 5, :session => "secret_hash", :float_param => "234.34"
- assert_response :success
- get :show, :id => 5, :session => "secret_hash", :float_param => "234"
- assert_response :success
- end
-
it "should understand regexp validator" do
get :show,
:id => 5,
@@ -44,7 +44,6 @@ class UsersController < ApplicationController
param :id, Fixnum, :desc => "user id", :required => true
param :id, Integer, :desc => "user id", :required => true
param :session, String, :desc => "user is logged in", :required => true
- 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"

0 comments on commit e070573

Please sign in to comment.