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.

Can't write custom WhenNotNull extension in v10

See original GitHub issue

FluentValidation version

10.0.0

ASP.NET version

No response

Summary

Now that IValidationContext doesn’t expose PropertyValue, I can’t write custom WhenNotNull, WhenNotEmpty, etc., extension methods.

In v9 I defined these (and more) extension methods:

public static IRuleBuilderOptions<T, TProperty> WhenNotNull<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
{
    return rule.Configure(config => config.ApplyCondition(context => context.PropertyValue != null, applyConditionTo));
}

public static IRuleBuilderOptions<T, string> WhenNotEmpty<T>(this IRuleBuilderOptions<T, string> rule, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
{
    return rule.Configure(config => config.ApplyCondition(context => !string.IsNullOrEmpty((string)context.PropertyValue), applyConditionTo));
}

And used them extensively like this:

public class MyClassValidator : AbstractValidator<MyClass>
{
    public MyClassValidator()
    {
        RuleFor(x => x.MyDictionary).MaximumSize(50, 40, 500).WhenNotNull(); // MaximumSize validates maxElements, maxKeyLength, maxValueLength for an IDictionary
        RuleFor(x => x.MyString).MaximumLength(10).WhenNotEmpty();
    }
}

I read the upgrade guide (and also #1672) but still couldn’t get it to work.

Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Apr 8, 2021

I’ve pushed out 10.0.1 to nuget with this change. I had to do it slightly differently and expose a public method rather than exposing the PropretyFunc directly. You can call this new method inside your extension like this:

public static IRuleBuilderOptions<T, TProperty> WhenNotNull<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
{
    return rule.Configure(config => 
    {
      config.ApplyCondition(context => config.GetPropertyValue(context.InstanceToValidate) != null, applyConditionTo);
    });
}

It’s untyped, returning object but there’s no way around this unfortunately as the TProperty on the extension method won’t match the property value when working with collections (inside RuleForEach collections, TProperty references the type of the current collection element, rather than the collection property type returned by the PropertyFunc, so the new method has to return object in order to correctly receive the collection reference when called as part of RuleForEach).

0reactions
gabrielmaldicommented, Apr 8, 2021

Just upgraded to 10.0.1, refactored my extension methods to use GetPropertyValue and now everything works perfectly!

Thanks a lot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

file reading and Gateway extensions in v10 | API Connect
Hi All,--> Is there any way to read .csv files, what is its maximum readable file size and what are the formats supported...
Read more >
Null Substitution
Null substitution allows you to supply an alternate value for a destination member if the source value is null anywhere along the member...
Read more >
API Connect - Configuring your Gateway server extensions
Configuring your Gateway server extensions. You can add extra IBM® DataPower® enforcement capabilities to a Gateway service by using API Connect CLI ...
Read more >
Fast Models User Guide
Fast Models tools enable you to create custom system models from the library of component models supplied in the Fast Models portfolio.
Read more >
Glossary
Fields that are typically null, and which are only rendered when not null. ... Relay supports adding types and fields in client schema...
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