gpt4 book ai didi

Granular Custom Model Validation in ASP.NET Web API(ASP.NET Web API中的细粒度定制模型验证)

转载 作者:bug小助手 更新时间:2023-10-24 20:20:18 31 4
gpt4 key购买 nike



In an ASP.NET Web API (.NET7) project, I have a data model as follows:

在ASP.NET Web API(.NET7)项目中,我有一个数据模型如下所示:




public class MeetupGroup 
{
public string GroupName { get; set; }
public string MeetingPlace { get; set; }
public ICollection<GroupMember> GroupMembers { get; set; }
}

public class GroupMember
{
public string Email { get; set; }
public string Name { get; set; }
}




We want to enforce the requirement that a Meetup Group must have at least one member at the time of creation.

我们希望强制执行Meetup Group在创建时必须至少有一个成员的要求。


I'd like to do this with a custom model Validation Attribute, rather than in the controller:

我想用一个定制的模型验证属性来做这件事,而不是在控制器中:




public class MembersNotEmptyAttribute : ValidationAttribute
{
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
var members = (GroupMember)value!;

if (!members.Any())
{
return new ValidationResult("At least one member must be present");
}

return ValidationResult.Success;
}
}




But that's just for creating a new Meetup Group (HTTP POST).

但这仅仅是为了创建一个新的Meetup Group(HTTP POST)。


We also need to provide an Update method (HTTP PATCH), but there is no requirement that they provide a Group Member for that. They would just be updating the GroupName and MeetingPlace properties. There will be other methods to add/remove or modify group members.

我们还需要提供一个更新方法(HTTP补丁),但并不要求他们为此提供一个组成员。他们将只更新GroupName和MeetingPlace属性。还会有其他方法来添加/删除或修改组成员。


Is there a way to do this? Or am I stuck enforcing this in the controller?

有办法做到这一点吗?或者是我在控制器中强制执行此操作?


Thanks.

谢谢。


更多回答

the easiest approach would be creating view model which applies the validation and use different view model for each different http verb, but that might be anti-pattern.

最简单的方法是创建应用验证的视图模型,并为每个不同的http动词使用不同的视图模型,但这可能是反模式的。

In the domain perspective, the context of the validation is the action, not the model. So if you have different requirements for updating different parts of your model may be you need to split the updating operations into two: One for the other properties and one for the members.

从域的角度来看,验证的上下文是动作,而不是模型。因此,如果您对更新模型的不同部分有不同的要求,您需要将更新操作拆分为两个:一个用于其他属性,另一个用于成员。

优秀答案推荐

According to your description, if you don't want to let the patch method still need follow the custom validation, I suggest you could put some condition inside the MembersNotEmptyAttribute class.

根据您的描述,如果您不希望Patch方法仍然需要遵循自定义验证,我建议您可以在MembersNotEmptyAttribute类中设置一些条件。


You could get the httpmethod by injecting the HttpContextAccessor and get current request context inside the MembersNotEmptyAttribute.

您可以通过注入HttpConextAccessor来获取HttpMethod,并在MembersNotEmptyAttribute中获取当前请求上下文。


If current request http method is PATCH, then you could just check the groupname and return the valdation result success.

如果当前请求的http方法是Patch,则只需检查组名并返回验证结果成功即可。


More details, you could refer to below codes:

更多详细信息,您可以参考以下代码:


public class MembersNotEmptyAttribute : ValidationAttribute
{
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
var httpContextAccessor = (IHttpContextAccessor)validationContext.GetService(typeof(IHttpContextAccessor));

//get current request's http method
var re = httpContextAccessor.HttpContext.Request.Method;
ICollection<GroupMember> members = (ICollection<GroupMember>)value!;

if (re == "PATCH")
{
//here you could do the custom validation for the PATCH request
}
else
{
//here you could do the custom vaidation for the not PATCH request
if (!members.Any())
{
return new ValidationResult("At least one member must be present");
}

}

return ValidationResult.Success;
}
}

Result:

结果:


enter image description here


更多回答

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com