From 2cff5e273f0789713f940d4d0f5461c4ea882b21 Mon Sep 17 00:00:00 2001 From: "Kristian F. Thomsen" Date: Tue, 21 Mar 2023 13:00:24 +0100 Subject: [PATCH] Upgrade package version --- .../DocumentValidation.csproj | 2 +- .../JsonMessageParser.cs | 36 ++++++++++--------- .../Asserts/AssertJsonMessage.cs | 6 ++-- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/source/DocumentValidation/DocumentValidation.csproj b/source/DocumentValidation/DocumentValidation.csproj index 383c6c06f5..4fecdcd752 100644 --- a/source/DocumentValidation/DocumentValidation.csproj +++ b/source/DocumentValidation/DocumentValidation.csproj @@ -7,7 +7,7 @@ - + diff --git a/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/JsonMessageParser.cs b/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/JsonMessageParser.cs index 2b6b27d14f..9e97b99179 100644 --- a/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/JsonMessageParser.cs +++ b/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/JsonMessageParser.cs @@ -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(); + foreach (var error in errorsValues) { - foreach (var result in validationResult.NestedResults) - { - AddValidationError(result.Message); - } + AddValidationError($"{propertyName}: {error}"); } } diff --git a/source/Tests/Infrastructure/OutgoingMessages/Asserts/AssertJsonMessage.cs b/source/Tests/Infrastructure/OutgoingMessages/Asserts/AssertJsonMessage.cs index 0db82a9923..77cbfcee51 100644 --- a/source/Tests/Infrastructure/OutgoingMessages/Asserts/AssertJsonMessage.cs +++ b/source/Tests/Infrastructure/OutgoingMessages/Asserts/AssertJsonMessage.cs @@ -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); }