47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
using Microsoft.Extensions.Options;
|
|
|
|
namespace WebAccess.MCP.Search;
|
|
|
|
internal sealed class SearXngOptionsValidator : IValidateOptions<SearXngOptions>
|
|
{
|
|
public ValidateOptionsResult Validate(string? name, SearXngOptions options)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(options.BaseUrl))
|
|
{
|
|
return ValidateOptionsResult.Fail(
|
|
$"{SearXngOptions.SectionName}:BaseUrl is required when search is used.");
|
|
}
|
|
|
|
if (!Uri.TryCreate(options.BaseUrl, UriKind.Absolute, out var baseUri))
|
|
{
|
|
return ValidateOptionsResult.Fail(
|
|
$"{SearXngOptions.SectionName}:BaseUrl must be an absolute URI.");
|
|
}
|
|
|
|
if (baseUri.Scheme != Uri.UriSchemeHttp && baseUri.Scheme != Uri.UriSchemeHttps)
|
|
{
|
|
return ValidateOptionsResult.Fail(
|
|
$"{SearXngOptions.SectionName}:BaseUrl must use the HTTP or HTTPS scheme; '{baseUri.Scheme}' is unsupported.");
|
|
}
|
|
|
|
if (options.Timeout <= TimeSpan.Zero)
|
|
{
|
|
return ValidateOptionsResult.Fail(
|
|
$"{SearXngOptions.SectionName}:Timeout must be greater than zero.");
|
|
}
|
|
|
|
try
|
|
{
|
|
SearXngEngineSelectors.Normalize(
|
|
options.DefaultEngines ?? [],
|
|
$"{SearXngOptions.SectionName}:DefaultEngines");
|
|
}
|
|
catch (ArgumentException exception)
|
|
{
|
|
return ValidateOptionsResult.Fail(exception.Message);
|
|
}
|
|
|
|
return ValidateOptionsResult.Success;
|
|
}
|
|
}
|