Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 1.45 KB

File metadata and controls

50 lines (33 loc) · 1.45 KB

MessageValidation.DataAnnotations

A DataAnnotations adapter for MessageValidation - validate messages using standard System.ComponentModel.DataAnnotations attributes in the MessageValidation pipeline.

Installation

dotnet add package MessageValidation.DataAnnotations

Quick Start

1. Decorate your message with attributes

using 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; }
}

2. Register the adapter

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.

Supported 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.

Requirements