Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/maru/params/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Maru.Params.Runtime do
nested = h.nested

parsed =
if value in [nil, "", '', %{}] do
if value in [nil, "", ~c"", %{}] do
h.blank_func.({value, passed?})
else
h.parser_func.({:ok, value}, options)
Expand Down
6 changes: 4 additions & 2 deletions lib/maru/params/types/atom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ defmodule Maru.Params.Types.Atom do
if input in values do
{:ok, input |> to_string |> String.to_existing_atom()}
else
{:error, :validate, "allowed values: #{Enum.join(values, ", ")}"}
{:error, :validate,
"Given input `#{input}` not in allowed values: #{Enum.join(values, ", ")}"}
end
rescue
ArgumentError -> {:error, :parse, "not an already existing atom"}
Expand All @@ -41,7 +42,8 @@ defmodule Maru.Params.Types.Atom do
if parsed in values do
{:ok, parsed}
else
{:error, :validate, "allowed values: #{Enum.join(values, ", ")}"}
{:error, :validate,
"Given input `#{parsed}` not in allowed values: #{Enum.join(values, ", ")}"}
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/maru/params/types/boolean.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ defmodule Maru.Params.Types.Boolean do
def parse(nil, _), do: {:ok, false}
def parse(false, _), do: {:ok, false}
def parse("false", _), do: {:ok, false}
def parse(input, _), do: {:error, :parse, "unknown format, expected Boolean, got: #{inspect(input)}"}

def parse(input, _),
do: {:error, :parse, "unknown format, expected Boolean, got: #{inspect(input)}"}
end
10 changes: 5 additions & 5 deletions lib/maru/params/types/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ defmodule Maru.Params.Types.List do

def parse(input, args) when is_binary(input) do
args
|> Map.get(:string_strategy, :codepoints)
|> Map.get(:string_strategy, :error)
|> case do
:codepoints -> String.codepoints(input)
:charlist -> String.to_charlist(input)
:wrap -> List.wrap(input)
:codepoints -> parse(String.codepoints(input), args)
:charlist -> parse(String.to_charlist(input), args)
:error -> {:error, :parse, "expected list, got string: #{inspect(input)}"}
:wrap -> parse(List.wrap(input), args)
end
|> parse(args)
end

def parse(input, _) do
Expand Down
16 changes: 14 additions & 2 deletions test/mixed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule Maru.Params.MixedTest do
optional :d1, String
optional :d2, Integer
end

optional :list_of_strings, List[String]
end
end

Expand Down Expand Up @@ -121,15 +123,25 @@ defmodule Maru.Params.MixedTest do
assert %{
map: %{
m1: %{d1: "d1", d2: 22},
m2: [%{d1: "d1", d2: 22}, %{d1: "dx", d2: 222}]
m2: [%{d1: "d1", d2: 22}, %{d1: "dx", d2: 222}],
list_of_strings: ["1", "2", "3"]
}
} =
T.nested(%{
"map" => %{
"m1" => %{"d1" => "d1", "d2" => 22},
"m2" => [%{"d1" => "d1", "d2" => 22}, %{"d1" => "dx", "d2" => 222}]
"m2" => [%{"d1" => "d1", "d2" => 22}, %{"d1" => "dx", "d2" => 222}],
"list_of_strings" => ["1", "2", "3"]
}
})

assert_raise ParseError,
~r/Error Parsing Parameter `list_of_strings`: expected list, got string:/,
fn ->
T.nested(%{
"map" => %{"list_of_strings" => "bf4be93a-7d5b-11ec-90d6-0242ac120003"}
})
end
end

test "pipeline" do
Expand Down
4 changes: 2 additions & 2 deletions test/types_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ defmodule Maru.Params.TypesTest do

test "integer" do
assert %{i1: 314, i2: 3, i3: -1, i4: 6, i5: 0} =
T.integer(%{"i1" => 314, "i2" => "3", "i3" => "-1", "i4" => '6'})
T.integer(%{"i1" => 314, "i2" => "3", "i3" => "-1", "i4" => ~c"6"})

assert_raise ParseError, ~r/Error Validating Parameter `i2`/, fn ->
T.integer(%{"i2" => 0})
Expand Down Expand Up @@ -216,7 +216,7 @@ defmodule Maru.Params.TypesTest do
assert %{
l1: [1],
l2: [1, 2, 3],
l3: '123',
l3: ~c"123",
l4: ["1", "2"],
l5: ["1", "2"],
l6: ["1", "2"],
Expand Down