I would like to limit the value for my DTO.
I have an enum of months:
public enum Month
{
January,
February,
March,
May,
June,
July,
August,
September,
October,
November,
December
}
Now in my DTO I have:
public class MyDTO {
public Month Month{ get; set; }
}
I need to use a Data Annotation in order to accept only From March
to October
, and test it. with Xunit
as https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-dotnet-test
[Fact]
public void valid DTO_Months_ok()
{
var dto = MyDTO()
{
Month = Month.March
}
var result = dto.validate();
Assert.Equal(0, result.Count);
}
And check with Error
[Fact]
public void valid DTO_Months_Error()
{
var dto = MyDTO()
{
Month = Month.February
}
var result = dto.validate();
Assert.Equal(1, result.Count);
}
My problem, is with the DataAnnotation.
How to use DataAnnotation with Enum?