ValidationModulesValidation for .NET, written at build time
Declare rules with attributes or in a rules class. A source generator turns them into a validator class of plain C#, which runs under Native AOT.
Declare rules with attributes or in a rules class. A source generator turns them into a validator class of plain C#, which runs under Native AOT.
using ValidationModules.Constraints;
public sealed class SignUp
{
[Required, EmailAddress]
public string? Email { get; init; }
[Required, StringLength(40, Min = 3)]
public string? DisplayName { get; init; }
[Range(18, 120)]
public int Age { get; init; }
}The generator writes SignUpValidator from these attributes. Validating new SignUp { Email = "not-an-email", DisplayName = "", Age = 12 } with it returns three errors:
Field | Code | Message |
|---|---|---|
email | email | email is not a valid email address. |
displayName | required | displayName is required. |
age | range | age must be between 18 and 120. |
System.ComponentModel.DataAnnotations, such as [Required], [StringLength] and [Range]. Models that already use DataAnnotations attributes compile as they are.| Package | Contents |
|---|---|
| ValidationModules.Runtime | Attributes, IValidatorFor<T>, results, and service registration. |
| ValidationModules.SourceGenerator | The source generator and its analyzers. |
| ValidationModules.AspNetCore | An endpoint filter for minimal APIs and RFC 9457 problem details. |
| ValidationModules.Options | Options validation when the host starts. |
| ValidationModules.Messages | Messages in German, Spanish, French, Japanese and Chinese. |
| ValidationModules.SourceGenerator.Impl | The generator's source, for authors of other generators. |