-
Notifications
You must be signed in to change notification settings - Fork 68
Conversation
We can split this out into a separate Options.DataAnnotations package if we still care about keeping dependencies of the core options package lean... |
Code seems good, but not sure about taking a dependency on DataAnnotations--we probably shouldn't do it now unless we want to keep it in 3.0. But if other things already have the dependency and will continue to have it in 3.0, then their probably won't be a problem. |
No not in core options. This should be another package reference unlesss we make it part of net standard |
+1 for new package layer |
|
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.
Code looks pretty straightforward and simple. Just some doc comment comments!
namespace Microsoft.Extensions.Options | ||
{ | ||
/// <summary> | ||
/// Implementation of <see cref="IValidateOptions{TOptions}"/> |
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.
Can maybe elaborate on this a bit more? This comment is just stating what can be seen from the type declaration anyway.
/// <typeparam name="TOptions">The instance being validated.</typeparam> | ||
public class DataAnnotationValidateOptions<TOptions> : IValidateOptions<TOptions> where TOptions : class | ||
{ | ||
public DataAnnotationValidateOptions(string name) |
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.
Need doc comments on all public APIs.
public static class OptionsBuilderDataAnnotationsExtensions | ||
{ | ||
/// <summary> | ||
/// Register this options instance for the built in DataAnnotationsAttribute |
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.
The term DataAnnotationsAttribute
looks like it's being used as a type name, but it isn't a type. Rephrase somehow?
Fixed new doc comments and removed the doc comment excludes so added all the missing doc comments as well |
} | ||
|
||
|
||
var errors = String.Join(" : ", validationResults.Select(r => r.ErrorMessage)); |
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 is a bit icky. I'm guessing there's no "list of errors" that we can send a list of these failures to? How about put one on each line instead?
"Validation of the object XYZ failed:
{reason 1}
{reason 2}
{reason 3}"
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 think it would be also useful here to use ValidationResult.MemberNames and format that in some way so that the user knows which property(s) failed.
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.
Okay I'll try to clean this up, yeah the data annotation validations are treated as a single validaiton failure along side an arbitrary amount of other validators, which is why it gets turned into a single string, since it potentially would need to be aggregated with other non data-annotation based validation errors.
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.
Yeah I think at least putting 1 per line would be a good separation.
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.
Specifically only for data annotations errors? or all of the aggregated errors as well? I'll add a new test that demonstrates what it looks like so its easier to review in the PR
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.
Yeah let's see it in the test then I'll review.
@@ -3,7 +3,7 @@ | |||
<PropertyGroup> | |||
<Description>Provides additional configuration specific functionality related to Options.</Description> | |||
<TargetFramework>netstandard2.0</TargetFramework> | |||
<NoWarn>$(NoWarn);CS1591</NoWarn> |
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.
YES! Another project finally has full docs!
Updated the new errors are more wordy now but hopefully more clear:
|
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.
Just a few small comments, then looks good to me.
@@ -49,7 +49,10 @@ public ValidateOptionsResult Validate(string name, TOptions options) | |||
} | |||
|
|||
|
|||
var errors = String.Join(" : ", validationResults.Select(r => r.ErrorMessage)); | |||
var errors = String.Join(Environment.NewLine, | |||
validationResults.Select(r => "DataAnnotation validation failed for members {" + |
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.
Maybe get rid of the {
? A bit too nerdy 😄
var errors = String.Join(Environment.NewLine, | ||
validationResults.Select(r => "DataAnnotation validation failed for members {" + | ||
String.Join(",", r.MemberNames) + | ||
"} with error '" + r.ErrorMessage + "'.")); |
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.
with *the* error
DataAnnotation validation failed for members {IntRange} with error 'Out of range.'. | ||
DataAnnotation validation failed for members {Custom} with error 'The field Custom is invalid.'.", | ||
"I don't want to go to nowhere!"); | ||
} |
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.
Could we add a case where there is more than 1 member as well? Maybe CompareValidator does that? Or write a dummy custom validator that does that?
Just as FYI, multi member error looks like: DataAnnotation validation failed for members Dep1,Dep2 with the error 'Dep1 != Dep2'. |
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.
OK literally just asking for 1 more space, then good to go as far as I'm concerned. THanks!
return ValidateOptionsResult.Fail(errors); | ||
return ValidateOptionsResult.Fail(String.Join(Environment.NewLine, | ||
validationResults.Select(r => "DataAnnotation validation failed for members " + | ||
String.Join(",", r.MemberNames) + |
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.
OK last nit, a space after the comma 😄
Add DataAnnotations validator support: (dotnet/aspnetcore#3385)
Basic support works pretty much as expected:
Basically you get a single error message with all of the data validation errors smushed together, since its considered a single options validator.
Thoughts? @ajcvickers @davidfowl @Eilon @DamianEdwards