Conditional transforms
See original GitHub issueIs your feature request related to a problem? Please describe. The upcoming changes to Transform in v10 poses some problems for us. In particular that it’s no longer possible to easily combine a condition with a transform. We currently have these extension methods to easily work with nullable values. Basically, if the value is null, don’t validate the property:
public static IRuleBuilderInitial<T, TValue> WithNonNullCondition<T, TValue>(this IRuleBuilderInitial<T, TValue> rule)
=> rule.Configure(r => r.ApplySharedCondition(c => r.PropertyFunc(c.InstanceToValidate) is not null));
public static IRuleBuilder<T, TValue> WhenNotNull<T, TValue>(this IRuleBuilderInitial<T, TValue?> rule)
where TValue : class
=> rule.WithNonNullCondition().Transform(value => value!);
public static IRuleBuilder<T, TValue> WhenNotNull<T, TValue>(this IRuleBuilderInitial<T, TValue?> rule)
where TValue : struct
=> rule.WithNonNullCondition().Transform(value => value!.Value);
That’s no longer doable with the new approach.
The other aspect of this problem is that it’s no longer possible to do this type of stuff as an extension method, because they are now protected methods on AbstractValidator. This makes it a bit hard to make custom extensions that don’t require usage of a different base class (which is doable, but requires some education of developers)
Describe the solution you’d like Some way to only apply the rule if some condition is met.
Describe alternatives you’ve considered Tried implementing as extension method on new API, but does not seem possible.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (9 by maintainers)
Top GitHub Comments
Got everything up and running, and tests are now passing with FV 10.0.0-preview3. Closing this one 😃
Awesome, thanks Jeremy, will check it out.