Add nwe DataAnnotation DatetimeNotInThePast

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-10 00:01:57 +03:00
parent 80525fa4be
commit 0d1e880d18

View file

@ -0,0 +1,22 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace Retroactiune.DataAnnotations
{
public class DatetimeNotInThePast : ValidationAttribute
{
/// <summary>
/// Validates the given DateTime object to be null or greater than the UtcNow date.
/// </summary>
/// <param name="value">An DateTime object.</param>
/// <returns>True if the date is null or in the future, false otherwise.</returns>
public override bool IsValid(object value)
{
// TODO: Test
var now = DateTime.UtcNow;
var date = value as DateTime?;
return !(date <= now);
}
}
}