Skip to content

Commit

Permalink
Add sanity checks for alert's time_window size
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Oct 1, 2024
1 parent 38b58dc commit a512e7f
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/sanbase/utils/validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ defmodule Sanbase.Validation do
do: {:error, "#{inspect(percent)} is not a valid percent"}

def valid_time_window?(time_window) when is_binary(time_window) do
Regex.match?(~r/^\d+[smhdw]$/, time_window)
|> case do
true -> :ok
false -> {:error, "#{inspect(time_window)} is not a valid time window"}
with :ok <- time_window_format_check(time_window),
:ok <- time_window_sanity_check(time_window) do
:ok
end
end

Expand Down Expand Up @@ -125,4 +124,24 @@ defmodule Sanbase.Validation do
:ok
end
end

# Private functions

defp time_window_format_check(time_window) do
case Regex.match?(~r/^\d+[smhdw]$/, time_window) do
true -> :ok
false -> {:error, "#{inspect(time_window)} is not a valid time window"}
end
end

@year_in_seconds 365 * 24 * 60 * 60
defp time_window_sanity_check(time_window) do
case str_to_sec(time_window) do
seconds when seconds >= @year_in_seconds ->
{:error, "The time_window parameter must not be bigger than 1 year"}

_ ->
:ok
end
end
end

0 comments on commit a512e7f

Please sign in to comment.