refactor(backend): use explicit types instead of var by default

This commit is contained in:
sam 2024-12-08 15:07:25 +01:00
parent bc7fd6d804
commit 649988db25
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
52 changed files with 506 additions and 420 deletions

View file

@ -69,8 +69,8 @@ public static class AuthUtils
public static bool ValidateScopes(Application application, string[] scopes)
{
var expandedScopes = scopes.ExpandScopes();
var appScopes = application.Scopes.ExpandAppScopes();
string[] expandedScopes = scopes.ExpandScopes();
string[] appScopes = application.Scopes.ExpandAppScopes();
return !expandedScopes.Except(appScopes).Any();
}
@ -78,7 +78,7 @@ public static class AuthUtils
{
try
{
var scheme = new Uri(uri).Scheme;
string scheme = new Uri(uri).Scheme;
return !ForbiddenSchemes.Contains(scheme);
}
catch

View file

@ -30,7 +30,7 @@ public class PatchRequestContractResolver : DefaultContractResolver
MemberSerialization memberSerialization
)
{
var prop = base.CreateProperty(member, memberSerialization);
JsonProperty prop = base.CreateProperty(member, memberSerialization);
prop.SetIsSpecified += (o, _) =>
{

View file

@ -39,6 +39,7 @@ public static partial class ValidationUtils
var errors = new List<(string, ValidationError?)>();
if (fields.Count > 25)
{
errors.Add(
(
"fields",
@ -50,11 +51,13 @@ public static partial class ValidationUtils
)
)
);
}
// No overwhelming this function, thank you
if (fields.Count > 100)
return errors;
foreach (var (field, index) in fields.Select((field, index) => (field, index)))
foreach ((Field? field, int index) in fields.Select((field, index) => (field, index)))
{
switch (field.Name.Length)
{
@ -111,6 +114,7 @@ public static partial class ValidationUtils
var errors = new List<(string, ValidationError?)>();
if (entries.Length > Limits.FieldEntriesLimit)
{
errors.Add(
(
errorPrefix,
@ -122,15 +126,19 @@ public static partial class ValidationUtils
)
)
);
}
// Same as above, no overwhelming this function with a ridiculous amount of entries
if (entries.Length > Limits.FieldEntriesLimit + 50)
return errors;
var customPreferenceIds =
customPreferences?.Keys.Select(id => id.ToString()).ToArray() ?? [];
string[] customPreferenceIds = customPreferences.Keys.Select(id => id.ToString()).ToArray();
foreach (var (entry, entryIdx) in entries.Select((entry, entryIdx) => (entry, entryIdx)))
foreach (
(FieldEntry? entry, int entryIdx) in entries.Select(
(entry, entryIdx) => (entry, entryIdx)
)
)
{
switch (entry.Value.Length)
{
@ -166,12 +174,14 @@ public static partial class ValidationUtils
!DefaultStatusOptions.Contains(entry.Status)
&& !customPreferenceIds.Contains(entry.Status)
)
{
errors.Add(
(
$"{errorPrefix}.{entryIdx}.status",
ValidationError.GenericValidationError("Invalid status", entry.Status)
)
);
}
}
return errors;
@ -188,6 +198,7 @@ public static partial class ValidationUtils
var errors = new List<(string, ValidationError?)>();
if (entries.Length > Limits.FieldEntriesLimit)
{
errors.Add(
(
errorPrefix,
@ -199,15 +210,17 @@ public static partial class ValidationUtils
)
)
);
}
// Same as above, no overwhelming this function with a ridiculous amount of entries
if (entries.Length > Limits.FieldEntriesLimit + 50)
return errors;
var customPreferenceIds =
customPreferences?.Keys.Select(id => id.ToString()).ToList() ?? [];
string[] customPreferenceIds = customPreferences.Keys.Select(id => id.ToString()).ToArray();
foreach (var (entry, entryIdx) in entries.Select((entry, entryIdx) => (entry, entryIdx)))
foreach (
(Pronoun? entry, int entryIdx) in entries.Select((entry, entryIdx) => (entry, entryIdx))
)
{
switch (entry.Value.Length)
{
@ -276,12 +289,14 @@ public static partial class ValidationUtils
!DefaultStatusOptions.Contains(entry.Status)
&& !customPreferenceIds.Contains(entry.Status)
)
{
errors.Add(
(
$"{errorPrefix}.{entryIdx}.status",
ValidationError.GenericValidationError("Invalid status", entry.Status)
)
);
}
}
return errors;

View file

@ -29,6 +29,7 @@ public static partial class ValidationUtils
var errors = new List<(string, ValidationError?)>();
if (preferences.Count > MaxCustomPreferences)
{
errors.Add(
(
"custom_preferences",
@ -40,20 +41,29 @@ public static partial class ValidationUtils
)
)
);
}
if (preferences.Count > 50)
return errors;
foreach (var (p, i) in preferences.Select((p, i) => (p, i)))
foreach (
(UsersController.CustomPreferenceUpdate? p, int i) in preferences.Select(
(p, i) => (p, i)
)
)
{
if (!BootstrapIcons.IsValid(p.Icon))
{
errors.Add(
(
$"custom_preferences.{i}.icon",
ValidationError.DisallowedValueError("Invalid icon name", [], p.Icon)
)
);
}
if (p.Tooltip.Length is 1 or > MaxPreferenceTooltipLength)
{
errors.Add(
(
$"custom_preferences.{i}.tooltip",
@ -65,6 +75,7 @@ public static partial class ValidationUtils
)
)
);
}
}
return errors;

View file

@ -46,6 +46,7 @@ public static partial class ValidationUtils
public static ValidationError? ValidateUsername(string username)
{
if (!UsernameRegex().IsMatch(username))
{
return username.Length switch
{
< 2 => ValidationError.LengthError("Username is too short", 2, 40, username.Length),
@ -55,6 +56,7 @@ public static partial class ValidationUtils
username
),
};
}
if (
InvalidUsernames.Any(u =>
@ -68,6 +70,7 @@ public static partial class ValidationUtils
public static ValidationError? ValidateMemberName(string memberName)
{
if (!MemberRegex().IsMatch(memberName))
{
return memberName.Length switch
{
< 1 => ValidationError.LengthError("Name is too short", 1, 100, memberName.Length),
@ -79,6 +82,7 @@ public static partial class ValidationUtils
memberName
),
};
}
if (
InvalidMemberNames.Any(u =>
@ -117,13 +121,15 @@ public static partial class ValidationUtils
if (links == null)
return [];
if (links.Length > MaxLinks)
{
return
[
("links", ValidationError.LengthError("Too many links", 0, MaxLinks, links.Length)),
];
}
var errors = new List<(string, ValidationError?)>();
foreach (var (link, idx) in links.Select((l, i) => (l, i)))
foreach ((string link, int idx) in links.Select((l, i) => (l, i)))
{
switch (link.Length)
{

View file

@ -12,9 +12,9 @@ public static partial class ValidationUtils
return;
var errorDict = new Dictionary<string, IEnumerable<ValidationError>>();
foreach (var error in errors)
foreach ((string, ValidationError?) error in errors)
{
if (errorDict.TryGetValue(error.Item1, out var value))
if (errorDict.TryGetValue(error.Item1, out IEnumerable<ValidationError>? value))
errorDict[error.Item1] = value.Append(error.Item2!);
errorDict.Add(error.Item1, [error.Item2!]);
}