-
Notifications
You must be signed in to change notification settings - Fork 741
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DataAnnotations based validation (#272)
- Loading branch information
Showing
21 changed files
with
551 additions
and
18 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
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
61 changes: 61 additions & 0 deletions
61
src/Microsoft.Extensions.Options.DataAnnotations/DataAnnotationValidateOptions.cs
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,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) | ||
{ | ||
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) + | ||
" 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 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 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 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 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.