-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathOpenApiServerRules.cs
57 lines (52 loc) · 1.96 KB
/
OpenApiServerRules.cs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Properties;
namespace Microsoft.OpenApi.Validations.Rules
{
/// <summary>
/// The validation rules for <see cref="OpenApiServer"/>.
/// </summary>
[OpenApiRule]
public static class OpenApiServerRules
{
/// <summary>
/// Validate the field is required.
/// </summary>
public static ValidationRule<OpenApiServer> ServerRequiredFields =>
new(nameof(ServerRequiredFields),
(context, server) =>
{
context.Enter("url");
if (server.Url == null)
{
context.CreateError(nameof(ServerRequiredFields),
String.Format(SRResource.Validation_FieldIsRequired, "url", "server"));
}
context.Exit();
context.Enter("variables");
foreach (var variable in server.Variables)
{
context.Enter(variable.Key);
ValidateServerVariableRequiredFields(context, variable.Key, variable.Value);
context.Exit();
}
context.Exit();
});
// add more rules
/// <summary>
/// Validate required fields in server variable
/// </summary>
private static void ValidateServerVariableRequiredFields(IValidationContext context, string key, OpenApiServerVariable item)
{
context.Enter("default");
if (string.IsNullOrEmpty(item.Default))
{
context.CreateError("ServerVariableMustHaveDefaultValue",
String.Format(SRResource.Validation_FieldIsRequired, "default", key));
}
context.Exit();
}
}
}