English 中文(简体)
如何通过使用流利器对不止一种财产进行认证?
原标题:How to validate for null more than one properties by using FluentValidator?

是否有办法以流利的方式证明一种以上财产无效?

例如,不使用挥发器,可能可以通过实施四甲苯胺。

    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使用流放器的方法,但这并不是为了产生挥发性变压器。

    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();
        }
    }
最佳回答

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");
    }
}
问题回答

我发现,这样做的另一个方式/方式是:

// 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, ... })
    ...




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签