来自a空的abody&quot值返回零模型(GUID和ENUM)
我有一个问题来验证我的模型中某些类型的值(使用Fluent验证和从Body中)。
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):
{ “用户身份”: ””, “模块”:[ { “ID”: ””, “读”:是的, “插入”:是的, “更新”:是的, “排除”:是的 } 这是给出的 }
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. (抛出模型状态无效)。
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.
{ “名称”:“字符串”, “状态”:“测试” }
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".
(当我使用Formform时,它可以正常工作,但是我需要从Body或类似物体的用户使用)。
如果有人可以帮助我,我会真的很感激!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论