Skip to content

Commit

Permalink
fix: JSON validation fails on timestamp wither longer fractional digits
Browse files Browse the repository at this point in the history
#265

Update to newer version of JsonSchema.Net 
---------

Signed-off-by: andreas hilti <andreas.hilti@bluewin.ch>
  • Loading branch information
andreas-hilti authored May 19, 2024
1 parent a27d923 commit 3124860
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/CycloneDX.Core/CycloneDX.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="JsonSchema.Net" Version="3.3.2" />
<PackageReference Include="JsonSchema.Net" Version="5.3.1" />
<PackageReference Include="protobuf-net" Version="3.2.26" />
<PackageReference Include="protobuf-net.BuildTools" Version="3.2.12" PrivateAssets="all" IncludeAssets="runtime;build;native;contentfiles;analyzers;buildtransitive" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
Expand Down
40 changes: 13 additions & 27 deletions src/CycloneDX.Core/Json/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ public static ValidationResult Validate(string jsonString, SpecificationVersion
private static ValidationResult Validate(JsonSchema schema, JsonDocument jsonDocument, string schemaVersionString)
{
var validationMessages = new List<string>();
var validationOptions = new ValidationOptions
var validationOptions = new EvaluationOptions
{
OutputFormat = OutputFormat.Detailed,
OutputFormat = OutputFormat.List,
RequireFormatValidation = true
};

var result = schema.Validate(jsonDocument.RootElement, validationOptions);
var result = schema.Evaluate(jsonDocument.RootElement, validationOptions);

if (result.IsValid)
{
Expand All @@ -197,34 +197,20 @@ private static ValidationResult Validate(JsonSchema schema, JsonDocument jsonDoc
}
else
{
validationMessages.Add($"Validation failed: {result.Message}");
validationMessages.Add(result.SchemaLocation.ToString());
validationMessages.Add($"On instance: {result.InstanceLocation}:");
validationMessages.Add(result.InstanceLocation.Evaluate(jsonDocument.RootElement).ToString());

if (result.NestedResults != null)
validationMessages.Add("Validation failed:");
// because we requested the results as a flat list
// there will be no nested results
foreach (var detail in result.Details)
{
var nestedResults = new Queue<ValidationResults>(result.NestedResults);

while (nestedResults.Count > 0)
if (detail.HasErrors)
{
var nestedResult = nestedResults.Dequeue();

if (
!string.IsNullOrEmpty(nestedResult.Message)
&& nestedResult.NestedResults != null
&& nestedResult.NestedResults.Count > 0)
{
validationMessages.Add($"{nestedResult.InstanceLocation}: {nestedResult.Message}");
}

if (nestedResult.NestedResults != null)
foreach (var error in detail.Errors)
{
foreach (var newNestedResult in nestedResult.NestedResults)
{
nestedResults.Enqueue(newNestedResult);
}
validationMessages.Add(error.Value);
}
validationMessages.Add(detail.SchemaLocation.ToString());
validationMessages.Add($"On instance: {detail.InstanceLocation}:");
validationMessages.Add(detail.InstanceLocation.Evaluate(jsonDocument.RootElement).ToString());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/CycloneDX.Spdx/CycloneDX.Spdx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="JsonSchema.Net" Version="3.3.2" />
<PackageReference Include="JsonSchema.Net" Version="5.3.1" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>

Expand Down
38 changes: 13 additions & 25 deletions src/CycloneDX.Spdx/Validation/JsonValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,42 +78,30 @@ public static ValidationResult Validate(string jsonString)
private static ValidationResult Validate(JsonSchema schema, JsonDocument jsonDocument)
{
var validationMessages = new List<string>();
var validationOptions = new ValidationOptions
var validationOptions = new EvaluationOptions
{
OutputFormat = OutputFormat.Detailed,
OutputFormat = OutputFormat.List,
RequireFormatValidation = true
};

var result = schema.Validate(jsonDocument.RootElement, validationOptions);
var result = schema.Evaluate(jsonDocument.RootElement, validationOptions);

if (!result.IsValid)
{
validationMessages.Add($"Validation failed: {result.Message}");
validationMessages.Add(result.SchemaLocation.ToString());

if (result.NestedResults != null)
validationMessages.Add("Validation failed:");
// because we requested the results as a flat list
// there will be no nested results
foreach (var detail in result.Details)
{
var nestedResults = new Queue<ValidationResults>(result.NestedResults);

while (nestedResults.Count > 0)
if (detail.HasErrors)
{
var nestedResult = nestedResults.Dequeue();

if (
!string.IsNullOrEmpty(nestedResult.Message)
&& nestedResult.NestedResults != null
&& nestedResult.NestedResults.Count > 0)
{
validationMessages.Add($"{nestedResult.InstanceLocation}: {nestedResult.Message}");
}

if (nestedResult.NestedResults != null)
foreach (var error in detail.Errors)
{
foreach (var newNestedResult in nestedResult.NestedResults)
{
nestedResults.Enqueue(newNestedResult);
}
validationMessages.Add(error.Value);
}
validationMessages.Add(detail.SchemaLocation.ToString());
validationMessages.Add($"On instance: {detail.InstanceLocation}:");
validationMessages.Add(detail.InstanceLocation.Evaluate(jsonDocument.RootElement).ToString());
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/CycloneDX.Core.Tests/Json/v1.5/ValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class ValidationTests
[InlineData("valid-metadata-manufacture-1.5.json")]
[InlineData("valid-metadata-supplier-1.5.json")]
[InlineData("valid-metadata-timestamp-1.5.json")]
[InlineData("valid-metadata-timestamp-higher-precision-1.5.json")]
[InlineData("valid-metadata-tool-1.5.json")]
[InlineData("valid-metadata-tool-deprecated-1.5.json")]
[InlineData("valid-minimal-viable-1.5.json")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"bomFormat": "CycloneDX",
"specVersion": "1.5",
"serialNumber": "urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79",
"version": 1,
"metadata": {
"timestamp": "2022-12-21T23:54:20.218381200Z"
},
"components": []
}

0 comments on commit 3124860

Please sign in to comment.