Skip to content

Commit

Permalink
Add shared cron :schedule schema validator
Browse files Browse the repository at this point in the history
  • Loading branch information
sorentwo committed Dec 1, 2023
1 parent 731ba63 commit 6deea9c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
12 changes: 1 addition & 11 deletions lib/oban/plugins/reindexer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ defmodule Oban.Plugins.Reindexer do
conf: :ok,
name: :ok,
indexes: {:list, :string},
schedule: {:custom, &validate_schedule/1},
schedule: :schedule,
timeout: :timeout,
timezone: :timezone
)
Expand Down Expand Up @@ -127,16 +127,6 @@ defmodule Oban.Plugins.Reindexer do
{:noreply, schedule_reindex(state)}
end

# Validation

defp validate_schedule(schedule) do
Expression.parse!(schedule)

:ok
rescue
error in [ArgumentError] -> {:error, error.message}
end

# Scheduling

defp schedule_reindex(state) do
Expand Down
9 changes: 9 additions & 0 deletions lib/oban/validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ defmodule Oban.Validation do
{:error, "expected #{inspect(key)} to be between #{min}..#{max}, got: #{inspect(val)}"}
end

defp validate_type(:schedule, key, val) do
Oban.Cron.Expression.parse!(val)

:ok
rescue
error in [ArgumentError] ->
{:error, "expected #{inspect(key)} to be a valid cron schedule, got: #{error.message}"}
end

defp validate_type(:string, key, val) when not is_binary(val) do
{:error, "expected #{inspect(key)} to be a string, got: #{inspect(val)}"}
end
Expand Down

0 comments on commit 6deea9c

Please sign in to comment.