Skip to content

Commit

Permalink
chore: Use ThrowIfNull for validating null objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
peombwa committed Oct 10, 2023
1 parent 687f1d3 commit 36189d1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/lib/Helpers/ValidationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ internal static void ValidateNullOrWhitespace(string parameterName, string? valu
throw new ArgumentNullException(parameterName, string.Format(ErrorMessage.FieldIsRequired, parameterName, parentName));
}

internal static void ValidateNullObject(string parameterName, object? value, string parentName)
internal static void ValidateNullObject(string parameterName, object? value)
{
if (value == null)
throw new ArgumentNullException(parameterName, string.Format(ErrorMessage.FieldIsRequired, parameterName, parentName));
ArgumentNullException.ThrowIfNull(value, parameterName);

Check warning on line 15 in src/lib/Helpers/ValidationHelpers.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this argument from the method call; it hides the caller information. (https://rules.sonarsource.com/csharp/RSPEC-3236)

Check warning on line 15 in src/lib/Helpers/ValidationHelpers.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this argument from the method call; it hides the caller information. (https://rules.sonarsource.com/csharp/RSPEC-3236)
}

internal static void ValidateLength(string parameterName, string? value, int maxLength)
Expand Down
3 changes: 2 additions & 1 deletion src/lib/OpenAI/Authentication/ManifestUserHttpAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class ManifestUserHttpAuth : BaseManifestAuth
public string? AuthorizationType { get; set; }
public ManifestUserHttpAuth(string? authorizationType)
{
if (string.IsNullOrWhiteSpace(authorizationType) || (authorizationType != "basic" && authorizationType != "bearer"))
if (string.IsNullOrWhiteSpace(authorizationType) ||
(!string.Equals(authorizationType, "basic", StringComparison.OrdinalIgnoreCase) && !string.Equals(authorizationType, "bearer", StringComparison.OrdinalIgnoreCase)))
{
// Reference: https://platform.openai.com/docs/plugins/authentication/user-level
throw new ArgumentException($"{nameof(authorizationType)} must be either 'basic' or 'bearer'.");
Expand Down
4 changes: 2 additions & 2 deletions src/lib/OpenAI/OpenAIPluginManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ private void Validate(OpenAIPluginManifest openAIPluginManifest)
ValidationHelpers.ValidateLength(nameof(DescriptionForModel), openAIPluginManifest.DescriptionForModel, 8000);

ValidationHelpers.ValidateNullOrWhitespace(nameof(SchemaVersion), openAIPluginManifest.SchemaVersion, nameof(OpenAIPluginManifest));
ValidationHelpers.ValidateNullObject(nameof(Auth), openAIPluginManifest.Auth, nameof(OpenAIPluginManifest));
ValidationHelpers.ValidateNullObject(nameof(Api), openAIPluginManifest.Api, nameof(OpenAIPluginManifest));
ValidationHelpers.ValidateNullObject(nameof(Auth), openAIPluginManifest.Auth);
ValidationHelpers.ValidateNullObject(nameof(Api), openAIPluginManifest.Api);
ValidationHelpers.ValidateNullOrWhitespace(nameof(LogoUrl), openAIPluginManifest.LogoUrl, nameof(OpenAIPluginManifest));
ValidationHelpers.ValidateEmail(nameof(ContactEmail), openAIPluginManifest.ContactEmail, nameof(OpenAIPluginManifest));
ValidationHelpers.ValidateNullOrWhitespace(nameof(LegalInfoUrl), openAIPluginManifest.LegalInfoUrl, nameof(OpenAIPluginManifest));
Expand Down

0 comments on commit 36189d1

Please sign in to comment.