Add request validation with FluentValidation
Introduced `ValidationBehavior` to validate MediatR requests using FluentValidation. Registered the behavior in the pipeline and added validators for `ReadDocumentDetailsQuery` and `ReadDocumentQuery`. Updated dependency injection to include validators and added FluentValidation NuGet packages. Improved exception handling with `BadRequestException` for validation failures. Refactored namespaces and imports to support the changes.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Documents.Queries;
|
||||
|
||||
public class ReadDocumentQueryValidator : AbstractValidator<ReadDocumentQuery>
|
||||
{
|
||||
public ReadDocumentQueryValidator()
|
||||
{
|
||||
RuleFor(x => x)
|
||||
.Must(x => (x.Id ?? 0) > 0 || (x.EnvelopeId ?? 0) > 0)
|
||||
.WithMessage("Id or EnvelopeId is required.");
|
||||
|
||||
RuleFor(x => x.Id)
|
||||
.GreaterThan(0)
|
||||
.When(x => x.Id.HasValue)
|
||||
.WithMessage("Id must be greater than 0.");
|
||||
|
||||
RuleFor(x => x.EnvelopeId)
|
||||
.GreaterThan(0)
|
||||
.When(x => x.EnvelopeId.HasValue)
|
||||
.WithMessage("EnvelopeId must be greater than 0.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user