|
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) |
13 | 61 | {
|
14 |
| - _logger = logger; |
15 |
| - } |
| 62 | + var generator = new JSchemaGenerator |
| 63 | + { |
| 64 | + ContractResolver = new CamelCasePropertyNamesContractResolver() |
| 65 | + }; |
| 66 | + JSchema schema = generator.Generate(typeof(Certification)); |
16 | 67 |
|
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 | +} |
|
0 commit comments