-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateEssential.ts
More file actions
159 lines (120 loc) · 3.7 KB
/
validateEssential.ts
File metadata and controls
159 lines (120 loc) · 3.7 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { AsyncAPIDocumentInterface, ChannelInterface, ChannelsInterface, ComponentsInterface, InfoInterface, MessagesInterface, ServerInterface, ServersInterface, ServerVariableInterface, ServerVariablesInterface } from '@asyncapi/parser';
import { writeFile } from 'fs';
export function write_json(data: string, filename: string) {
writeFile(filename, data, 'utf8', (err) => {
if (err) {
console.error('Error writing to the file:', err);
return;
}
console.log('JSON file has been written successfully!');
});
}
export function validateAsyncApi(item: AsyncAPIDocumentInterface): string[] {
let missing: string[] = [];
if (item.info == undefined) {
missing.push('info');
} else {
missing = missing.concat(validateInfo(item.info()));
}
if (item.servers == undefined) {
missing.push('servers')
} else {
missing = missing.concat(validateServers(item.servers()))
}
if (item.channels == undefined) {
missing.push('channels')
} else {
missing = missing.concat(validateChannels(item.channels()))
}
if (item.components == undefined) {
missing.push('components')
} else {
missing = missing.concat(validateComponents(item.components()))
}
if (item.operations == undefined) {
missing.push('operations')
}
if (item.operations == undefined) {
missing.push('operations')
}
return missing.map(i => 'root.' + i);
}
function validateInfo(item: & InfoInterface): string[] {
const missing: string[] = [];
const title = 'title';
if (item.title == undefined) {
missing.push(title)
}
const version = 'version';
if (item.version == undefined) {
missing.push(version)
}
return missing.map(i => 'info' + i);;
}
function validateServers(items: & ServersInterface): string[] {
let missing: string[] = [];
for (const item of items) {
missing = missing.concat(validateServer(item))
}
return missing.map(i => 'servers.' + i);
}
function validateServer(item: & ServerInterface): string[] {
let missing: string[] = [];
if (item.url == undefined) {
missing.push('url')
}
if (item.protocol == undefined) {
missing.push('protocol')
}
if (item.variables != undefined) {
missing = missing.concat(validateServerVariables(item.variables()))
}
return missing.map(i => 'server.' + i);
}
function validateServerVariables(variables: & ServerVariablesInterface): string[] {
let missing: string[] = [];
for (const variable of variables) {
missing = missing.concat(validateServerVariable(variable))
}
return missing.map(i => 'variables.' + i);
}
function validateServerVariable(item: & ServerVariableInterface): string[] {
const missing: string[] = [];
if (item.defaultValue == undefined) {
missing.push(`item missing default value`);
}
return missing.map(i => 'variable.' + i);;
}
function validateChannels(channels: & ChannelsInterface): string[] {
let missing: string[] = [];
for (const channel of channels) {
missing = missing.concat(validateChannel(channel))
}
return missing.map(i => 'channels.' + i);
}
function validateChannel(item: & ChannelInterface): string[] {
let missing: string[] = [];
if (item.messages == undefined) {
missing.push(`message`)
} else {
missing = missing.concat(validateMessages(item.messages()))
}
return missing.map(i => "channel." + i);
}
function validateMessages(items: & MessagesInterface): string[] {
const missing: string[] = [];
if (items.length == 0) {
missing.push('missing messages');
}
return missing.map(i => 'messages.' + i);
}
function validateComponents(item: & ComponentsInterface): string[] {
const missing: string[] = [];
if (Object.keys(item.messages()).length == 0) {
missing.push('message');
}
if (Object.keys(item.schemas()).length == 0) {
missing.push('schema');
}
return missing.map(i => 'components.' + i);;
}