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.
24 lines
694 B
C#
24 lines
694 B
C#
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.");
|
|
}
|
|
}
|