Skip to content

Commit 9a0adf4

Browse files
authored
Merge pull request #27 from I-RzR-I/feature/AddJsonValidation
Fix test for `CalculateAge`. Add new string extensions.
2 parents cbad95d + 3f963e6 commit 9a0adf4

File tree

6 files changed

+121
-5
lines changed

6 files changed

+121
-5
lines changed

docs/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,8 @@
111111

112112
### **v1.2.0.0**
113113
-> Add/adjust input validations in the `DataTypeExtensions` foler with extensions;<br />
114-
-> Add new string extensions: `IfNullThenEmpty`.<br />
114+
-> Add new string extensions: `IfNullThenEmpty`.<br />
115+
116+
### **v1.3.0.0**
117+
-> Fix test for `CalculateAge`;<br />
118+
-> Add new string extensions: `IsValidJson`, `IsValidJsonObject`, `IsValidJsonArray`.<br />

src/DomainCommonExtensions/DataTypeExtensions/StringExtensions.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#if NET || NETSTANDARD2_0_OR_GREATER
3131
using System.Text.Encodings.Web;
32+
using System.Text.Json;
3233
#endif
3334

3435
using System.Text.RegularExpressions;
@@ -1373,6 +1374,7 @@ public static string AddQueryString(this string url, string name, string value)
13731374
return url.AddQueryString(name + "=" + UrlEncoder.Default.Encode(value.IfNullThenEmpty()));
13741375
}
13751376
#endif
1377+
13761378
/// <summary>
13771379
/// Add hash fragment to URL
13781380
/// </summary>
@@ -1494,5 +1496,83 @@ public static string TrimSuffix(this string source, string suffix, StringCompari
14941496
/// <returns></returns>
14951497
/// <remarks></remarks>
14961498
public static string IfNullThenEmpty(this string source) => source ?? string.Empty;
1499+
1500+
#if NET || NETSTANDARD2_0_OR_GREATER
1501+
1502+
/// <summary>
1503+
/// Check if provided source is as valid JSON
1504+
/// </summary>
1505+
/// <param name="source">Source string</param>
1506+
/// <returns></returns>
1507+
/// <remarks></remarks>
1508+
public static bool IsValidJson(this string source)
1509+
{
1510+
try
1511+
{
1512+
if (source.IsMissing()) return false;
1513+
if ((source.TrimIfNotNull().StartsWith("{") && source.TrimIfNotNull().EndsWith("}")) ||
1514+
(source.TrimIfNotNull().StartsWith("[") && source.TrimIfNotNull().EndsWith("]")))
1515+
{
1516+
using var jsonDoc = JsonDocument.Parse(source);
1517+
1518+
return true;
1519+
}
1520+
else return false;
1521+
}
1522+
catch
1523+
{
1524+
return false;
1525+
}
1526+
}
1527+
1528+
/// <summary>
1529+
/// Check if provided source is as valid JSON object
1530+
/// </summary>
1531+
/// <param name="source">Source string</param>
1532+
/// <returns></returns>
1533+
/// <remarks></remarks>
1534+
public static bool IsValidJsonObject(this string source)
1535+
{
1536+
try
1537+
{
1538+
if (source.IsMissing()) return false;
1539+
if (source.TrimIfNotNull().StartsWith("{")
1540+
&& source.TrimIfNotNull().EndsWith("}"))
1541+
{
1542+
return source.IsValidJson();
1543+
}
1544+
else return false;
1545+
}
1546+
catch
1547+
{
1548+
return false;
1549+
}
1550+
}
1551+
1552+
/// <summary>
1553+
/// Check if provided source is as valid JSON array
1554+
/// </summary>
1555+
/// <param name="source">Source string</param>
1556+
/// <returns></returns>
1557+
/// <remarks></remarks>
1558+
public static bool IsValidJsonArray(this string source)
1559+
{
1560+
try
1561+
{
1562+
if (source.IsMissing()) return false;
1563+
if (source.TrimIfNotNull().StartsWith("[")
1564+
&& source.TrimIfNotNull().EndsWith("]"))
1565+
{
1566+
return source.IsValidJson();
1567+
}
1568+
else return false;
1569+
}
1570+
catch
1571+
{
1572+
return false;
1573+
}
1574+
}
1575+
1576+
#endif
14971577
}
14981578
}

src/DomainCommonExtensions/DomainCommonExtensions.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
<PackageReference Include="System.Text.Encodings.Web">
6262
<Version>4.7.2</Version>
6363
</PackageReference>
64+
<PackageReference Include="System.Text.Json">
65+
<Version>4.7.2</Version>
66+
</PackageReference>
6467
</ItemGroup>
6568

6669
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">

src/shared/GeneralAssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
4848
#endif
4949

50-
[assembly: AssemblyVersion("1.2.0.0")]
51-
[assembly: AssemblyFileVersion("1.2.0.0")]
52-
[assembly: AssemblyInformationalVersion("1.2.0.0")]
50+
[assembly: AssemblyVersion("1.3.0.0")]
51+
[assembly: AssemblyFileVersion("1.3.0.0")]
52+
[assembly: AssemblyInformationalVersion("1.3.0.0")]

src/tests/DataTypeTests/DateTimeTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ public void CalculateAgeTest()
164164
var date = new DateTime(1994, 8, 8, 13, 0, 0);
165165
var res = date.CalculateAge();
166166

167+
var age = DateTime.Now.Date.Day >= date.Day
168+
? DateTime.Now.Year - 1994
169+
: (DateTime.Now.Year - 1994) + 1;
170+
167171
Assert.IsNotNull(res);
168-
Assert.IsTrue(res.Equals(29));
172+
Assert.IsTrue(res.Equals(age));
169173
}
170174

171175
[TestMethod]

src/tests/DataTypeTests/StringTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,5 +510,30 @@ public void ValidatePullTest()
510510

511511
Assert.IsTrue(result);
512512
}
513+
514+
[DataRow("{ \"key\": \"value\" }")]
515+
[DataRow("[{ \"key\": \"value\" }]")]
516+
[DataRow("{ \"key\": 1 }")]
517+
[DataRow("[{ \"key\": 1 }]")]
518+
[TestMethod]
519+
public void IsValidJson_Success_Test(string source)
520+
{
521+
var result = source.IsValidJson();
522+
523+
Assert.IsTrue(result);
524+
}
525+
526+
[DataRow("{ \"key\": \"value\"")]
527+
[DataRow("{ \"key\": \"value\"")]
528+
[DataRow("[{ key: \"value\" }]")]
529+
[DataRow("{ \"key\": 1 }]")]
530+
[DataRow("\"key\": 1")]
531+
[TestMethod]
532+
public void IsValidJson_Fail_Test(string source)
533+
{
534+
var result = source.IsValidJson();
535+
536+
Assert.IsFalse(result);
537+
}
513538
}
514539
}

0 commit comments

Comments
 (0)