ASP.NET Core-验证合同中仅指定一个参数

发布于 2025-02-11 10:28:55 字数 1092 浏览 1 评论 0原文

我有以下合同:

public class UpdateUserRequest
{
    public string Id { get; set; }
    public string Email { get; set; }
    public string AccountId { get; set; }
}

我想验证只有一个更新三个的属性。我使用ivalidtableObject来实现这一点:

public class UpdateUserRequest : IValidatableObject
{
    public string Id { get; set; }
    public string Email { get; set; }
    public string AccountId { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if ((Id is null && Email is null && AccountId is null) || 
                (Id is not null && Email is not null && AccountId is not null))
            {
                yield return new ValidationResult("Exactly one parameter must be specified.",
                    new[] { nameof(Id), nameof(Email), nameof(AccountId) });
            }
        }
}

在提供全部三个的情况下有效或 none 。但是,如果提供两个,该怎么办?是否有一种更简单的方法来实现仅提供一个参数的验证,而不是多个检查?

I have the following contract:

public class UpdateUserRequest
{
    public string Id { get; set; }
    public string Email { get; set; }
    public string AccountId { get; set; }
}

I want to validate that only one update property of the three is set in this request. I used IValidtableObject to achieve this:

public class UpdateUserRequest : IValidatableObject
{
    public string Id { get; set; }
    public string Email { get; set; }
    public string AccountId { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if ((Id is null && Email is null && AccountId is null) || 
                (Id is not null && Email is not null && AccountId is not null))
            {
                yield return new ValidationResult("Exactly one parameter must be specified.",
                    new[] { nameof(Id), nameof(Email), nameof(AccountId) });
            }
        }
}

That works in stopping cases where all three are supplied or none. But what do I do if two are supplied? Is there an easier way to achieve validation that only one parameter is supplied, instead of multiple if checks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们的影子 2025-02-18 10:28:55

最简单的方法是对它们进行计数:

if (((Id == default ? 0 : 1) 
    + (Email == default ? 0 : 1)
    + (AccountId == default ? 0 : 1)) != 1)
{
    yield return new ValidationResult(
        "Exactly one parameter must be specified.",
        new[] { nameof(Id), nameof(Email), nameof(AccountId) });
}

您可以使用一种实用程序方法概括它,该方法使用反射来枚举请求对象上的属性并返回非默认属性的计数,但似乎有些沉重。

Simplest way is to just count them:

if (((Id == default ? 0 : 1) 
    + (Email == default ? 0 : 1)
    + (AccountId == default ? 0 : 1)) != 1)
{
    yield return new ValidationResult(
        "Exactly one parameter must be specified.",
        new[] { nameof(Id), nameof(Email), nameof(AccountId) });
}

You could generalise it with a utility method that uses reflection to enumerate the properties on the request object and returns a count of non-default properties, but seems a little heavy handed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文