Skip to main content

Curiosus.DataAnnotations

NuGet Downloads Coverage

Small additions to System.ComponentModel.DataAnnotations: range attributes bound to an enum, a one-call object validator and a helper that reads [StringLength] limits.

Installation​

dotnet add package Curiosus.DataAnnotations

Usage​

using System.ComponentModel.DataAnnotations;
using Curiosus.DataAnnotations;

public enum OrderStatus
{
New = 1,
Paid = 2,
Shipped = 3,
}

public class OrderRequest
{
[StringLength(64)]
public string Title { get; set; } = null!;

// valid range is 1..max value of the enum; use EnumRange(0, typeof(...)) to allow 0
[EnumRange(typeof(OrderStatus))]
public OrderStatus Status { get; set; }
}

var request = new OrderRequest { Title = "Books", Status = (OrderStatus)10 };

if (!DataAnnotationsValidator.TryValidate(request, out var results))
{
foreach (var result in results) Console.WriteLine(result.ErrorMessage);
}

int? maxTitleLength = request.GetPropertyMaxLength(nameof(OrderRequest.Title)); // 64

EnumRangeAttribute is for int-based enums, EnumLongRangeAttribute for long-based ones. DataAnnotationsValidator.TryValidate validates all properties of the object (not nested objects).

See also​