39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
using Microsoft.Extensions.Options;
|
|
using Mirea.Api.Endpoint.Common.Settings;
|
|
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;
|
|
}
|
|
} |