From 36189d1950252955136550becc367b1ed6f00aaa Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Tue, 10 Oct 2023 10:12:10 -0700 Subject: [PATCH] chore: Use ThrowIfNull for validating null objects. --- src/lib/Helpers/ValidationHelpers.cs | 5 ++--- src/lib/OpenAI/Authentication/ManifestUserHttpAuth.cs | 3 ++- src/lib/OpenAI/OpenAIPluginManifest.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib/Helpers/ValidationHelpers.cs b/src/lib/Helpers/ValidationHelpers.cs index 3f7ff17..0299c9a 100644 --- a/src/lib/Helpers/ValidationHelpers.cs +++ b/src/lib/Helpers/ValidationHelpers.cs @@ -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); } internal static void ValidateLength(string parameterName, string? value, int maxLength) diff --git a/src/lib/OpenAI/Authentication/ManifestUserHttpAuth.cs b/src/lib/OpenAI/Authentication/ManifestUserHttpAuth.cs index 45692c8..5435dab 100644 --- a/src/lib/OpenAI/Authentication/ManifestUserHttpAuth.cs +++ b/src/lib/OpenAI/Authentication/ManifestUserHttpAuth.cs @@ -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'."); diff --git a/src/lib/OpenAI/OpenAIPluginManifest.cs b/src/lib/OpenAI/OpenAIPluginManifest.cs index ed38c77..ac586e5 100644 --- a/src/lib/OpenAI/OpenAIPluginManifest.cs +++ b/src/lib/OpenAI/OpenAIPluginManifest.cs @@ -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));