Skip to content

Commit

Permalink
Merge pull request #136 from Energinet-DataHub/kft/update_nuget_packages
Browse files Browse the repository at this point in the history
Upgrade package version
  • Loading branch information
Kristian F. Thomsen authored Mar 21, 2023
2 parents 9245954 + 6d26566 commit 55b8504
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion source/DocumentValidation/DocumentValidation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonSchema.Net" Version="3.3.2" />
<PackageReference Include="JsonSchema.Net" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,35 +185,39 @@ private static string GetJsonDateStringWithoutQuotes(JToken token)
return token.ToString(Formatting.None).Trim('"');
}

private static bool IsValid(JsonDocument document, JsonSchema schema)
{
return schema.Evaluate(document, new EvaluationOptions() { OutputFormat = OutputFormat.Flag, }).IsValid;
}

private async Task ValidateMessageAsync(JsonSchema schema, Stream message)
{
var jsonDocument = await JsonDocument.ParseAsync(message).ConfigureAwait(false);

var validationOptions = new ValidationOptions()
if (IsValid(jsonDocument, schema) == false)
{
OutputFormat = OutputFormat.Detailed,
};

var validationResult = schema.Validate(jsonDocument, validationOptions);

if (!validationResult.IsValid)
{
AddValidationErrors(validationResult);
ExtractValidationErrors(jsonDocument, schema);
}

ResetMessagePosition(message);
}

private void AddValidationErrors(ValidationResults validationResult)
private void ExtractValidationErrors(JsonDocument jsonDocument, JsonSchema schema)
{
AddValidationError(validationResult.Message);
var result = schema.Evaluate(jsonDocument, new EvaluationOptions() { OutputFormat = OutputFormat.List, });
result
.Details
.Where(detail => detail.HasErrors)
.ToList().ForEach(AddValidationErrors);
}

if (validationResult.HasNestedResults)
private void AddValidationErrors(EvaluationResults validationResult)
{
var propertyName = validationResult.InstanceLocation.ToString();
var errorsValues = validationResult.Errors ?? new Dictionary<string, string>();
foreach (var error in errorsValues)
{
foreach (var result in validationResult.NestedResults)
{
AddValidationError(result.Message);
}
AddValidationError($"{propertyName}: {error}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public static void AssertConformsToSchema(JsonDocument document, JsonSchema sche
{
if (schema == null) throw new InvalidOperationException($"Schema not found for business process type {documentType}");

var validationOptions = new ValidationOptions()
var validationOptions = new EvaluationOptions()
{
OutputFormat = OutputFormat.Detailed,
OutputFormat = OutputFormat.List,
};

var validationResult = schema.Validate(document, validationOptions);
var validationResult = schema.Evaluate(document, validationOptions);

Assert.True(validationResult.IsValid);
}
Expand Down

0 comments on commit 55b8504

Please sign in to comment.