-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAsyncApiDocumentRules.cs
More file actions
66 lines (57 loc) · 2.27 KB
/
AsyncApiDocumentRules.cs
File metadata and controls
66 lines (57 loc) · 2.27 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI.Validation.Rules
{
using System.Linq;
using System.Text.RegularExpressions;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Validations;
[AsyncApiRule]
public static class AsyncApiDocumentRules
{
/// <summary>
/// The key regex.
/// </summary>
public static Regex KeyRegex = new Regex(@"^[a-zA-Z0-9\.\-_]+$");
public static ValidationRule<AsyncApiDocument> DocumentRequiredFields =>
new ValidationRule<AsyncApiDocument>(
(context, document) =>
{
context.Enter("info");
if (document.Info == null)
{
context.CreateError(
nameof(DocumentRequiredFields),
string.Format(Resource.Validation_FieldRequired, "info", "document"));
}
context.Exit();
context.Enter("channels");
if (document.Channels == null)
{
context.CreateError(
nameof(DocumentRequiredFields),
string.Format(Resource.Validation_FieldRequired, "channels", "document"));
}
context.Exit();
});
public static ValidationRule<AsyncApiDocument> KeyMustBeRegularExpression =>
new ValidationRule<AsyncApiDocument>(
(context, document) =>
{
if (document.Servers?.Keys == null)
{
return;
}
context.Enter("servers");
foreach (var key in document.Servers?.Keys)
{
if (!KeyRegex.IsMatch(key))
{
context.CreateError(
nameof(KeyMustBeRegularExpression),
string.Format(Resource.Validation_KeyMustMatchRegularExpr, key, "servers", KeyRegex.ToString()));
}
}
context.Exit();
});
}
}