Skip to content

Commit

Permalink
Merge pull request #27 from I-RzR-I/feature/AddJsonValidation
Browse files Browse the repository at this point in the history
Fix test for `CalculateAge`. Add new string extensions.
  • Loading branch information
I-RzR-I authored Sep 22, 2024
2 parents cbad95d + 3f963e6 commit 9a0adf4
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 5 deletions.
6 changes: 5 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@

### **v1.2.0.0**
-> Add/adjust input validations in the `DataTypeExtensions` foler with extensions;<br />
-> Add new string extensions: `IfNullThenEmpty`.<br />
-> Add new string extensions: `IfNullThenEmpty`.<br />

### **v1.3.0.0**
-> Fix test for `CalculateAge`;<br />
-> Add new string extensions: `IsValidJson`, `IsValidJsonObject`, `IsValidJsonArray`.<br />
80 changes: 80 additions & 0 deletions src/DomainCommonExtensions/DataTypeExtensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#if NET || NETSTANDARD2_0_OR_GREATER
using System.Text.Encodings.Web;
using System.Text.Json;
#endif

using System.Text.RegularExpressions;
Expand Down Expand Up @@ -1373,6 +1374,7 @@ public static string AddQueryString(this string url, string name, string value)
return url.AddQueryString(name + "=" + UrlEncoder.Default.Encode(value.IfNullThenEmpty()));
}
#endif

/// <summary>
/// Add hash fragment to URL
/// </summary>
Expand Down Expand Up @@ -1494,5 +1496,83 @@ public static string TrimSuffix(this string source, string suffix, StringCompari
/// <returns></returns>
/// <remarks></remarks>
public static string IfNullThenEmpty(this string source) => source ?? string.Empty;

#if NET || NETSTANDARD2_0_OR_GREATER

/// <summary>
/// Check if provided source is as valid JSON
/// </summary>
/// <param name="source">Source string</param>
/// <returns></returns>
/// <remarks></remarks>
public static bool IsValidJson(this string source)
{
try
{
if (source.IsMissing()) return false;
if ((source.TrimIfNotNull().StartsWith("{") && source.TrimIfNotNull().EndsWith("}")) ||
(source.TrimIfNotNull().StartsWith("[") && source.TrimIfNotNull().EndsWith("]")))
{
using var jsonDoc = JsonDocument.Parse(source);

return true;
}
else return false;
}
catch
{
return false;
}
}

/// <summary>
/// Check if provided source is as valid JSON object
/// </summary>
/// <param name="source">Source string</param>
/// <returns></returns>
/// <remarks></remarks>
public static bool IsValidJsonObject(this string source)
{
try
{
if (source.IsMissing()) return false;
if (source.TrimIfNotNull().StartsWith("{")
&& source.TrimIfNotNull().EndsWith("}"))
{
return source.IsValidJson();
}
else return false;
}
catch
{
return false;
}
}

/// <summary>
/// Check if provided source is as valid JSON array
/// </summary>
/// <param name="source">Source string</param>
/// <returns></returns>
/// <remarks></remarks>
public static bool IsValidJsonArray(this string source)
{
try
{
if (source.IsMissing()) return false;
if (source.TrimIfNotNull().StartsWith("[")
&& source.TrimIfNotNull().EndsWith("]"))
{
return source.IsValidJson();
}
else return false;
}
catch
{
return false;
}
}

#endif
}
}
3 changes: 3 additions & 0 deletions src/DomainCommonExtensions/DomainCommonExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
<PackageReference Include="System.Text.Encodings.Web">
<Version>4.7.2</Version>
</PackageReference>
<PackageReference Include="System.Text.Json">
<Version>4.7.2</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
Expand Down
6 changes: 3 additions & 3 deletions src/shared/GeneralAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
#endif

[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyInformationalVersion("1.3.0.0")]
6 changes: 5 additions & 1 deletion src/tests/DataTypeTests/DateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,12 @@ public void CalculateAgeTest()
var date = new DateTime(1994, 8, 8, 13, 0, 0);
var res = date.CalculateAge();

var age = DateTime.Now.Date.Day >= date.Day
? DateTime.Now.Year - 1994
: (DateTime.Now.Year - 1994) + 1;

Assert.IsNotNull(res);
Assert.IsTrue(res.Equals(29));
Assert.IsTrue(res.Equals(age));
}

[TestMethod]
Expand Down
25 changes: 25 additions & 0 deletions src/tests/DataTypeTests/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,5 +510,30 @@ public void ValidatePullTest()

Assert.IsTrue(result);
}

[DataRow("{ \"key\": \"value\" }")]
[DataRow("[{ \"key\": \"value\" }]")]
[DataRow("{ \"key\": 1 }")]
[DataRow("[{ \"key\": 1 }]")]
[TestMethod]
public void IsValidJson_Success_Test(string source)
{
var result = source.IsValidJson();

Assert.IsTrue(result);
}

[DataRow("{ \"key\": \"value\"")]
[DataRow("{ \"key\": \"value\"")]
[DataRow("[{ key: \"value\" }]")]
[DataRow("{ \"key\": 1 }]")]
[DataRow("\"key\": 1")]
[TestMethod]
public void IsValidJson_Fail_Test(string source)
{
var result = source.IsValidJson();

Assert.IsFalse(result);
}
}
}

0 comments on commit 9a0adf4

Please sign in to comment.