RuleForEach.SetValidator ignores when clause
See original GitHub issueSystem Details
- FluentValidation version: 8.4.0
Issue Description
Steps to reproduce
Use the following rule in an AbstractValidator
RuleForEach(p => p)
.SetValidator(c => new CustomValidator())
.When(p => false);
Expected behaviour
The rule does not run, as the when
will always evaluate to false.
Actual behaviour The rule runs.
Complete Example
public class ClassA
{
public ClassB OptionalProperty { get; set; }
}
public class ClassB
{
public List<ClassC> Collection { get; set; }
}
public class ClassC
{
public string Name { get; set; }
}
public class ClassAValidator : AbstractValidator<ClassA>
{
public ClassAValidator()
{
// Correct behaviour. Generates a deprecated warning
RuleFor(p => p.OptionalProperty.Collection)
.SetCollectionValidator(c => new ClassCValidator())
.When(p => p.OptionalProperty != null);
// Blows up- NullReferenceException
RuleForEach(p => p.OptionalProperty.Collection)
.SetValidator(c => new ClassCValidator())
.When(p => p.OptionalProperty != null);
}
}
public class ClassCValidator : AbstractValidator<ClassC>
{
public ClassCValidator()
{
RuleFor(c => c.Name).MaximumLength(20);
}
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:8 (5 by maintainers)
Top Results From Across the Web
FluentValidation rule for null object
If you use SetValidator with an AbstractValidator derivative, then it will not be run if the property value is null. This is intentional ......
Read more >Conditions — FluentValidation documentation
If you only want the condition to apply to the validator that immediately precedes the condition, you must explicitly specify this: RuleFor(customer =>...
Read more >Validation - Laravel 10.x - The PHP Framework For Web ...
In this example, we are specifying that the publish_at field may be either null or a valid date representation. If the nullable modifier...
Read more >YARA-L 2.0 language syntax | Chronicle Security
This statement returns $var1 and $var2 (defined in the events section) when the rule finds a match. The time specified is 5 minutes....
Read more >Rule Language Reference
You can use DRL comments to annotate rules or any related components in DRL files. DRL comments are ignored by the Drools rule...
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
Also, I think that the behaviour isn’t particularly intutive, so I’ll think about changing this so that calls to
When
do run outside the loop (we already have theWhere
method for filtering elements inside the loop), but I need to do some testing to see if this would be a breaking change.Excellent. Thank you!