You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Transform and TransformForEach methods are being deprecated in FluentValidation 11, and will be removed in FluentValidation 12.
Background
Since version 9, FluentValidation has supported transformations - the ability to apply a conversion to a property value before it's validated (for example, converting a string property to a nullable integer for the purposes of conversion). This feature has always been problematic as only simplistic transformations are supported, and there is no caching of the transformed value. Additionally this feature has always felt very much out of scope as it isn't directly related to validation.
As such we've decided to deprecate the Transform and TransformForEach methods and recommend that any transformations be done as computed properties within your model.
Migration
Option 1 - Use a computed property on the model (Recommended)
The example below uses a custom transformation method to transform a string to a nullable int. The updated version moves this logic into a computed property within the model itself.
// ModelclassFoo{publicstringSomeValue{get;set;}}// Old behaviour, using the Transform method:publicclassFooValidator:AbstractValidator<Foo>{publicFooValidator(){
Transform(x => x.SomeValue, to: StringToNullableInt).NotNull().GreaterThan(5);}int?StringToNullableInt(stringvalue)=>int.TryParse(value,outint val)?(int?) val :null;}// After migration - the transformation takes place inside the model and is exposed via a computed property. classFoo{publicstringSomeValue{get;set;}publicint?SomeValueAsInt=>int.TryParse(SomeValue,outint val)?val:null;}publicclassFooValidator:AbstractValidator<Foo>{publicFooValidator(){
RuleFor(x => x.SomeValueAsInt).NotNull().GreaterThan(5);}}
If calculating the transformed value is expensive and needs to be accessed many times then you could optionally cache it:
Option 2 - Perform the transformation within RuleFor (not recommended)
As an alternative, if it's not possible for you to change the definition of the model to include the computed property then you could continue to perform the transformation within the validator by performing the transformation within a call to RuleFor rather than using the Transform method:
publicclassFooValidator:AbstractValidator<Foo>{publicFooValidator(){
RuleFor(x => StringToNullalbleInt(x.SomeValue)).NotNull().GreaterThan(5).OverridePropertyName("SomeValue");// Must include a call to OverridePropertyName as the property name can't be inferred with this approach. }int?StringToNullableInt(stringvalue)=>int.TryParse(value,outint val)?(int?) val :null;}
Note that this approach above is not recommended for the following reasons:
Using a method call within a call to RuleFor means we can't cache the member accessor, making this rule more expensive to run than others
Using a method call within a call to RuleFor means we can't automatically compute the associated property name, so it has to be set explicitly with a call to OverridePropertyName
Because of the above reasons we don't recommend this approach and instead recommend using Option 1 - a computed property on the model.
The text was updated successfully, but these errors were encountered:
Summary
The
Transform
andTransformForEach
methods are being deprecated in FluentValidation 11, and will be removed in FluentValidation 12.Background
Since version 9, FluentValidation has supported transformations - the ability to apply a conversion to a property value before it's validated (for example, converting a string property to a nullable integer for the purposes of conversion). This feature has always been problematic as only simplistic transformations are supported, and there is no caching of the transformed value. Additionally this feature has always felt very much out of scope as it isn't directly related to validation.
As such we've decided to deprecate the
Transform
andTransformForEach
methods and recommend that any transformations be done as computed properties within your model.Migration
Option 1 - Use a computed property on the model (Recommended)
The example below uses a custom transformation method to transform a string to a nullable int. The updated version moves this logic into a computed property within the model itself.
If calculating the transformed value is expensive and needs to be accessed many times then you could optionally cache it:
Option 2 - Perform the transformation within RuleFor (not recommended)
As an alternative, if it's not possible for you to change the definition of the model to include the computed property then you could continue to perform the transformation within the validator by performing the transformation within a call to
RuleFor
rather than using theTransform
method:Note that this approach above is not recommended for the following reasons:
RuleFor
means we can't cache the member accessor, making this rule more expensive to run than othersRuleFor
means we can't automatically compute the associated property name, so it has to be set explicitly with a call toOverridePropertyName
Because of the above reasons we don't recommend this approach and instead recommend using Option 1 - a computed property on the model.
The text was updated successfully, but these errors were encountered: