Fluent validation against a group of properties
See original GitHub issueIs 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:
- Created 7 years ago
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
Hope that helps
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.