DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="5.0.0" />

 RequiredAttribute<T>

public sealed class RequiredAttribute<T> : RequiredAttribute
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; } } }