RequiredAttribute<T>
Checks whether the data field of type Optional<T> has a value.
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;
namespace DotNext.ComponentModel.DataAnnotations
{
[NullableContext(2)]
[Nullable(0)]
public sealed class RequiredAttribute<T> : RequiredAttribute
{
public bool AllowNull { get; set; }
public override bool IsValid(object value)
{
if (value is Optional<T>) {
ref Optional<T> reference = ref Unsafe.Unbox<Optional<T>>(value);
if (!reference.IsNull)
return reference.HasValue;
return AllowNull;
}
return false;
}
}
}