From 0d1e880d18cfd8f5a15def4c25e7dfcda3fcc7a4 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Thu, 10 Jun 2021 00:01:57 +0300 Subject: [PATCH] Add nwe DataAnnotation DatetimeNotInThePast --- .../DataAnnotations/DatetimeNotInThePast.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Retroactiune.WebAPI/DataAnnotations/DatetimeNotInThePast.cs diff --git a/Retroactiune.WebAPI/DataAnnotations/DatetimeNotInThePast.cs b/Retroactiune.WebAPI/DataAnnotations/DatetimeNotInThePast.cs new file mode 100644 index 0000000..e3ee189 --- /dev/null +++ b/Retroactiune.WebAPI/DataAnnotations/DatetimeNotInThePast.cs @@ -0,0 +1,22 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace Retroactiune.DataAnnotations +{ + public class DatetimeNotInThePast : ValidationAttribute + { + + /// + /// Validates the given DateTime object to be null or greater than the UtcNow date. + /// + /// An DateTime object. + /// True if the date is null or in the future, false otherwise. + public override bool IsValid(object value) + { + // TODO: Test + var now = DateTime.UtcNow; + var date = value as DateTime?; + return !(date <= now); + } + } +} \ No newline at end of file