Add Application configuration #11
@ -0,0 +1,38 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Mirea.Api.Endpoint.Configuration.General.Attributes;
|
||||
using Mirea.Api.Endpoint.Configuration.General.Interfaces;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Mirea.Api.Endpoint.Configuration.General.Validators;
|
||||
|
||||
public class SettingsRequiredValidator
|
||||
{
|
||||
private readonly GeneralConfig _generalConfig;
|
||||
|
||||
public SettingsRequiredValidator(IOptionsSnapshot<GeneralConfig> configuration) =>
|
||||
_generalConfig = configuration.Value;
|
||||
|
||||
public SettingsRequiredValidator(GeneralConfig configuration) =>
|
||||
_generalConfig = configuration;
|
||||
|
||||
public bool AreSettingsValid()
|
||||
{
|
||||
foreach (var property in _generalConfig
|
||||
.GetType()
|
||||
.GetProperties(BindingFlags.Public | BindingFlags.Instance))
|
||||
{
|
||||
if (!Attribute.IsDefined(property.PropertyType, typeof(RequiredSettingsAttribute))) continue;
|
||||
|
||||
var value = property.GetValue(_generalConfig);
|
||||
if (value == null)
|
||||
return false;
|
||||
|
||||
var isConfigured = value as IIsConfigured;
|
||||
if (!isConfigured!.IsConfigured())
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user