45 lines
1.6 KiB
C#
45 lines
1.6 KiB
C#
namespace GameList.Endpoints;
|
|
|
|
internal static class SuggestionValidator
|
|
{
|
|
public static async Task<string?> ValidateAsync(SuggestionInput input, IHttpClientFactory httpFactory)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(input.Name) || input.Name.Length > 100)
|
|
return "Name is required and must be <= 100 characters.";
|
|
|
|
if (!EndpointHelpers.IsValidImageUrl(input.ScreenshotUrl))
|
|
return "Screenshot URL must be http(s) and end with an image file extension.";
|
|
|
|
if (!await EndpointHelpers.IsReachableImageAsync(input.ScreenshotUrl, httpFactory))
|
|
return "Screenshot URL could not be validated as an image. Use a public image link (http/https, no redirects, max 5 MB).";
|
|
|
|
if (!EndpointHelpers.IsValidHttpUrl(input.GameUrl))
|
|
return "Game URL must be http or https.";
|
|
|
|
if (!EndpointHelpers.IsValidHttpUrl(input.YoutubeUrl))
|
|
return "YouTube URL must be http or https.";
|
|
|
|
return ValidatePlayers(input.MinPlayers, input.MaxPlayers);
|
|
}
|
|
|
|
private static string? ValidatePlayers(int? minPlayers, int? maxPlayers)
|
|
{
|
|
if (minPlayers is null && maxPlayers is null)
|
|
return null;
|
|
|
|
if (minPlayers is < 1 or > 32)
|
|
return "Min players must be between 1 and 32.";
|
|
|
|
if (maxPlayers is < 1 or > 32)
|
|
return "Max players must be between 1 and 32.";
|
|
|
|
if (minPlayers is null || maxPlayers is null)
|
|
return "Provide both min and max players.";
|
|
|
|
if (minPlayers > maxPlayers)
|
|
return "Min players cannot exceed max players.";
|
|
|
|
return null;
|
|
}
|
|
}
|