37 lines
1.4 KiB
C#
37 lines
1.4 KiB
C#
using Microsoft.Extensions.Options;
|
|
using DramaLing.Api.Models.Configuration;
|
|
|
|
namespace DramaLing.Api.Models.Configuration;
|
|
|
|
public class GeminiOptionsValidator : IValidateOptions<GeminiOptions>
|
|
{
|
|
public ValidateOptionsResult Validate(string? name, GeminiOptions options)
|
|
{
|
|
var failures = new List<string>();
|
|
|
|
if (string.IsNullOrWhiteSpace(options.ApiKey))
|
|
failures.Add("Gemini API key is required");
|
|
|
|
if (options.ApiKey?.StartsWith("AIza") != true)
|
|
failures.Add("Gemini API key format is invalid (should start with 'AIza')");
|
|
|
|
if (options.TimeoutSeconds <= 0 || options.TimeoutSeconds > 120)
|
|
failures.Add("Timeout must be between 1 and 120 seconds");
|
|
|
|
if (options.MaxRetries <= 0 || options.MaxRetries > 10)
|
|
failures.Add("Max retries must be between 1 and 10");
|
|
|
|
if (options.Temperature < 0 || options.Temperature > 2)
|
|
failures.Add("Temperature must be between 0 and 2");
|
|
|
|
if (string.IsNullOrWhiteSpace(options.Model))
|
|
failures.Add("Model name is required");
|
|
|
|
if (string.IsNullOrWhiteSpace(options.BaseUrl) || !Uri.IsWellFormedUriString(options.BaseUrl, UriKind.Absolute))
|
|
failures.Add("Base URL must be a valid absolute URL");
|
|
|
|
return failures.Any()
|
|
? ValidateOptionsResult.Fail(failures)
|
|
: ValidateOptionsResult.Success;
|
|
}
|
|
} |