This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for options validation (#266)
- Loading branch information
Showing
7 changed files
with
368 additions
and
1 deletion.
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,20 @@ | ||
// 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. | ||
|
||
namespace Microsoft.Extensions.Options | ||
{ | ||
/// <summary> | ||
/// Interface used to validate options. | ||
/// </summary> | ||
/// <typeparam name="TOptions">The options type to validate.</typeparam> | ||
public interface IValidateOptions<TOptions> where TOptions : class | ||
{ | ||
/// <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> | ||
ValidateOptionsResult Validate(string name, TOptions options); | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
src/Microsoft.Extensions.Options/OptionsValidationException.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,26 @@ | ||
// 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; | ||
|
||
namespace Microsoft.Extensions.Options | ||
{ | ||
/// <summary> | ||
/// Thrown when options validation fails. | ||
/// </summary> | ||
public class OptionsValidationException : Exception | ||
{ | ||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
/// <param name="failureMessages">The validation failure messages.</param> | ||
public OptionsValidationException(IEnumerable<string> failureMessages) | ||
=> Failures = failureMessages ?? new List<string>(); | ||
|
||
/// <summary> | ||
/// The validation failures. | ||
/// </summary> | ||
public IEnumerable<string> Failures { get; } | ||
} | ||
} |
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,52 @@ | ||
// 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; | ||
|
||
namespace Microsoft.Extensions.Options | ||
{ | ||
/// <summary> | ||
/// Implementation of <see cref="IValidateOptions{TOptions}"/> | ||
/// </summary> | ||
/// <typeparam name="TOptions">The instance being validated.</typeparam> | ||
public class ValidateOptions<TOptions> : IValidateOptions<TOptions> where TOptions : class | ||
{ | ||
public ValidateOptions(string name, Func<TOptions, bool> validation, string failureMessage) | ||
{ | ||
Name = name; | ||
Validation = validation; | ||
FailureMessage = failureMessage; | ||
} | ||
|
||
/// <summary> | ||
/// The options name. | ||
/// </summary> | ||
public string Name { get; } | ||
|
||
/// <summary> | ||
/// The validation action. | ||
/// </summary> | ||
public Func<TOptions, bool> Validation { get; } | ||
|
||
/// <summary> | ||
/// The error to return when validation fails. | ||
/// </summary> | ||
public string FailureMessage { get; } | ||
|
||
public ValidateOptionsResult Validate(string name, TOptions options) | ||
{ | ||
// Null name is used to configure all named options. | ||
if (Name == null || name == Name) | ||
{ | ||
if ((Validation?.Invoke(options)).Value) | ||
{ | ||
return ValidateOptionsResult.Success; | ||
} | ||
return ValidateOptionsResult.Fail(FailureMessage); | ||
} | ||
|
||
// Ignored if not validating this instance. | ||
return ValidateOptionsResult.Skip; | ||
} | ||
} | ||
} |
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,49 @@ | ||
// 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. | ||
|
||
namespace Microsoft.Extensions.Options | ||
{ | ||
/// <summary> | ||
/// Represents the result of an options validation. | ||
/// </summary> | ||
public class ValidateOptionsResult | ||
{ | ||
/// <summary> | ||
/// Result when validation was skipped due to name not matching. | ||
/// </summary> | ||
public static readonly ValidateOptionsResult Skip = new ValidateOptionsResult() { Skipped = true }; | ||
|
||
/// <summary> | ||
/// Validation was successful. | ||
/// </summary> | ||
public static readonly ValidateOptionsResult Success = new ValidateOptionsResult() { Skipped = true }; | ||
|
||
/// <summary> | ||
/// True if validation was successful. | ||
/// </summary> | ||
public bool Succeeded { get; protected set; } | ||
|
||
/// <summary> | ||
/// True if validation was not run. | ||
/// </summary> | ||
public bool Skipped { get; protected set; } | ||
|
||
/// <summary> | ||
/// True if validation failed. | ||
/// </summary> | ||
public bool Failed { get; protected set; } | ||
|
||
/// <summary> | ||
/// Used to describe why validation failed. | ||
/// </summary> | ||
public string FailureMessage { get; protected set; } | ||
|
||
/// <summary> | ||
/// Returns a failure result. | ||
/// </summary> | ||
/// <param name="failureMessage">The reason for the failure.</param> | ||
/// <returns>The failure result.</returns> | ||
public static ValidateOptionsResult Fail(string failureMessage) | ||
=> new ValidateOptionsResult { Failed = true, FailureMessage = failureMessage }; | ||
} | ||
} |
Oops, something went wrong.