-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathJsonContentValidator.cs
More file actions
29 lines (23 loc) · 930 Bytes
/
JsonContentValidator.cs
File metadata and controls
29 lines (23 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using Microsoft.OpenApi;
using Newtonsoft.Json.Linq;
#if NET11_0_OR_GREATER
using OpenApiMediaType = Microsoft.OpenApi.IOpenApiMediaType;
#endif
namespace Swashbuckle.AspNetCore.ApiTesting;
public sealed class JsonContentValidator : IContentValidator
{
private readonly JsonValidator _jsonValidator = new();
public bool CanValidate(string mediaType) => mediaType.Contains("json");
public void Validate(OpenApiMediaType mediaTypeSpec, OpenApiDocument openApiDocument, HttpContent content)
{
if (mediaTypeSpec?.Schema == null)
{
return;
}
var instance = JToken.Parse(content.ReadAsStringAsync().Result);
if (!_jsonValidator.Validate(mediaTypeSpec.Schema, openApiDocument, instance, out IEnumerable<string> errorMessages))
{
throw new ContentDoesNotMatchSpecException(string.Join(Environment.NewLine, errorMessages));
}
}
}