-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Property validation in JObjects (#25)
- Loading branch information
1 parent
eee709f
commit 1a15a1e
Showing
7 changed files
with
182 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/HotPotato.OpenApi/Validators/BodyValidators/JsonSchemaExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Newtonsoft.Json.Linq; | ||
using NJsonSchema; | ||
using System.Collections.Generic; | ||
|
||
namespace HotPotato.OpenApi.Validators | ||
{ | ||
internal static class JsonSchemaExtensions | ||
{ | ||
public static List<ValidationError> ValidateUndefinedProperties(this JsonSchema schema, string bodyString) | ||
{ | ||
JToken bodyToken = JToken.Parse(bodyString); | ||
List<ValidationError> validationErrors = new List<ValidationError>(); | ||
|
||
if (bodyToken is JObject) | ||
{ | ||
JEnumerable<JToken> childTokens = bodyToken.Children(); | ||
var schemaProperties = schema?.ActualSchema?.ActualProperties; | ||
|
||
if (schemaProperties != null) | ||
{ | ||
foreach (JToken childToken in childTokens) | ||
{ | ||
if (!schemaProperties.ContainsKey(childToken.Path)) | ||
{ | ||
validationErrors.Add(new ValidationError($"Property not found in spec: {childToken.Path}", ValidationErrorKind.PropertyNotInSpec, childToken.Path, 0, 0)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return validationErrors; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
test/HotPotato.OpenApi.Test/Validators/BodyValidator/JsonSchemaExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using HotPotato.OpenApi.Models; | ||
using Moq; | ||
using NJsonSchema; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace HotPotato.OpenApi.Validators | ||
{ | ||
public class JsonSchemaExtensionsTest | ||
{ | ||
private const string AValidBody = "{'foo': 1}"; | ||
private const string AValidSchema = @"{'properties':{'foo':{'type':'integer'}}}"; | ||
private const string ABodyWithAnUnexpectedProperty = "{'bar': 2}"; | ||
|
||
[Fact] | ||
public void ValidateUndefinedProperties_ReturnsEmptyList_WithValidSchema() | ||
{ | ||
JsonSchema subject = JsonSchema.FromJsonAsync(AValidSchema).Result; | ||
|
||
List<ValidationError> results = subject.ValidateUndefinedProperties(AValidBody); | ||
|
||
Assert.Empty(results); | ||
} | ||
|
||
[Fact] | ||
public void ValidateUndefinedProperties_ReturnsEmptyList_WithNullActualSchema() | ||
{ | ||
JsonSchema nullSchema = null; | ||
Mock<JsonSchema> subject = new Mock<JsonSchema>(); | ||
subject.SetupGet(x => x.ActualSchema).Returns(nullSchema); | ||
|
||
List<ValidationError> results = subject.Object.ValidateUndefinedProperties(AValidBody); | ||
|
||
Assert.Empty(results); | ||
} | ||
|
||
[Fact] | ||
public void JsonBodyValidator_ReturnsAListWithCorrectValidationErrorKind_WithInvalidSchema() | ||
{ | ||
JsonSchema subject = JsonSchema.FromJsonAsync(AValidSchema).Result; | ||
|
||
List<ValidationError> results = subject.ValidateUndefinedProperties(ABodyWithAnUnexpectedProperty); | ||
|
||
Assert.Equal(ValidationErrorKind.PropertyNotInSpec, results[0].Kind); | ||
} | ||
|
||
[Fact] | ||
public void JsonBodyValidator_ReturnsAListWithCorrectValidationErrorKind_WithBlankSchema() | ||
{ | ||
JsonSchema subject = new JsonSchema(); | ||
|
||
List<ValidationError> results = subject.ValidateUndefinedProperties(ABodyWithAnUnexpectedProperty); | ||
|
||
Assert.Equal(ValidationErrorKind.PropertyNotInSpec, results[0].Kind); | ||
} | ||
} | ||
} |