abp icon indicating copy to clipboard operation
abp copied to clipboard

FluentValidation Triggered But No Validation Applied For DTOs in Custom Module

Open Ramiii0 opened this issue 5 months ago • 5 comments

I created a custom module named JobAnnouncement using the ABP Framework and added FluentValidation for my JobCreateDto class. Although the validator is defined correctly and the module depends on AbpFluentValidationModule, the validation is being triggered, but the validation rules are not being applied when I call the application service in the host project.

✅ Validator Code

using FluentValidation;
using JobAnnouncement.App.Jobs.Dtos;

namespace JobAnnouncement.App.Jobs.Validations
{
    public class JobCreateDtoValidator : AbstractValidator<JobCreateDto>
    {
        public JobCreateDtoValidator()
        {
            RuleFor(x => x.Title).NotNull();
            RuleFor(x => x.Description).NotEmpty();
            RuleFor(x => x.Requirements).NotEmpty();
            RuleFor(x => x.IsActive).NotEmpty();
        }
    }
}

✅ Module Configuration

public async override Task<JobDto> CreateAsync(JobCreateDto input)
{
// some logic

    return await base.CreateAsync(input);
}

✅ Module Configuration

[DependsOn(
    typeof(JobAnnouncementDomainModule),
    typeof(JobAnnouncementApplicationContractsModule),
    typeof(AbpDddApplicationModule),
    typeof(AbpAutoMapperModule),
    typeof(AbpFluentValidationModule)
)]
public class JobAnnouncementApplicationModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddAutoMapperObjectMapper<JobAnnouncementApplicationModule>();

        Configure<AbpAutoMapperOptions>(options =>
        {
            options.AddMaps<JobAnnouncementApplicationModule>(validate: true);
        });
    }
}

What I’ve Tried The validator class is public and inherits from AbstractValidator<JobCreateDto>.

I added AbpFluentValidationModule in the [DependsOn] attribute in my module.

The module is properly referenced and loaded in the host application.

❌ Issue Validation does not run — even when required fields are missing, no validation error or exception is triggered. The CreateAsync method accepts invalid data without any validation feedback.

Ramiii0 avatar Aug 06 '25 09:08 Ramiii0

hi

Please share a project to reproduce the problem.

Thanks.

maliming avatar Aug 07 '25 07:08 maliming

Hi @maliming

Thanks for your response. You can find the project to reproduce the issue here: https://github.com/Ramiii0/XHost

Please let me know if you need any additional details.

Ramiii0 avatar Aug 07 '25 19:08 Ramiii0

hi

What are the steps to reproduce?

Thanks.

maliming avatar Aug 08 '25 01:08 maliming

Hi,

To reproduce the issue:

  1. Clone and run the XHost solution.
  2. Send a POST request to /api/app/game with any of the following payloads:
{
  "title": "A"
}

or

{
  "title": ""
}

Validation Rule:

RuleFor(x => x.Title)
    .NotEmpty()
    .Length(3, 10);

what happens:

  • The validation does not apply — even when the title is just one character or only spaces.
  • The request is processed as if the input were valid.

Let me know if you need anything else.

Thanks!

Ramiii0 avatar Aug 08 '25 15:08 Ramiii0

hi

Try to add IGameAppService controller in XModule.HttpApi project instead of options.ConventionalControllers.Create(typeof(XModuleApplicationModule).Assembly);

Same as https://github.com/Ramiii0/XHost/blob/master/modules/src/XModule.HttpApi/Samples/ExampleController.cs#L8-L33

The Types in ConventionalControllers will disable Interceptor.

maliming avatar Aug 09 '25 04:08 maliming