From 5e0e7ef304e12ccc7a5dd0126f001b60854dfa32 Mon Sep 17 00:00:00 2001 From: Slava Vedernikov Date: Thu, 9 May 2024 14:16:39 +0100 Subject: [PATCH] Added YAML and JSON syntax validation --- .../C4InterFlow.Automation.csproj | 4 +- .../Readers/JsonAaCReaderStrategy.cs | 43 ++++++++++++++++--- .../Readers/YamlAaCReaderStrategy.cs | 41 +++++++++++++++++- C4InterFlow.Cli/C4InterFlow.Cli.csproj | 2 +- C4InterFlow/C4InterFlow.csproj | 4 +- 5 files changed, 83 insertions(+), 11 deletions(-) diff --git a/C4InterFlow.Automation/C4InterFlow.Automation.csproj b/C4InterFlow.Automation/C4InterFlow.Automation.csproj index 490551b18..17057bfd4 100644 --- a/C4InterFlow.Automation/C4InterFlow.Automation.csproj +++ b/C4InterFlow.Automation/C4InterFlow.Automation.csproj @@ -7,12 +7,12 @@ true true snupkg - 1.5.0 + 1.6.0 C4InterFlow.Automation - 1.5.0 + 1.6.0 Slava Vedernikov Revolutionise your Application Architecture Documentation with C4InterFlow. Designed for Architects and Engineers, this tool leverages the widely-recognised C4 Model (Architecture Visualisation framework), enhanced with unique features like Interface and Flow, to describe your Application Architecture as Code. Experience an intuitive, efficient way to document complex systems, ensuring clarity and consistency across your teams and products. Copyright 2024 Slava Vedernikov diff --git a/C4InterFlow.Automation/Readers/JsonAaCReaderStrategy.cs b/C4InterFlow.Automation/Readers/JsonAaCReaderStrategy.cs index cd8649213..28266ad47 100644 --- a/C4InterFlow.Automation/Readers/JsonAaCReaderStrategy.cs +++ b/C4InterFlow.Automation/Readers/JsonAaCReaderStrategy.cs @@ -1,9 +1,5 @@ using Newtonsoft.Json.Linq; -using YamlDotNet.Serialization.NamingConventions; -using YamlDotNet.Serialization; -using System.Reflection; -using System.Text; -using YamlDotNet.RepresentationModel; +using Newtonsoft.Json; namespace C4InterFlow.Automation.Readers { @@ -18,8 +14,45 @@ protected override JObject GetJsonObjectFromFile(string filePath) return JObject.Parse(json); } + private bool ValidateFiles(string[] paths) + { + var result = true; + + foreach (var path in paths) + { + string[] jsonFiles = Directory.GetFiles(path, "*.json", SearchOption.AllDirectories); + + foreach (string jsonFile in jsonFiles) + { + var json = File.ReadAllText(jsonFile); + + try + { + var jsonObject = JsonConvert.DeserializeObject(json); + } + catch (JsonReaderException ex) + { + Console.WriteLine($"File '{jsonFile}' has error at line {ex.LineNumber}, column {ex.LinePosition}: {ex.Message}"); + result = false; + } + catch (Exception ex) + { + Console.WriteLine($"File '{jsonFile}' has error: {ex.Message}"); + result = false; + } + } + } + + return result; + } + protected override JObject GetJsonObjectFromFiles(string[] paths) { + if (!ValidateFiles(paths)) + { + throw new InvalidDataException("Input file(s) have errors."); + } + JObject rootNode = null; foreach (var path in paths) diff --git a/C4InterFlow.Automation/Readers/YamlAaCReaderStrategy.cs b/C4InterFlow.Automation/Readers/YamlAaCReaderStrategy.cs index 7b8771279..39d96470f 100644 --- a/C4InterFlow.Automation/Readers/YamlAaCReaderStrategy.cs +++ b/C4InterFlow.Automation/Readers/YamlAaCReaderStrategy.cs @@ -1,9 +1,9 @@ using Newtonsoft.Json.Linq; using YamlDotNet.Serialization.NamingConventions; using YamlDotNet.Serialization; -using System.Reflection; using System.Text; using YamlDotNet.RepresentationModel; +using YamlDotNet.Core; namespace C4InterFlow.Automation.Readers { @@ -33,8 +33,47 @@ private JObject GetJsonObjectFromYaml(string yaml) return JObject.Parse(json); } + private bool ValidateFiles(string[] paths) + { + var result = true; + + foreach (var path in paths) + { + string[] yamlFiles = Directory.GetFiles(path, "*.yaml", SearchOption.AllDirectories); + + foreach (string yamlFile in yamlFiles) + { + var yaml = File.ReadAllText(yamlFile); + var input = new StringReader(yaml); + var deserializer = new DeserializerBuilder().Build(); + try + { + var yamlObject = deserializer.Deserialize(input); + } + catch (YamlException ex) + { + Console.WriteLine($"File '{yamlFile}' has error at line {ex.Start.Line}, column {ex.Start.Column}: {ex.Message}"); + result = false; + } + catch (Exception ex) + { + Console.WriteLine($"File '{yamlFile}' has error: {ex.Message}"); + result = false; + } + } + } + + return result; + } + protected override JObject GetJsonObjectFromFiles(string[] paths) { + + if(!ValidateFiles(paths)) + { + throw new InvalidDataException("Input file(s) have errors."); + } + YamlMappingNode rootNode = null; foreach (var path in paths) diff --git a/C4InterFlow.Cli/C4InterFlow.Cli.csproj b/C4InterFlow.Cli/C4InterFlow.Cli.csproj index 3426b37fd..18bea2084 100644 --- a/C4InterFlow.Cli/C4InterFlow.Cli.csproj +++ b/C4InterFlow.Cli/C4InterFlow.Cli.csproj @@ -5,7 +5,7 @@ net6.0 enable enable - 1.5.0 + 1.6.0 true true win-x64 diff --git a/C4InterFlow/C4InterFlow.csproj b/C4InterFlow/C4InterFlow.csproj index f3c84b1a8..594853907 100644 --- a/C4InterFlow/C4InterFlow.csproj +++ b/C4InterFlow/C4InterFlow.csproj @@ -7,12 +7,12 @@ true true snupkg - 1.5.0 + 1.6.0 C4InterFlow - 1.5.0 + 1.6.0 Slava Vedernikov Revolutionise your Application Architecture Documentation with C4InterFlow. Designed for Architects and Engineers, this tool leverages the widely-recognised C4 Model (Architecture Visualisation framework), enhanced with unique features like Interface and Flow, to describe your Application Architecture as Code. Experience an intuitive, efficient way to document complex systems, ensuring clarity and consistency across your teams and products. Copyright 2024 Slava Vedernikov