using Microsoft.Extensions.Options; using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Attributes; using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Interfaces; using Mirea.Api.Endpoint.Configuration.Model; using System; using System.Reflection; namespace Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Validators; public class SettingsRequiredValidator { private readonly GeneralConfig _generalConfig; public SettingsRequiredValidator(IOptionsSnapshot 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; } }