Can't write custom WhenNotNull extension in v10
See original GitHub issueFluentValidation 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:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top 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 >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
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:
It’s untyped, returning
object
but there’s no way around this unfortunately as theTProperty
on the extension method won’t match the property value when working with collections (insideRuleForEach
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 returnobject
in order to correctly receive the collection reference when called as part of RuleForEach).Just upgraded to 10.0.1, refactored my extension methods to use
GetPropertyValue
and now everything works perfectly!Thanks a lot!