MicroElements.Swashbuckle.FluentValidation AddFluentValidationRules使用命令处理程序模式

问题描述 投票:1回答:1

尝试使用https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/microservice-application-layer-implementation-web-api中的命令处理程序模式来使MicroElements.Swashbuckle.FluentValidation正常工作>

使用ASP .Net Core 2.2MicroElements.Swashbuckle.FluentValidation v3.0.0-alpha.1(作为程序集而不是程序包引用)Swashbuckle.AspNetCore 5.0.0-rc2

我有这个是Startup.cs

return services.AddSwaggerGen(setup =>
            {
                setup.AddFluentValidationRules();                
            });

使用流利验证

这不会将Fluent验证提取到请求主体对象的架构中。

    public class AddModelsCommandValidator : AbstractValidator<AddModelsCommand>
    {
        public AddModelsCommandValidator()
        {
            //1. validate request
            RuleFor(e => e.Model).InvalidRequestValidation();

            When(x => x.Model != null, () =>
            {
                //2. validate request body
                RuleFor(e => e.Model.ModelCode).StringRequiredValidation();
                RuleFor(e => e.Model.ModelCode).StringMaxLengthValidation(5);
                RuleFor(e => e.Model.ProgramName).StringRequiredValidation();
                RuleFor(e => e.Model.ProgramName).StringMaxLengthValidation(50);
            });
        }
    }

    public class AddModelsCommand : IRequest<AddModelsCommandResult>
    {
        public Model Model { get; }

        public AddModelsCommand(Model model)
        {
            Model = model;
        }
    }

    public class Model
    {   
        /// <summary>
        /// Unique code of the Model
        /// </summary>
        public string ModelCode { get; set; }

        /// <summary>
        /// The name of the Program
        /// </summary>
        public string ProgramName { get; set; }
    }   

以下代码does

将Fluent验证提取到请求正文对象的架构中。 (因为1. AbstractValidator在Model上而不是Command上,并且2.我删除了条件When()验证)
public class AddModelsCommandValidator : AbstractValidator<Model>
    {
        public AddModelsCommandValidator()
        {

                //2. validate request body
                RuleFor(e => e.ModelCode).StringRequiredValidation();
                RuleFor(e => e.ModelCode).StringMaxLengthValidation(5);
                RuleFor(e => e.ProgramName).StringRequiredValidation();
                RuleFor(e => e.ProgramName).StringMaxLengthValidation(50);
        }
    }

是否可以调用AddFluentValidationRules并使用命令处理程序模式?

尝试从https://docs.microsoft.com/zh-cn/dotnet/architecture/microservices/microservice-ddd-cqrs -...中使用命令处理程序模式来使MicroElements.Swashbuckle.FluentValidation正常工作。]] >

这里的问题是要使用的AbstractValidator<Model> 已经

。[C0毫无意义
swagger asp.net-core-2.1 fluentvalidation swashbuckle
1个回答
0
投票

这里的问题是要使用的AbstractValidator<Model> 已经

© www.soinside.com 2019 - 2024. All rights reserved.