mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
validate boolean option in connection string. proper downcase.
This commit is contained in:
parent
4721ecbf6b
commit
da2831c17d
2 changed files with 12 additions and 3 deletions
|
@ -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
|
||||||
|
|
11
src/db.cr
11
src/db.cr
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue