-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Added IServiceProvider to DataAnnotationsValidator #39445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SteveSandersonMS
merged 11 commits into
dotnet:main
from
MariovanZeist:mvz/DataAnnotationsValidatorWithDI
Jan 19, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
de650e1
Add IServiceProvider to ValidationContext
MariovanZeist 737bff1
Add e2e test
MariovanZeist 4e8357d
Salad chef to the rescue
MariovanZeist 072bdce
Apply suggestions from code review
pranavkm c18dabb
Update src/Components/test/testassets/BasicTestApp/FormsTest/Validati…
TanayParikh ccebda4
Apply suggestions from code review
pranavkm d866cf0
Apply suggestions from code review
pranavkm 6f59305
Fix nullable warning.
MariovanZeist 912abe4
Updated tests in repsonse of obsoleting EnableDataAnnotationsValidati…
MariovanZeist 65fc71c
Added explicit null check, and Updated tests to reflect this change b…
MariovanZeist 442733e
Fix test not working when running Blazor-Server due to different DI C…
MariovanZeist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 +1,2 @@ | ||
| #nullable enable | ||
| static Microsoft.AspNetCore.Components.Forms.EditContextDataAnnotationsExtensions.EnableDataAnnotationsValidation(this Microsoft.AspNetCore.Components.Forms.EditContext! editContext, System.IServiceProvider! serviceProvider) -> System.IDisposable! |
This file contains hidden or 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 hidden or 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 hidden or 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
44 changes: 44 additions & 0 deletions
44
src/Components/test/testassets/BasicTestApp/FormsTest/ValidationComponentDI.razor
This file contains hidden or 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,44 @@ | ||
| @using System.ComponentModel.DataAnnotations | ||
| @using Microsoft.AspNetCore.Components.Forms | ||
|
|
||
| <EditForm Model="@this" autocomplete="off"> | ||
| <DataAnnotationsValidator /> | ||
|
|
||
| <p class="the-quiz"> | ||
| Name something you can put in a salad: | ||
| <input @bind="SaladIngredient" class="@context.FieldCssClass(() => SaladIngredient)" /> | ||
| </p> | ||
|
|
||
| <button type="submit">Submit</button> | ||
| <ul class="validation-errors"> | ||
| @foreach (var message in context.GetValidationMessages()) | ||
| { | ||
| <li class="validation-message">@message</li> | ||
| } | ||
| </ul> | ||
|
|
||
| </EditForm> | ||
|
|
||
| @code { | ||
| [SaladChefValidator] | ||
| public string SaladIngredient { get; set; } | ||
|
|
||
| public class SaladChefValidatorAttribute : ValidationAttribute | ||
| { | ||
| protected override ValidationResult IsValid(object value, ValidationContext validationContext) | ||
| { | ||
| var saladChef = validationContext.GetRequiredService<SaladChef>(); | ||
| if (saladChef.ThingsYouCanPutInASalad.Contains(value.ToString())) | ||
| { | ||
| return ValidationResult.Success; | ||
| } | ||
| return new ValidationResult("You should not put that in a salad!"); | ||
| } | ||
| } | ||
|
|
||
| // Simple class to check if DI can be used in Validation attributes | ||
| public class SaladChef | ||
| { | ||
| public string[] ThingsYouCanPutInASalad = { "Strawberries", "Pineapple", "Honeydew", "Watermelon", "Grapes" }; | ||
|
pranavkm marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm in two minds about the nullability annotation here. I know the only way you can make it
nullis to use an obsolete API, but existing code may do that, and then newer code will be getting told a lie by the annotation.Should we keep it as
IServiceProvider?until the obsoleted API is actually removed? Is there prior art for how we handle this scenario elsewhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see that @pranavkm just suggested marking it as non-nullable, so perhaps that is an established pattern about obsoletion already. Just waiting for a comment from him to confirm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is a private property and our implementation always expects injected properties to be available. So annotating it as non-nullable seems correct. Does that sound right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're quite right. Sorry for the confusion!