From d560c5d548643f28cce37c3dc9690531ecc6f301 Mon Sep 17 00:00:00 2001 From: setop Date: Fri, 3 Apr 2020 23:37:56 +0200 Subject: [PATCH] fix compare begin and end dates this bug prevent from creating an event with the day of month of the begin date greater than the day of month of the end date, event if end date is effectively greater than begin date. for example, if the begin date is '2020-04-28' and end date is '2020-05-13', these dates are valid but because 28 > 13, the validation fails. this is better explained in this article ["Never compare dates in Elixir using < or >"](https://blog.leif.io/never-use-to-compare-dates/). using Date.compare, as proposed in this PR fix the issue. --- lib/mobilizon/events/event.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mobilizon/events/event.ex b/lib/mobilizon/events/event.ex index 13172705d..d3a218a63 100644 --- a/lib/mobilizon/events/event.ex +++ b/lib/mobilizon/events/event.ex @@ -167,7 +167,7 @@ defmodule Mobilizon.Events.Event do case fetch_field(changeset, :begins_on) do {_, begins_on} -> validate_change(changeset, :ends_on, fn :ends_on, ends_on -> - if begins_on > ends_on, + if Date.compare(begins_on, ends_on) == :gt, do: [ends_on: "ends_on cannot be set before begins_on"], else: [] end)