Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added YAML and JSON syntax validation #84

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions C4InterFlow.Automation/C4InterFlow.Automation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<Deterministic>true</Deterministic>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>1.5.0</Version>
<Version>1.6.0</Version>
</PropertyGroup>

<PropertyGroup>
<PackageId>C4InterFlow.Automation</PackageId>
<PackageVersion>1.5.0</PackageVersion>
<PackageVersion>1.6.0</PackageVersion>
<Authors>Slava Vedernikov</Authors>
<Description>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.</Description>
<Copyright>Copyright 2024 Slava Vedernikov</Copyright>
Expand Down
43 changes: 38 additions & 5 deletions C4InterFlow.Automation/Readers/JsonAaCReaderStrategy.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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)
Expand Down
41 changes: 40 additions & 1 deletion C4InterFlow.Automation/Readers/YamlAaCReaderStrategy.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion C4InterFlow.Cli/C4InterFlow.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.5.0</Version>
<Version>1.6.0</Version>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
Expand Down
4 changes: 2 additions & 2 deletions C4InterFlow/C4InterFlow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<Deterministic>true</Deterministic>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>1.5.0</Version>
<Version>1.6.0</Version>
</PropertyGroup>

<PropertyGroup>
<PackageId>C4InterFlow</PackageId>
<PackageVersion>1.5.0</PackageVersion>
<PackageVersion>1.6.0</PackageVersion>
<Authors>Slava Vedernikov</Authors>
<Description>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.</Description>
<Copyright>Copyright 2024 Slava Vedernikov</Copyright>
Expand Down