-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathJsonAnyOfValidator.cs
More file actions
39 lines (32 loc) · 1.23 KB
/
JsonAnyOfValidator.cs
File metadata and controls
39 lines (32 loc) · 1.23 KB
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
30
31
32
33
34
35
36
37
38
39
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Linq;
namespace Swashbuckle.AspNetCore.ApiTesting
{
public class JsonAnyOfValidator(JsonValidator jsonValidator) : IJsonValidator
{
private readonly JsonValidator _jsonValidator = jsonValidator;
public bool CanValidate(OpenApiSchema schema) => schema.AnyOf != null && schema.AnyOf.Any();
public bool Validate(
OpenApiSchema schema,
OpenApiDocument openApiDocument,
JToken instance,
out IEnumerable<string> errorMessages)
{
var errorMessagesList = new List<string>();
var anyOfArray = schema.AnyOf.ToArray();
for (int i = 0; i < anyOfArray.Length; i++)
{
if (_jsonValidator.Validate(anyOfArray[i], openApiDocument, instance, out IEnumerable<string> subErrorMessages))
{
errorMessages = [];
return true;
}
errorMessagesList.AddRange(subErrorMessages.Select(msg => $"{msg} (anyOf[{i}])"));
}
errorMessages = errorMessagesList;
return !errorMessages.Any();
}
}
}