Skip to content

Commit

Permalink
Deprecate Transform and TransformForEach.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremySkinner committed Feb 20, 2023
1 parent bab4306 commit c6f4476
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
1 change: 0 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ Example

dependentrules
inheritance
transform
advanced

.. _aspnet-docs:
Expand Down
20 changes: 6 additions & 14 deletions docs/transform.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Transforming Values

```eval_rst
.. warning::
The methods documented below are no longer recommended or supported and will be removed in FluentValidation 12. We instead recommend using computed properties on your model if you need to perform a transformation. For details please see `this GitHub issue <https://github.com/FluentValidation/FluentValidation/issues/2072>`_
```

As of FluentValidation 9.5, you can apply a transformation to a property value prior to validation being performed against it. For example, if you have property of type `string` that actually contains numeric input, you could apply a transformation to convert the string value to a number.


Expand All @@ -23,17 +28,4 @@ int? StringToNullableInt(string value)

This syntax is available in FluentValidation 9.5 and newer.

There is also a `TransformForEach` method available, which performs the transformation against each item in a collection.


## Transforming Values (9.0 - 9.4)

Prior to FluentValidation 9.5, you can use the `Transform` method after a call to `RuleFor` to achieve the same result.

```csharp
RuleFor(x => x.SomeStringProperty)
.Transform(value => int.TryParse(value, out int val) ? (int?) val : null)
.GreaterThan(10);
```

This `Transform` method is marked as obsolete as of FluentValidation 9.5 and is removed in FluentValidation 10.0. In newer versions of FluentValidation the transformation should be applied by calling `Transform` as the first method in the chain (see above).
There is also a `TransformForEach` method available, which performs the transformation against each item in a collection.
2 changes: 2 additions & 0 deletions src/FluentValidation.Tests/TransformTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace FluentValidation.Tests;
using System.Threading.Tasks;
using Xunit;

#pragma warning disable CS0618

public class TransformTests {
[Fact]
public void Transforms_property_value() {
Expand Down
4 changes: 4 additions & 0 deletions src/FluentValidation/AbstractValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ ValueTask<ValidationResult> completedValueTask
/// <param name="from">The expression representing the property to transform</param>
/// <param name="to">Function to transform the property value into a different type</param>
/// <returns>an IRuleBuilder instance on which validators can be defined</returns>
[Obsolete("The Transform method is deprecated and will be removed in FluentValidation 12. We recommend using a computed property on your model instead. For details see https://github.com/FluentValidation/FluentValidation/issues/2072")]
public IRuleBuilderInitial<T, TTransformed> Transform<TProperty, TTransformed>(Expression<Func<T, TProperty>> from, Func<TProperty, TTransformed> to) {
from.Guard("Cannot pass null to Transform", nameof(from));
var rule = PropertyRule<T, TTransformed>.Create(from, to, () => RuleLevelCascadeMode);
Expand All @@ -316,6 +317,7 @@ ValueTask<ValidationResult> completedValueTask
/// <param name="from">The expression representing the property to transform</param>
/// <param name="to">Function to transform the property value into a different type</param>
/// <returns>an IRuleBuilder instance on which validators can be defined</returns>
[Obsolete("The Transform method is deprecated and will be removed in FluentValidation 12. We recommend using a computed property on your model instead. For details see https://github.com/FluentValidation/FluentValidation/issues/2072")]
public IRuleBuilderInitial<T, TTransformed> Transform<TProperty, TTransformed>(Expression<Func<T, TProperty>> from, Func<T, TProperty, TTransformed> to) {
from.Guard("Cannot pass null to Transform", nameof(from));
var rule = PropertyRule<T, TTransformed>.Create(from, to, () => RuleLevelCascadeMode);
Expand Down Expand Up @@ -345,6 +347,7 @@ ValueTask<ValidationResult> completedValueTask
/// <param name="expression">Expression representing the collection to validate</param>
/// <param name="to">Function to transform the collection element into a different type</param>
/// <returns>An IRuleBuilder instance on which validators can be defined</returns>
[Obsolete("The TransformForEach method is deprecated and will be removed in FluentValidation 12. We recommend using a computed property on your model instead. For details see https://github.com/FluentValidation/FluentValidation/issues/2072")]
public IRuleBuilderInitialCollection<T, TTransformed> TransformForEach<TElement, TTransformed>(Expression<Func<T, IEnumerable<TElement>>> expression, Func<TElement, TTransformed> to) {
expression.Guard("Cannot pass null to RuleForEach", nameof(expression));
var rule = CollectionPropertyRule<T, TTransformed>.CreateTransformed(expression, to, () => RuleLevelCascadeMode);
Expand All @@ -360,6 +363,7 @@ ValueTask<ValidationResult> completedValueTask
/// <param name="expression">Expression representing the collection to validate</param>
/// <param name="to">Function to transform the collection element into a different type</param>
/// <returns>An IRuleBuilder instance on which validators can be defined</returns>
[Obsolete("The TransformForEach method is deprecated and will be removed in FluentValidation 12. We recommend using a computed property on your model instead. For details see https://github.com/FluentValidation/FluentValidation/issues/2072")]
public IRuleBuilderInitialCollection<T, TTransformed> TransformForEach<TElement, TTransformed>(Expression<Func<T, IEnumerable<TElement>>> expression, Func<T, TElement, TTransformed> to) {
expression.Guard("Cannot pass null to RuleForEach", nameof(expression));
var rule = CollectionPropertyRule<T, TTransformed>.CreateTransformed(expression, to, () => RuleLevelCascadeMode);
Expand Down

0 comments on commit c6f4476

Please sign in to comment.