FluentValidationSchemaTransformer
Namespace: RA.Utilities.OpenApi.SchemaTransformers
The FluentValidationSchemaTransformer is a powerful utility that serves a single, clear purpose: to automatically enrich your OpenAPI (Swagger) documentation by translating your FluentValidation rules into schema constraints.
In simpler terms, it reads the validation rules you've already written for your request models and applies them directly to your API documentation. For example:
NotNull()orNotEmpty()will mark a property as required.Length(min, max)sets theminLengthandmaxLengthschema properties.Matches("regex")sets thepatternproperty.GreaterThan(10)sets theminimumandexclusiveMinimumproperties.EmailAddress()sets theformatto"email".
🎯 Key Purpose and Benefits
- Single Source of Truth: This is the most significant benefit.
Your
FluentValidationclasses become the single source of truth for validation. You no longer need to duplicate this logic with data attributes like[Required]or[MaxLength]on your models just to get them to show up in the Swagger UI. This reduces code duplication and the risk of your documentation becoming out-of-sync with your actual validation logic. - Improved API Contract: By automatically documenting these rules, you provide a much richer and more accurate API contract to your consumers. Developers using your API can immediately see constraints like required fields, length limits, and data formats directly in the documentation, leading to a better developer experience and fewer invalid requests.
- Automated and Boilerplate-Free: The transformer works automatically. Once registered, it inspects your models and their corresponding validators, eliminating the need for developers to manually annotate every property on every request model.
⚙️ How It Works
For each model in your API:
- It finds the registered IValidator for that model type.
- It inspects the rules for each property.
- It then applies the corresponding OpenAPI schema constraint. For example:
NotNull()orNotEmpty()will mark a property asrequired.Length(min, max)sets theminLengthandmaxLengthschema properties.Matches("regex")sets thepatternproperty.GreaterThan(10)sets theminimumandexclusiveMinimumproperties.EmailAddress()sets theformatto"email".CreditCard()sets theformatto"credit-card".
🚀 How to use it
Simply call the AddFluentValidationRules() extension method when configuring your OpenAPI services.
// In Program.cs
builder.Services.AddOpenApi(options =>
{
// ... other configurations
options.AddFluentValidationRules();
});