-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavrolint.js
175 lines (144 loc) · 5.38 KB
/
avrolint.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const fs = require('fs')
const core = require('@actions/core');
const avro = require('avro-js');
let avrolint = function(avscFilePath, options={"undocumentedCheck": true, "complexUnionCheck": true}) {
return new Promise((resolve) => {
// default, assume input is single file
var filePaths = [avscFilePath];
try {
// try to see if it's a list of paths in JSON array
filePaths = JSON.parse(avscFilePath);
}
catch (err) {
// ignore
}
var filePathsWithErrors = new Set();
for (const filePath of filePaths) {
if (typeof filePath === 'undefined' || !fs.existsSync(filePath)) {
if (typeof filePath === 'undefined') {
filePathsWithErrors.add("undefined"); // without this a blank error message is printed
} else {
filePathsWithErrors.add(filePath);
}
core.error("avscFilePath is invalid: '" + filePath + "'");
continue;
}
var fileContents = fs.readFileSync(filePath);
var avroSchemaJson;
try {
avroSchemaJson = JSON.parse(fileContents);
} catch (err) {
filePathsWithErrors.add(filePath);
core.error("AVSC file specified is not valid/parseable JSON: " + filePath + "\n " + err.toString());
continue;
}
var avroType;
try {
avroType = avro.parse(avroSchemaJson);
} catch (err) {
filePathsWithErrors.add(filePath);
core.error("AVSC file specified is not valid/parseable: " + filePath + "\n " + err.toString());
continue;
}
if (options.undocumentedCheck) {
const undocumentedFields = getUndocumentedFields(avroType.getName(), avroSchemaJson);
if (undocumentedFields.length > 0) {
filePathsWithErrors.add(filePath);
const errorMessage = `Invalid Schema at '${filePath}'! The following fields are not documented:`;
core.error(errorMessage.concat("\n ", ...undocumentedFields.join("\n ")));
}
}
if (options.complexUnionCheck) {
const complexUnionFields = getComplexUnionFields(avroType.getName(), avroSchemaJson);
if (complexUnionFields.length > 0) {
filePathsWithErrors.add(filePath);
const errorMessage = `Invalid Schema at '${filePath}'! The following fields are or contain complex unions:`;
core.error(errorMessage.concat("\n ", ...complexUnionFields.join("\n ")));
}
}
}
if (filePathsWithErrors.size > 0) {
const errorMessage = "Validation failed for the following files:";
throw new Error(errorMessage.concat("\n ", ...Array.from(filePathsWithErrors.keys()).join("\n ")));
}
resolve("done!");
});
};
let isOrContainsRecord = function(field) {
if (Array.isArray(field.type)) {
// Check UNION type for RECORD type
return field.type.filter(x => x.type?.toUpperCase() === "RECORD").length > 0;
}
const upperCaseFieldType = field.type.type?.toUpperCase();
if (upperCaseFieldType === "RECORD") {
return true;
} else if (upperCaseFieldType === "ARRAY" && field.type.items?.type?.toUpperCase() === "RECORD") {
return true;
} else if (upperCaseFieldType === "MAP" && field.type.values?.type?.toUpperCase() === "RECORD") {
return true;
}
return false;
}
let isUnionType = function(type) {
return Array.isArray(type);
}
let getRecordSchema = function(field) {
if (isUnionType(field.type)) {
// Extract UNION type RECORD schema
return field.type.filter(x => x.type?.toUpperCase() === "RECORD")[0];
}
const upperCaseFieldType = field.type.type?.toUpperCase();
if (upperCaseFieldType === "RECORD") {
return field.type;
} else if (upperCaseFieldType === "ARRAY" && field.type.items?.type?.toUpperCase() === "RECORD") {
return field.type.items;
} else if (upperCaseFieldType === "MAP" && field.type.values?.type?.toUpperCase() === "RECORD") {
return field.type.values;
}
}
let getUndocumentedFields = function(pathPrefix, avroSchemaJson) {
const undocumentedFields = [];
for (const field of avroSchemaJson.fields) {
if (field.doc == null || field.doc.trim() === '') {
undocumentedFields.push(pathPrefix + '.' + field.name);
}
if (isOrContainsRecord(field)) {
undocumentedFields.push(...getUndocumentedFields(pathPrefix + '.' + field.name, getRecordSchema(field)));
}
}
return undocumentedFields;
}
let getUnionSchema = function(field) {
if (isUnionType(field.type)) {
return field.type;
}
const upperCaseFieldType = field.type.type?.toUpperCase();
if (upperCaseFieldType === "ARRAY" && isUnionType(field.type.items)) {
return field.type.items;
} else if (upperCaseFieldType === "MAP" && isUnionType(field.type.values)) {
return field.type.values;
}
}
let isComplexUnion = function(field) {
const unionSchema = getUnionSchema(field);
if (!unionSchema) {
return false;
}
if (unionSchema.length == 2 && unionSchema[0].toUpperCase() === "NULL") {
return false;
}
return true;
}
let getComplexUnionFields = function(pathPrefix, avroSchemaJson) {
const complexUnionFields = [];
for (const field of avroSchemaJson.fields) {
if (isComplexUnion(field)) {
complexUnionFields.push(pathPrefix + '.' + field.name);
}
if (isOrContainsRecord(field)) {
complexUnionFields.push(...getComplexUnionFields(pathPrefix + '.' + field.name, getRecordSchema(field)));
}
}
return complexUnionFields;
}
module.exports = avrolint;