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

Upgrade package version #136

Merged
merged 2 commits into from
Mar 21, 2023
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
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