forked from emersonsoares/FluentValidation.Net35
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tag 9.0.0
- Loading branch information
Showing
83 changed files
with
848 additions
and
451 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
blank_issues_enabled: false |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
name: Feature request | ||
about: Suggest an idea for this project | ||
title: '' | ||
labels: '' | ||
assignees: '' | ||
|
||
--- | ||
|
||
**Is your feature request related to a problem? Please describe.** | ||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] | ||
|
||
**Describe the solution you'd like** | ||
A clear and concise description of what you want to happen. | ||
|
||
**Describe alternatives you've considered** | ||
A clear and concise description of any alternative solutions or features you've considered. | ||
|
||
**Additional context** | ||
Add any other context or sample code about the feature request here. |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
name: Support Request | ||
about: Issues, bug reports and general support requests | ||
title: '' | ||
labels: '' | ||
assignees: '' | ||
|
||
--- | ||
<!-- | ||
Please check the documentation at https://fluentvalidation.net first to see if your question is addressed there. | ||
If not, please fill in the details below. | ||
--> | ||
|
||
**Which version of FluentValidation are you using?** | ||
Please specify which version of FluentValidation you're using. | ||
|
||
**Which version of ASP.NET are you using?** | ||
If you are using FluentValidation with ASP.NET, please provide the version of ASP.NET that you're using (eg .NET Core 2.1, NET Core 3.1 etc) | ||
|
||
**Describe the issue that you're having** | ||
A clear and concise description of the issue that you're having, steps to reproduce, and sample code to reproduce the issue. |
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Custom State | ||
|
||
There may be an occasion where you'd like to return contextual information about the state of your validation rule when it was run. The `WithCustomState` method allows you to associate any custom data with the validation results. | ||
|
||
We could assign a custom state by modifying a line to read: | ||
|
||
```csharp | ||
public class PersonValidator : AbstractValidator<Person> { | ||
public PersonValidator() { | ||
RuleFor(person => person.Surname).NotNull(); | ||
RuleFor(person => person.Forename).NotNull().WithState(person => 1234); | ||
} | ||
} | ||
``` | ||
|
||
This state is then available within the `CustomState` property of the `ValidationFailure`. | ||
|
||
```csharp | ||
var validator = new PersonValidator(); | ||
var result = validator.Validate(new Person()); | ||
foreach (var failure in result.Errors) { | ||
Console.WriteLine($"Property: {failure.PropertyName} State: {failure.CustomState}"); | ||
} | ||
``` | ||
|
||
The output would be: | ||
|
||
``` | ||
Property: Surname State: | ||
Property: Forename State: 1234 | ||
``` | ||
|
||
By default the `CustomState` property will be `null` if `WithState` hasn't been called. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Custom Error Codes | ||
|
||
A custom error code can also be associated with validation rules by calling the `WithErrorCode` method: | ||
|
||
```csharp | ||
public class PersonValidator : AbstractValidator<Person> { | ||
public PersonValidator() { | ||
RuleFor(person => person.Surname).NotNull().WithErrorCode("ERR1234"); | ||
RuleFor(person => person.Forename).NotNull(); | ||
} | ||
} | ||
``` | ||
|
||
The resulting error code can be obtained from the `ErrorCode` property on the `ValidationFailure`: | ||
|
||
```csharp | ||
var validator = new PersonValidator(); | ||
var result = validator.Validate(new Person()); | ||
foreach (var failure in result.Errors) { | ||
Console.WriteLine($"Property: {failure.PropertyName} Error Code: {failure.ErrorCode}"); | ||
} | ||
``` | ||
|
||
The output would be: | ||
|
||
``` | ||
Property: Surname Error Code: ERR1234 | ||
Property: Forename Error Code: NotNullValidator | ||
``` | ||
|
||
## ErrorCode and Error Messages | ||
|
||
The `ErrorCode` is also used to determine the default error message for a particular validator. At a high level: | ||
|
||
* The error code is used as the lookup key for an error message. For example, a `NotNull()` validator has a default error code of `NotNullValidator`, which used to look up the error messages from the `LanguageManager`. [See the documentation on localization.](localization) | ||
* If you provide an error code, you could also provide a localized message with the name of that error code to create a custom message. | ||
* If you provide an error code but no custom message, the message will fall back to the default message for that validator. You're not required to add a custom message. | ||
* Using `ErrorCode` can also be used to override the default error message. For example, if you use a custom `Must()` validator, but you'd like to reuse the `NotNull()` validator's default error message, you can call `WithErrorCode("NotNullValidator")` to achieve this result. |
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
Oops, something went wrong.