'FromBody with a "empty" values return null Model (Guid and Enum)
I'm having a problem to validate certain types of values in my model (using fluent validation and FromBody).
I have this model:
public class PostAcess
{
public Guid UserId { get; set; }
public List<PostAcessModule> Modules { get; set; }
}
That I'm passing as a parameter in my POST method:
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] PostAcess postAcess)
{
if (!ModelState.IsValid) return CustomResponseFail(ModelState);
ViewAcess result = await acessService.GetByUserId(postAcess.userId);
...
}
In Swagger when I make a Post, with this values (UserId empty):
{ "UserId": "", "Modules": [ { "id": "", "read": true, "insert": true, "update": true, "exclude": true } ] }
and debbuging my API, my Model (object parameter) always comes Null in my Controller, so my Model values can't get be validated by my Fluent Validator class. (Throwing a ModelState Invalid).
The same happens when I try to pass a Enum string that didn't exists in my Enum:
Enum:
public enum Status
{
Active = 1,
Inactive,
Excluded
}
When I pass status string as a different value that was spected my Model (object parameter) in the Controller comes null.
{ "name": "string", "status": "Test" }
Validators: Unity
public class PostUnityValidator : AbstractValidator<PostUnity>{
private readonly IUnityRepository unityRepository;
private readonly IMapper mapper;
public Post(IUnityRepository unityRepository,
IMapper mapper)
{
this.unityRepository = unityRepository;
this.mapper = mapper;
RuleFor(x => x.Name)
.NotNull()
.WithMessage("Name can't be null")
.NotEmpty()
.WithMessage("Name can't be empty");
RuleFor(x => x.Status)
.NotNull()
.WithMessage("Status can't be null")
.NotEmpty()
.WithMessage("Status can't be empty")
.IsInEnum()
.WithMessage("Status value doesn't exists");
}}
PostAcess:
public class PostAcessValidator : AbstractValidator<PostAcess>{
private readonly IModuleRepository moduleRepository;
private readonly IUserRepository userRepository;
public PostAcessValidator(IModuleRepository moduleRepository, IUserRepository userRepository)
{
this.moduleRepository = moduleRepository;
this.userRepository = userRepository;
RuleFor(x => x.UserId)
.NotNull()
.WithMessage("ID can't be null")
.NotEmpty()
.WithMessage("ID can't be empty")
.MustAsync(async (userId, cancelar) =>
{
return await UserExists(userId);
}).WithMessage("User doesn't exists");
RuleForEach(x => x.Modules).SetValidator(new PostAcessModuleValidator(moduleRepository));
}
private async Task<bool> UserExists(Guid userId)
{
return await userRepository.UserExists(userId);
}}
PostAcessModule:
public class PostAcessModuleValidator : AbstractValidator<PostAcessModule>{
private readonly IModuleRepository moduleRepository;
public PostAcessModuleValidator(IModuleRepository moduleRepository)
{
this.moduleRepository = moduleRepository;
RuleFor(x => x.Id)
.NotNull()
.WithMessage("ID can't be null")
.NotEmpty()
.WithMessage("ID can't be empty")
.MustAsync(async (moduleId, cancelar) =>
{
return await ModuleExists(moduleId);
}).WithMessage("Module doesn't exists");
RuleFor(x => x.Read)
.NotNull()
.WithMessage("The value can't be null");
RuleFor(x => x.Post)
.NotNull()
.WithMessage("The value can't be null");
RuleFor(x => x.Put)
.NotNull()
.WithMessage("The value can't be null");
RuleFor(x => x.Exclude)
.NotNull()
.WithMessage("The value can't be null");
}
private async Task<bool> ModuleExists(Guid moduleId)
{
return await moduleRepository.ModuleExists(moduleId);
}}
I would like to catch that cases to return a message that "UserId can't be empty" or "Enum value not found".
(When I use FromForm it works but I need to user FromBody or something like that).
If someone could help me, I'll be really thankfull!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|