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.

RuleForEach.SetValidator ignores when clause

See original GitHub issue

System 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:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
JeremySkinnercommented, Jul 22, 2019

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 the Where method for filtering elements inside the loop), but I need to do some testing to see if this would be a breaking change.

0reactions
BadgerCodecommented, Jul 24, 2019

Excellent. Thank you!

Read more comments on GitHub >

github_iconTop 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 >

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