Skip to content

Commit 193280e

Browse files
committed
Switched to newtonsoft json schema for validation and implented validation
1 parent 1bbaa54 commit 193280e

File tree

4 files changed

+139
-33
lines changed

4 files changed

+139
-33
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace azure_project_generator
9+
{
10+
public class Certification
11+
{
12+
[Required]
13+
public string CertificationCode { get; set; }
14+
[Required]
15+
public string CertificationName { get; set; }
16+
[Required]
17+
public List<SkillMeasured> SkillsMeasured { get; set; }
18+
}
19+
20+
}

azure-project-generator/ProcessFile.cs

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,88 @@
1-
using System.IO;
2-
using System.Threading.Tasks;
3-
using Microsoft.Azure.Functions.Worker;
4-
using Microsoft.Extensions.Logging;
5-
6-
namespace azure_project_generator
7-
{
8-
public class ProcessFile
9-
{
10-
private readonly ILogger<ProcessFile> _logger;
11-
12-
public ProcessFile(ILogger<ProcessFile> logger)
1+
using Microsoft.Azure.Functions.Worker;
2+
using Microsoft.Extensions.Logging;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
5+
using Newtonsoft.Json.Schema;
6+
using Newtonsoft.Json.Schema.Generation;
7+
using Newtonsoft.Json.Serialization;
8+
9+
namespace azure_project_generator
10+
{
11+
public class ProcessFile
12+
{
13+
private readonly ILogger<ProcessFile> _logger;
14+
15+
16+
public ProcessFile(ILogger<ProcessFile> logger)
17+
{
18+
_logger = logger;
19+
20+
}
21+
22+
23+
[Function(nameof(ProcessFile))] public async Task Run([BlobTrigger("certdata/{name}", Connection = "AzureWebJobsStorage")] Stream stream, string name)
24+
{
25+
string content;
26+
try
27+
{
28+
using var blobStreamReader = new StreamReader(stream);
29+
content = await blobStreamReader.ReadToEndAsync();
30+
}
31+
catch (IOException ex)
32+
{
33+
_logger.LogError($"Error reading blob content: {ex.Message}");
34+
return;
35+
}
36+
37+
_logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name}");
38+
39+
if (string.IsNullOrWhiteSpace(content))
40+
{
41+
_logger.LogError("Blob content is empty or whitespace.");
42+
return;
43+
}
44+
45+
try
46+
{
47+
ValidateJsonContent(content);
48+
49+
50+
}
51+
catch (JsonReaderException ex)
52+
{
53+
_logger.LogError($"JSON parsing error: {ex.Message}");
54+
}
55+
catch (Exception ex)
56+
{
57+
_logger.LogError($"An unexpected error occurred: {ex.Message}");
58+
}
59+
}
60+
private void ValidateJsonContent(string content)
1361
{
14-
_logger = logger;
15-
}
62+
var generator = new JSchemaGenerator
63+
{
64+
ContractResolver = new CamelCasePropertyNamesContractResolver()
65+
};
66+
JSchema schema = generator.Generate(typeof(Certification));
1667

17-
[Function(nameof(ProcessFile))]
18-
public async Task Run([BlobTrigger("raw/{name}", Connection = "AzureWebJobsStorage")] Stream stream, string name)
19-
{
20-
using var blobStreamReader = new StreamReader(stream);
21-
var content = await blobStreamReader.ReadToEndAsync();
22-
_logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {content}");
23-
}
24-
}
25-
}
68+
JToken jsonContent = JToken.Parse(content);
69+
IList<string> messages;
70+
bool valid = jsonContent.IsValid(schema, out messages);
71+
72+
if (!valid)
73+
{
74+
foreach (var message in messages)
75+
{
76+
_logger.LogError($"Schema validation error: {message}");
77+
}
78+
}
79+
else
80+
{
81+
_logger.LogInformation("JSON content is valid against the schema.");
82+
}
83+
}
84+
85+
86+
87+
}
88+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace azure_project_generator
9+
{
10+
public class SkillMeasured
11+
{
12+
[Required]
13+
public string Name { get; set; }
14+
[Required]
15+
public string Percentage { get; set; }
16+
[Required]
17+
public Dictionary<string, List<string>> Topics { get; set; }
18+
}
19+
}

azure-project-generator/azure-project-generator.csproj

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99
<UserSecretsId>1d7c9e7d-f7ff-4f28-a6f1-943f954af989</UserSecretsId>
1010
</PropertyGroup>
1111
<ItemGroup>
12-
<PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" />
13-
<PackageReference Include="Azure.Storage.Files.Shares" Version="12.18.0" />
14-
<PackageReference Include="Azure.Storage.Queues" Version="12.18.0" />
15-
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" />
16-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
17-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.0.0" />
18-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.2" />
19-
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
20-
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0" />
21-
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.3" />
12+
<Content Include="local.settings.json" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<PackageReference Include="Azure.Storage.Blobs" Version="12.21.2" />
16+
<PackageReference Include="Azure.Storage.Files.Shares" Version="12.19.1" />
17+
<PackageReference Include="Azure.Storage.Queues" Version="12.19.1" />
18+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
19+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
20+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.6.0" />
21+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.4" />
22+
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
23+
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.3.0" />
24+
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.5" />
2225
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
26+
<PackageReference Include="Newtonsoft.Json.Schema" Version="4.0.1" />
2327
</ItemGroup>
2428
<ItemGroup>
2529
<None Update="host.json">

0 commit comments

Comments
 (0)