spazio-solazzo/lib/spazio_solazzo/booking_system/validations/future_date.ex
Víctor Martínez 69f992f8f6
feat: new booking system + admin dashboard (#12)
feat: implement a new booking system and admin dashboard
2026-02-07 19:08:39 +01:00

35 lines
965 B
Elixir

defmodule SpazioSolazzo.BookingSystem.Validations.FutureDate do
@moduledoc """
Validates that a date or datetime is in the future relative to UTC now/today.
"""
use Ash.Resource.Validation
@impl true
def init(opts) do
if Keyword.has_key?(opts, :field) do
{:ok, opts}
else
{:error, "The `field` option is required."}
end
end
@impl true
def validate(changeset, opts, _context) do
field = opts[:field]
value = get_value(changeset, field)
if value && in_past?(value) do
{:error, field: field, message: "cannot be in the past"}
else
:ok
end
end
defp in_past?(%Date{} = date), do: Date.compare(date, Date.utc_today()) == :lt
defp in_past?(%DateTime{} = dt), do: DateTime.compare(dt, DateTime.utc_now()) == :lt
defp in_past?(_), do: false
defp get_value(changeset, field) do
Ash.Changeset.get_argument(changeset, field) || Ash.Changeset.get_attribute(changeset, field)
end
end