2024-12-09 21:11:46 +01:00
|
|
|
// Copyright (C) 2023-present sam/u1f320 (vulpine.solutions)
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
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>>();
|
2024-12-08 15:07:25 +01:00
|
|
|
foreach ((string, ValidationError?) error in errors)
|
2024-07-12 17:12:24 +02:00
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
if (errorDict.TryGetValue(error.Item1, out IEnumerable<ValidationError>? value))
|
2024-10-02 00:28:07 +02:00
|
|
|
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
|
|
|
}
|