2024-07-12 17:12:24 +02:00
|
|
|
namespace Foxnouns.Backend.Utils;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Static methods for validating user input (mostly making sure it's not too short or too long)
|
|
|
|
/// </summary>
|
2024-10-01 16:04:36 +02:00
|
|
|
public static partial class ValidationUtils
|
2024-07-12 17:12:24 +02:00
|
|
|
{
|
2024-07-14 16:44:41 +02:00
|
|
|
public static void Validate(IEnumerable<(string, ValidationError?)> errors)
|
2024-07-12 17:12:24 +02:00
|
|
|
{
|
2024-07-14 16:44:41 +02:00
|
|
|
errors = errors.Where(e => e.Item2 != null).ToList();
|
2024-10-02 00:28:07 +02:00
|
|
|
if (!errors.Any())
|
|
|
|
return;
|
2024-07-14 16:44:41 +02:00
|
|
|
|
|
|
|
var errorDict = new Dictionary<string, IEnumerable<ValidationError>>();
|
|
|
|
foreach (var error in errors)
|
2024-07-12 17:12:24 +02:00
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
if (errorDict.TryGetValue(error.Item1, out var value))
|
|
|
|
errorDict[error.Item1] = value.Append(error.Item2!);
|
2024-07-14 16:44:41 +02:00
|
|
|
errorDict.Add(error.Item1, [error.Item2!]);
|
2024-07-12 17:12:24 +02:00
|
|
|
}
|
2024-07-14 16:44:41 +02:00
|
|
|
|
|
|
|
throw new ApiError.BadRequest("Error validating input", errorDict);
|
2024-07-12 17:12:24 +02:00
|
|
|
}
|
2024-10-02 00:28:07 +02:00
|
|
|
}
|