question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fluent validation against a group of properties

See original GitHub issue

Is there a way to specify more than one property as part of a rule so that they can all be checked at the same time, probably as part of a custom validator. Something like

RuleFor(f => f.ProductA).AndFor(f => f.ProductB).AndFor(f => ProductC).Must(AtLeastOneProduct).WithMessage("Select at least one product")

private bool AtLeastOneProduct(bool productA, bool productB, bool productC)
{
    return productA || productB || productC; 
}

Which would execute once and product a error for each of the properties.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
JeremySkinnercommented, Oct 5, 2016

So the ShouldHaveValidationErrorFor method is for testing against properties. As you’re using a model level rule you can’t use this method- you should execute the Validate method directly and run your assertions on the returned ValidationResult (you can iterate over the Errors collection to find if any errors match this case, I’d tend to use the PropertyName to identify the appropriate error, which I believe will just be an empty string for model-level rules).

Edit: In this case, as you’ve used WithName you can actually find based on PropertyName = Destination. Example:

[Fact]
public void Example() {
    var validator = new ContentMenuValidator();
    var result = validator.Validate(new ContentMenu {Id = 1, Text = "Test", PageId = null, Url = null});

    // Find the validation result with the expected error
    var matchingErrors = result.Errors.Where(x => x.PropertyName == "Destination").ToList();
    Assert.Equal(1, matchingErrors.Count);
    Assert.Equal("You must select a page or enter a URL for a menu", matchingErrors[0].ErrorMessage);
       // .... other assertions etc. 
}

Hope that helps

1reaction
LeahPikecommented, Oct 5, 2016

I’ve added a rule against the entire model to handle the user having to have one of two properties populated.

RuleFor(e => e).Must(e => e.PageId.HasValue || !string.IsNullOrEmpty(e.Url)).WithName("Destination").WithMessage("You must select a page or enter a URL for a menu");

How do I run a test against this?

_validator.ShouldHaveValidationErrorFor(e => e, new ContentMenu { Id = 1, Text ="Test", PageId = null, Url = null });

It doesn’t like the above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FluentValidation rule for multiple properties
This works but I want to pass both Zip and county to the rue in order to validate. What is the best method...
Read more >
Validating specific properties
When working with sub-properties of collections, you can use a wildcard indexer ( [] ) to indicate all items of a collection. For...
Read more >
Validating Objects With FluentValidation
With FluentValidation, we can check if properties are empty or null, and also if numeric fields are greater than zero.
Read more >
Built-In, Nested, Custom Validators with FluentValidation
This is where we can make use of the range validators. Let's get back to the OrderValidator class and add a validator against...
Read more >
Hacking Fluent Validation: Configuring via Attributes
If you aren't familiar with FluentValidation , it's a validation framework. If you hate attributes as much as I do it's a great...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found