A DataAnnotations adapter for MessageValidation - validate messages using standard System.ComponentModel.DataAnnotations attributes in the MessageValidation pipeline.
dotnet add package MessageValidation.DataAnnotationsusing System.ComponentModel.DataAnnotations;
public class TemperatureReading
{
[Required(ErrorMessage = "SensorId is required.")]
public string SensorId { get; set; } = "";
[Range(-50, 150, ErrorMessage = "Value must be between -50 and 150.")]
public double Value { get; set; }
}builder.Services.AddMessageValidation(options =>
{
options.MapSource<TemperatureReading>("sensors/+/temperature");
});
builder.Services.AddMessageDataAnnotationsValidation();That's it. The pipeline will automatically validate any message using its DataAnnotations attributes.
All standard System.ComponentModel.DataAnnotations attributes are supported, including [Required], [Range], [StringLength], [RegularExpression], [EmailAddress], [Phone], and [CustomValidation].
Messages implementing IValidatableObject are also supported for cross-property validation.
- .NET 10+
- MessageValidation >= 0.3.0