namespace ZB.MOM.WW.ScadaBridge.Commons.Validation;
///
/// Validation for site gRPC endpoint addresses (Site.GrpcNodeAAddress/GrpcNodeBAddress).
/// A bare host or host:port is rejected because the gRPC channel needs an absolute http(s) URI.
///
public static class GrpcEndpointRules
{
///
/// Validates a gRPC endpoint. Null/whitespace is "not configured": valid only when
/// is true.
///
public static bool TryValidate(string? value, bool allowEmpty, out string? error)
{
error = null;
if (string.IsNullOrWhiteSpace(value))
{
if (allowEmpty)
{
return true;
}
error = "gRPC endpoint is required (e.g. http://host:8083).";
return false;
}
if (!Uri.TryCreate(value.Trim(), UriKind.Absolute, out var uri)
|| (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)
|| string.IsNullOrEmpty(uri.Host))
{
error = $"gRPC endpoint '{value}' must be an absolute http:// or https:// URL with a host (e.g. http://host:8083).";
return false;
}
return true;
}
}