-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAsyncApiDocumentRules.cs
More file actions
112 lines (98 loc) · 4.35 KB
/
AsyncApiDocumentRules.cs
File metadata and controls
112 lines (98 loc) · 4.35 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI.Validation.Rules
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Validations;
[AsyncApiRule]
public static class AsyncApiDocumentRules
{
private static TimeSpan RegexTimeout = TimeSpan.FromSeconds(1);
/// <summary>
/// The key regex.
/// </summary>
public static Regex KeyRegex = new Regex(@"^[a-zA-Z0-9\.\-_]+$", RegexOptions.None, RegexTimeout);
public static Regex ChannelKeyUriTemplateRegex = new Regex(@"^(?:(?:[^\x00-\x20""'<>%\\^`{|}]|%[0-9a-f]{2})|\{[+#./;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?(?:,(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?)*\})*$", RegexOptions.IgnoreCase, RegexTimeout);
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");
try
{
if (document.Channels == null)
{
context.CreateError(
nameof(DocumentRequiredFields),
string.Format(Resource.Validation_FieldRequired, "channels", "document"));
return;
}
var hashSet = new HashSet<string>();
foreach (var key in document.Channels.Keys)
{
// Uri-template
if (!ChannelKeyUriTemplateRegex.IsMatch(key))
{
context.CreateError(
"ChannelKeys",
string.Format(Resource.Validation_KeyMustMatchRegularExpr, key, "channels", KeyRegex.ToString()));
}
// Unique channel keys
var pathSignature = GetKeySignature(key);
if (!hashSet.Add(pathSignature))
{
context.CreateError("ChannelKey", string.Format(Resource.Validation_ChannelsMustBeUnique, pathSignature));
}
}
}
finally
{
context.Exit();
}
});
private static string GetKeySignature(string path)
{
for (int openBrace = path.IndexOf('{'); openBrace > -1; openBrace = path.IndexOf('{', openBrace + 2))
{
int closeBrace = path.IndexOf('}', openBrace);
if (closeBrace < 0)
{
return path;
}
path = path.Substring(0, openBrace + 1) + path.Substring(closeBrace);
}
return path;
}
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();
});
}
}