Skip to content

Commit

Permalink
validate boolean option in connection string. proper downcase.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian J. Cardiff committed Dec 7, 2016
1 parent 4721ecb commit da2831c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 4 additions & 0 deletions spec/db_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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", 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
11 changes: 8 additions & 3 deletions src/db.cr
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,15 @@ module DB

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

0 comments on commit da2831c

Please sign in to comment.