-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6042b4f
commit 39a9b5b
Showing
7 changed files
with
186 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,39 @@ | ||
# Transforming Values | ||
|
||
As of FluentValidation 9.0, you can use the `Transform` method to transform a property value prior to validation being performed against it. For example, if you have property of type `string` that atually contains numeric input, you could use `Transform` to convert the string value to a number. | ||
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. | ||
|
||
|
||
```csharp | ||
RuleFor(x => x.SomeStringProperty) | ||
.Transform(value => int.TryParse(value, out int val) ? (int?) val : null) | ||
Transform(from: x => x.SomeStringProperty, to: value => int.TryParse(value, out int val) ? (int?) val : null) | ||
.GreaterThan(10); | ||
``` | ||
|
||
This rule transforms the value from a `string` to an nullable `int` (returning `null` if the value couldn't be converted). A greater-than check is then performed on the resulting value. | ||
This rule transforms the value from a `string` to an nullable `int` (returning `null` if the value couldn't be converted). A greater-than check is then performed on the resulting value. | ||
|
||
Syntactically this is not particularly nice to read, so this can be cleaned up by using an extension method: | ||
Syntactically this is not particularly nice to read, so the logic for the transformation can optionally be moved into a separate method: | ||
|
||
```csharp | ||
public static class ValidationExtensions { | ||
public static IRuleBuilder<T, int?> TransformToInt<T>(this IRuleBuilderInitial<T, string> ruleBuilder) { | ||
return ruleBuilder.Transform(value => int.TryParse(value, out int val) ? (int?) val : null); | ||
} | ||
} | ||
Transform(x => x.SomeStringProperty, StringToNullableInt) | ||
.GreaterThan(10); | ||
|
||
int? StringToNullableInt(string value) | ||
=> int.TryParse(value, out int val) ? (int?) val : null; | ||
|
||
``` | ||
|
||
The rule can then be written as: | ||
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) | ||
.TransformToInt() | ||
.Transform(value => int.TryParse(value, out int val) ? (int?) val : null) | ||
.GreaterThan(10); | ||
``` | ||
|
||
|
||
```eval_rst | ||
.. note:: | ||
FluentValidation 8.x supported a limited version of the Transform method that could only be used to perform transformations on the same type (e.g., if the property is a string, the result of the transformation must also be a string). FluentValidation 9.0 allows transformations to be performed that change the type. | ||
``` | ||
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters