dramaling-vocab-learning/backend/DramaLing.Api/Models/Configuration/OptionsVocabularyOptionsVal...

62 lines
2.4 KiB
C#

using Microsoft.Extensions.Options;
namespace DramaLing.Api.Models.Configuration;
/// <summary>
/// OptionsVocabularyOptions 配置驗證器
/// </summary>
public class OptionsVocabularyOptionsValidator : IValidateOptions<OptionsVocabularyOptions>
{
public ValidateOptionsResult Validate(string? name, OptionsVocabularyOptions options)
{
var errors = new List<string>();
// 驗證快取過期時間
if (options.CacheExpirationMinutes < 1 || options.CacheExpirationMinutes > 60)
{
errors.Add("CacheExpirationMinutes must be between 1 and 60 minutes");
}
// 驗證最小詞彙庫門檻
if (options.MinimumVocabularyThreshold < 1 || options.MinimumVocabularyThreshold > 100)
{
errors.Add("MinimumVocabularyThreshold must be between 1 and 100");
}
// 驗證詞彙長度差異範圍
if (options.WordLengthTolerance < 0 || options.WordLengthTolerance > 10)
{
errors.Add("WordLengthTolerance must be between 0 and 10");
}
// 驗證快取大小限制
if (options.CacheSizeLimit < 10 || options.CacheSizeLimit > 1000)
{
errors.Add("CacheSizeLimit must be between 10 and 1000");
}
// 驗證快取預熱組合
if (options.PrewarmCombinations != null)
{
var validCEFRLevels = new[] { "A1", "A2", "B1", "B2", "C1", "C2" };
var validPartsOfSpeech = new[] { "noun", "verb", "adjective", "adverb", "pronoun", "preposition", "conjunction", "interjection", "idiom" };
foreach (var combination in options.PrewarmCombinations)
{
if (string.IsNullOrEmpty(combination.CEFRLevel) || !validCEFRLevels.Contains(combination.CEFRLevel))
{
errors.Add($"Invalid CEFR level in prewarm combination: {combination.CEFRLevel}");
}
if (string.IsNullOrEmpty(combination.PartOfSpeech) || !validPartsOfSpeech.Contains(combination.PartOfSpeech))
{
errors.Add($"Invalid part of speech in prewarm combination: {combination.PartOfSpeech}");
}
}
}
return errors.Any()
? ValidateOptionsResult.Fail(errors)
: ValidateOptionsResult.Success;
}
}