Skip to content

Commit

Permalink
Adding String.trim to Ecto Type cast() (#89)
Browse files Browse the repository at this point in the history
* Update ecto_type_test.exs

Added test for cast with whitespace

* Update ecto_type.ex

* Applied mix format

Co-authored-by: Jonatan Männchen <[email protected]>
  • Loading branch information
h4cc and maennchen committed Jan 14, 2022
1 parent 81bd15a commit 878757d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/crontab/cron_expression/ecto_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ end
def cast(cron_expression = %Crontab.CronExpression{}), do: {:ok, cron_expression}

def cast(cron_expression) when is_binary(cron_expression) do
case Parser.parse(cron_expression) do
cron_expression
|> String.trim()
|> Parser.parse()
|> case do
result = {:ok, _} -> result
_ -> :error
end
Expand Down
6 changes: 6 additions & 0 deletions test/crontab/cron_expression/ecto_type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ end
assert Type.cast("*") == {:ok, ~e[*]}
end

test "cast/1 String with whitespace" do
assert Type.cast("* ") == {:ok, ~e[*]}
assert Type.cast(" * ") == {:ok, ~e[*]}
assert Type.cast(" *") == {:ok, ~e[*]}
end

test "cast/1 CronExpression" do
assert Type.cast(~e[*]) == {:ok, ~e[*]}
end
Expand Down

0 comments on commit 878757d

Please sign in to comment.