如何使用 FluentValidator 验证多个属性是否为 null?

发布于 2025-01-14 20:14:53 字数 1478 浏览 1 评论 0原文

有没有一种方法可以流畅地验证多个属性是否为空?

例如,在不使用 Fluentvalidator 的情况下,可以通过实现 IValidatableObject 来实现这一点。

    public class Request : IValidatableObject
    {
        public int Id { get; init; }

        public string Property1 { get; init; }
        public string Property2 { get; init; }
        public string Property3 { get; init; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (Property1 == null && Property2 == null && Property3 == null)
            {
                yield return new ValidationResult("Some message");
            }
        }
    }

我找到了一种通过覆盖 Validate 来使用 FluentValidator 的方法,但这不是创建 FluentValidator 的方式。

    public class RequestValidator : AbstractValidator<Request>
    {
        public override FluentValidation.Results.ValidationResult Validate(ValidationContext<Request> context)
        {
            if (context.InstanceToValidate.Property1 is null &&
                context.InstanceToValidate.Property2 is null &&
                context.InstanceToValidate.Property3 is null)
            {
                return new FluentValidation.Results.ValidationResult(new[]
                {
                    new ValidationFailure("", "Custom message")
                });
            }
        
            return new FluentValidation.Results.ValidationResult();
        }
    }

Is there a way to validate for null more than one properties in a fluent manner?

For example, without using Fluentvalidator, this may be possible by implementing IValidatableObject.

    public class Request : IValidatableObject
    {
        public int Id { get; init; }

        public string Property1 { get; init; }
        public string Property2 { get; init; }
        public string Property3 { get; init; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (Property1 == null && Property2 == null && Property3 == null)
            {
                yield return new ValidationResult("Some message");
            }
        }
    }

I found a way to use FluentValidator via override Validate, but this is not the way FluentValidator was created for.

    public class RequestValidator : AbstractValidator<Request>
    {
        public override FluentValidation.Results.ValidationResult Validate(ValidationContext<Request> context)
        {
            if (context.InstanceToValidate.Property1 is null &&
                context.InstanceToValidate.Property2 is null &&
                context.InstanceToValidate.Property3 is null)
            {
                return new FluentValidation.Results.ValidationResult(new[]
                {
                    new ValidationFailure("", "Custom message")
                });
            }
        
            return new FluentValidation.Results.ValidationResult();
        }
    }

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

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

发布评论

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

评论(2

一种可能的解决方案:

public class RequestValidator : AbstractValidator<Request>
{
    public RequestValidator()
    {
        RuleFor(x => x)
            .Must(
                x => x.Property1 is not null
                  || x.Property2 is not null
                  || x.Property3 is not null)
            .WithMessage("Custom message");
    }
}

One possible solution:

public class RequestValidator : AbstractValidator<Request>
{
    public RequestValidator()
    {
        RuleFor(x => x)
            .Must(
                x => x.Property1 is not null
                  || x.Property2 is not null
                  || x.Property3 is not null)
            .WithMessage("Custom message");
    }
}
贩梦商人 2025-01-21 20:14:53

另一种方法/风格,我发现对我来说更具可读性,是:

// I have nullable enabled

public class Request
{
    public int Id { get; init; }

    public string? Property1 { get; init; }
    public string? Property2 { get; init; }
    public string? Property3 { get; init; }
}

public class RequestValidator : AbstractValidator<Request>
{
    public RequestValidator()
    {
        RuleFor(x => new List<string?> { x.Property1, x.Property2, x.Property3 })
            .Must(x => x.Any(x => x is not null))
            .WithMessage("You need to have at least one of Property 1, 2 or 3.");
    }
}

在它们是对象而不是原始类型的情况下,您可以执行类似的操作

RuleFor(x => new List<object?> { x.Property1, x.Property2, ... })
    ...

Another way/style to do it, which I found little more readable to me, is:

// I have nullable enabled

public class Request
{
    public int Id { get; init; }

    public string? Property1 { get; init; }
    public string? Property2 { get; init; }
    public string? Property3 { get; init; }
}

public class RequestValidator : AbstractValidator<Request>
{
    public RequestValidator()
    {
        RuleFor(x => new List<string?> { x.Property1, x.Property2, x.Property3 })
            .Must(x => x.Any(x => x is not null))
            .WithMessage("You need to have at least one of Property 1, 2 or 3.");
    }
}

In the case where they're objects instead of primitive types, you can do something like

RuleFor(x => new List<object?> { x.Property1, x.Property2, ... })
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文