feat(backend): improve bad request errors
This commit is contained in:
parent
e7ec0e6661
commit
fb34464199
6 changed files with 114 additions and 45 deletions
|
@ -23,54 +23,65 @@ public static class ValidationUtils
|
|||
"pronouns.cc",
|
||||
"pronounscc"
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Validates whether a username is valid. If it is not valid, throws <see cref="Foxnouns.Backend.ApiError" />.
|
||||
/// This does not check if the username is already taken.
|
||||
/// </summary>
|
||||
public static void ValidateUsername(string username)
|
||||
|
||||
public static ValidationError? ValidateUsername(string username)
|
||||
{
|
||||
if (!UsernameRegex.IsMatch(username))
|
||||
throw username.Length switch
|
||||
return username.Length switch
|
||||
{
|
||||
< 2 => new ApiError.BadRequest("Username is too short", "username"),
|
||||
> 40 => new ApiError.BadRequest("Username is too long", "username"),
|
||||
_ => new ApiError.BadRequest(
|
||||
"Username is invalid, can only contain alphanumeric characters, dashes, underscores, and periods",
|
||||
"username")
|
||||
< 2 => ValidationError.LengthError("Username is too short", 2, 40, username.Length),
|
||||
> 40 => ValidationError.LengthError("Username is too long", 2, 40, username.Length),
|
||||
_ => ValidationError.GenericValidationError(
|
||||
"Username is invalid, can only contain alphanumeric characters, dashes, underscores, and periods", username)
|
||||
};
|
||||
|
||||
if (InvalidUsernames.Any(u => string.Equals(u, username, StringComparison.InvariantCultureIgnoreCase)))
|
||||
throw new ApiError.BadRequest("Username is not allowed", "username");
|
||||
return ValidationError.GenericValidationError("Username is not allowed", username);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void ValidateDisplayName(string? displayName)
|
||||
public static void Validate(IEnumerable<(string, ValidationError?)> errors)
|
||||
{
|
||||
if (displayName == null) return;
|
||||
switch (displayName.Length)
|
||||
errors = errors.Where(e => e.Item2 != null).ToList();
|
||||
if (!errors.Any()) return;
|
||||
|
||||
var errorDict = new Dictionary<string, IEnumerable<ValidationError>>();
|
||||
foreach (var error in errors)
|
||||
{
|
||||
case 0:
|
||||
throw new ApiError.BadRequest("Display name is too short", "display_name");
|
||||
case > 100:
|
||||
throw new ApiError.BadRequest("Display name is too long", "display_name");
|
||||
if (errorDict.TryGetValue(error.Item1, out var value)) errorDict[error.Item1] = value.Append(error.Item2!);
|
||||
errorDict.Add(error.Item1, [error.Item2!]);
|
||||
}
|
||||
|
||||
throw new ApiError.BadRequest("Error validating input", errorDict);
|
||||
}
|
||||
|
||||
public static void ValidateBio(string? bio)
|
||||
public static ValidationError? ValidateDisplayName(string? displayName)
|
||||
{
|
||||
if (bio == null) return;
|
||||
switch (bio.Length)
|
||||
return displayName?.Length switch
|
||||
{
|
||||
case 0:
|
||||
throw new ApiError.BadRequest("Bio is too short", "bio");
|
||||
case > 1024:
|
||||
throw new ApiError.BadRequest("Bio is too long", "bio");
|
||||
}
|
||||
0 => ValidationError.LengthError("Display name is too short", 1, 100, displayName.Length),
|
||||
> 100 => ValidationError.LengthError("Display name is too long", 1, 100, displayName.Length),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
public static void ValidateAvatar(string? avatar)
|
||||
public static ValidationError? ValidateBio(string? bio)
|
||||
{
|
||||
if (avatar == null) return;
|
||||
if (avatar.Length > 1_500_000) throw new ApiError.BadRequest("Avatar is too big", "avatar");
|
||||
return bio?.Length switch
|
||||
{
|
||||
0 => ValidationError.LengthError("Bio is too short", 1, 1024, bio.Length),
|
||||
> 1024 => ValidationError.LengthError("Bio is too long", 1, 1024, bio.Length),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
public static ValidationError? ValidateAvatar(string? avatar)
|
||||
{
|
||||
return avatar?.Length switch
|
||||
{
|
||||
0 => ValidationError.GenericValidationError("Avatar cannot be empty", null),
|
||||
> 1_500_00 => ValidationError.GenericValidationError("Avatar is too large", null),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue