From 878757d26ca679da677899c194042db3fd3b26e8 Mon Sep 17 00:00:00 2001 From: Julius Beckmann Date: Fri, 14 Jan 2022 16:20:33 +0100 Subject: [PATCH] Adding String.trim to Ecto Type cast() (#89) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update ecto_type_test.exs Added test for cast with whitespace * Update ecto_type.ex * Applied mix format Co-authored-by: Jonatan Männchen --- lib/crontab/cron_expression/ecto_type.ex | 5 ++++- test/crontab/cron_expression/ecto_type_test.exs | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/crontab/cron_expression/ecto_type.ex b/lib/crontab/cron_expression/ecto_type.ex index dba6646..5c8481c 100644 --- a/lib/crontab/cron_expression/ecto_type.ex +++ b/lib/crontab/cron_expression/ecto_type.ex @@ -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 diff --git a/test/crontab/cron_expression/ecto_type_test.exs b/test/crontab/cron_expression/ecto_type_test.exs index 8eda42d..5573eea 100644 --- a/test/crontab/cron_expression/ecto_type_test.exs +++ b/test/crontab/cron_expression/ecto_type_test.exs @@ -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