validate boolean option in connection string. proper downcase.

This commit is contained in:
Brian J. Cardiff 2016-12-05 10:38:46 -03:00
parent 4721ecbf6b
commit da2831c17d
2 changed files with 12 additions and 3 deletions

View file

@ -107,5 +107,9 @@ describe DB do
DB.fetch_bool(HTTP::Params.parse("bar=true"), "foo", false).should be_false DB.fetch_bool(HTTP::Params.parse("bar=true"), "foo", false).should be_false
DB.fetch_bool(HTTP::Params.parse("bar=true"), "foo", true).should be_true DB.fetch_bool(HTTP::Params.parse("bar=true"), "foo", true).should be_true
expect_raises(ArgumentError, %(invalid "other" value for option "foo")) do
DB.fetch_bool(HTTP::Params.parse("foo=other"), "foo", true)
end
end end
end end

View file

@ -123,10 +123,15 @@ module DB
# :nodoc: # :nodoc:
def self.fetch_bool(params : HTTP::Params, name, default : Bool) def self.fetch_bool(params : HTTP::Params, name, default : Bool)
if value = params[name]? case (value = params[name]?).try &.downcase
value.underscore == "true" when nil
else
default default
when "true"
true
when "false"
false
else
raise ArgumentError.new(%(invalid "#{value}" value for option "#{name}"))
end end
end end
end end