From 987fbe974236379f788df4fca2f0326901b2c094 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sat, 17 Aug 2024 17:51:32 -0500 Subject: [PATCH] fix: reuse validation logic in constructors --- src/AnthropicClient/Models/ImageContent.cs | 12 ++++++++---- src/AnthropicClient/Models/TextContent.cs | 11 ++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/AnthropicClient/Models/ImageContent.cs b/src/AnthropicClient/Models/ImageContent.cs index 20c071c..b096fab 100644 --- a/src/AnthropicClient/Models/ImageContent.cs +++ b/src/AnthropicClient/Models/ImageContent.cs @@ -19,6 +19,12 @@ public class ImageContent : Content { } + private void Validate(string mediaType, string data) + { + ArgumentValidator.ThrowIfNull(mediaType, nameof(mediaType)); + ArgumentValidator.ThrowIfNull(data, nameof(data)); + } + /// /// Initializes a new instance of the class. /// @@ -28,8 +34,7 @@ public class ImageContent : Content /// A new instance of the class. public ImageContent(string mediaType, string data) : base(ContentType.Image) { - ArgumentValidator.ThrowIfNull(mediaType, nameof(mediaType)); - ArgumentValidator.ThrowIfNull(data, nameof(data)); + Validate(mediaType, data); Source = new(mediaType, data); } @@ -44,8 +49,7 @@ public class ImageContent : Content /// Thrown when the media type, data, or cache control is null. public ImageContent(string mediaType, string data, CacheControl cacheControl) : base(ContentType.Image, cacheControl) { - ArgumentValidator.ThrowIfNull(mediaType, nameof(mediaType)); - ArgumentValidator.ThrowIfNull(data, nameof(data)); + Validate(mediaType, data); Source = new(mediaType, data); } diff --git a/src/AnthropicClient/Models/TextContent.cs b/src/AnthropicClient/Models/TextContent.cs index 01b14ee..b9827bc 100644 --- a/src/AnthropicClient/Models/TextContent.cs +++ b/src/AnthropicClient/Models/TextContent.cs @@ -19,6 +19,11 @@ public class TextContent : Content { } + private void Validate(string text) + { + ArgumentValidator.ThrowIfNull(text, nameof(text)); + } + /// /// Initializes a new instance of the class. /// @@ -27,7 +32,7 @@ public class TextContent : Content /// A new instance of the class. public TextContent(string text) : base(ContentType.Text) { - ArgumentValidator.ThrowIfNull(text, nameof(text)); + Validate(text); Text = text; } @@ -41,8 +46,8 @@ public class TextContent : Content /// Thrown when the text or cache control is null. public TextContent(string text, CacheControl cacheControl) : base(ContentType.Text, cacheControl) { - ArgumentValidator.ThrowIfNull(text, nameof(text)); - + Validate(text); + Text = text; } } \ No newline at end of file