This repository was archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Add DataAnnotations based validation #272
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
be3630d
Add DataAnnotations based validation
HaoK 991099d
Move data annotations into its own package
HaoK b7bccab
Update doc comments
HaoK 44bf9e5
CR tweaks
HaoK 80c3dba
Tweak data annotation failure message
HaoK 7b41ce5
Cleanup baseline
HaoK 4f7c044
Add multi field validator test
HaoK 48aab0c
Space
HaoK 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
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
61 changes: 61 additions & 0 deletions
61
src/Microsoft.Extensions.Options.DataAnnotations/DataAnnotationValidateOptions.cs
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,61 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Extensions.Options | ||
{ | ||
/// <summary> | ||
/// Implementation of <see cref="IValidateOptions{TOptions}"/> that uses DataAnnotation's <see cref="Validator"/> for validation. | ||
/// </summary> | ||
/// <typeparam name="TOptions">The instance being validated.</typeparam> | ||
public class DataAnnotationValidateOptions<TOptions> : IValidateOptions<TOptions> where TOptions : class | ||
{ | ||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
/// <param name="name"></param> | ||
public DataAnnotationValidateOptions(string name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need doc comments on all public APIs. |
||
{ | ||
Name = name; | ||
} | ||
|
||
/// <summary> | ||
/// The options name. | ||
/// </summary> | ||
public string Name { get; } | ||
|
||
/// <summary> | ||
/// Validates a specific named options instance (or all when name is null). | ||
/// </summary> | ||
/// <param name="name">The name of the options instance being validated.</param> | ||
/// <param name="options">The options instance.</param> | ||
/// <returns>The <see cref="ValidateOptionsResult"/> result.</returns> | ||
public ValidateOptionsResult Validate(string name, TOptions options) | ||
{ | ||
// Null name is used to configure all named options. | ||
if (Name == null || name == Name) | ||
{ | ||
var validationResults = new List<ValidationResult>(); | ||
if (Validator.TryValidateObject(options, | ||
new ValidationContext(options, serviceProvider: null, items: null), | ||
validationResults, | ||
validateAllProperties: true)) | ||
{ | ||
return ValidateOptionsResult.Success; | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. OK last nit, a space after the comma 😄 |
||
" with the error '" + r.ErrorMessage + "'."))); | ||
} | ||
|
||
// Ignored if not validating this instance. | ||
return ValidateOptionsResult.Skip; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ft.Extensions.Options.DataAnnotations/Microsoft.Extensions.Options.DataAnnotations.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>Provides additional DataAnnotations specific functionality related to Options.</Description> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<NoWarn>$(NoWarn)</NoWarn> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<PackageTags>aspnetcore;validation;options</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Microsoft.Extensions.Options\Microsoft.Extensions.Options.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsDependencyInjectionAbstractionsPackageVersion)" /> | ||
<PackageReference Include="System.ComponentModel.Annotations" Version="$(SystemComponentModelAnnotationsPackageVersion)" /> | ||
</ItemGroup> | ||
|
||
</Project> |
25 changes: 25 additions & 0 deletions
25
src/Microsoft.Extensions.Options.DataAnnotations/OptionsBuilderDataAnnotationsExtensions.cs
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,25 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
/// <summary> | ||
/// Extension methods for adding configuration related options services to the DI container via <see cref="OptionsBuilder{TOptions}"/>. | ||
/// </summary> | ||
public static class OptionsBuilderDataAnnotationsExtensions | ||
{ | ||
/// <summary> | ||
/// Register this options instance for validation of its DataAnnotations. | ||
/// </summary> | ||
/// <typeparam name="TOptions">The options type to be configured.</typeparam> | ||
/// <param name="optionsBuilder">The options builder to add the services to.</param> | ||
/// <returns>The <see cref="OptionsBuilder{TOptions}"/> so that additional calls can be chained.</returns> | ||
public static OptionsBuilder<TOptions> ValidateDataAnnotations<TOptions>(this OptionsBuilder<TOptions> optionsBuilder) where TOptions : class | ||
{ | ||
optionsBuilder.Services.AddSingleton<IValidateOptions<TOptions>>(new DataAnnotationValidateOptions<TOptions>(optionsBuilder.Name)); | ||
return optionsBuilder; | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/Microsoft.Extensions.Options.DataAnnotations/baseline.netcore.json
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,2 @@ | ||
{ | ||
} |
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
Oops, something went wrong.
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.
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!