-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathvalidation.js
More file actions
373 lines (321 loc) · 10.9 KB
/
validation.js
File metadata and controls
373 lines (321 loc) · 10.9 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const fs = require('fs');
const path = require('path');
// Constants for validation rules
const MAX_LENGTHS = {
authority: 40,
category: 30
};
const VALID_COUNTRIES = [
'uv', 'eu', 'us', 'at', 'au', 'be', 'br', 'ca', 'ch', 'de', 'dk', 'pl',
'fi', 'fr', 'gb', 'in', 'it', 'jp', 'kr', 'nl', 'no', 'nz', 'se', 'tw', 'es', 'bd',
'cr', 'uz', 'cz'
];
const VALID_PRODUCTS = ['fhir', 'cda', 'v2', 'openehr'];
class ValidationError extends Error {
constructor(message, file = null, guide = null) {
super(message);
this.file = file;
this.guide = guide;
}
}
class Validator {
constructor() {
this.errors = [];
}
addError(message, file = null, guide = null) {
this.errors.push(new ValidationError(message, file, guide));
}
// Validate that a file contains valid JSON
validateJsonFile(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf8');
JSON.parse(content);
console.log(`✓ ${filePath} is valid JSON`);
return JSON.parse(content);
} catch (error) {
this.addError(`Invalid JSON in ${filePath}: ${error.message}`, filePath);
return null;
}
}
// Check for duplicate values in an array
findDuplicates(array, fieldName) {
const seen = new Set();
const duplicates = [];
for (const item of array) {
if (seen.has(item)) {
duplicates.push(item);
} else {
seen.add(item);
}
}
return duplicates;
}
// Validate string field length
validateLength(value, maxLength, fieldName, guideName) {
if (typeof value === 'string' && value.length > maxLength) {
this.addError(
`${fieldName} exceeds maximum length of ${maxLength} characters: "${value}" (${value.length} chars)`,
'fhir-ig-list.json',
guideName
);
}
}
// Check for placeholder values
validateNotPlaceholder(value, fieldName, guideName, fileName = 'fhir-ig-list.json') {
if (value === '??') {
this.addError(
`${fieldName} cannot be '??' - please provide a valid value`,
fileName,
guideName
);
}
}
// Recursively check for '??' in any object/array
validateNoPlaceholders(obj, path = '', guideName = null, fileName = 'fhir-ig-list.json') {
if (obj === '??') {
this.addError(
`Property at ${path || 'root'} cannot be '??' - please provide a valid value`,
fileName,
guideName
);
return;
}
if (typeof obj === 'object' && obj !== null) {
if (Array.isArray(obj)) {
obj.forEach((item, index) => {
this.validateNoPlaceholders(item, `${path}[${index}]`, guideName, fileName);
});
} else {
Object.keys(obj).forEach(key => {
const newPath = path ? `${path}.${key}` : key;
this.validateNoPlaceholders(obj[key], newPath, guideName, fileName);
});
}
}
}
// Validate that URLs don't end with /
validateUrlFormat(url, fieldName, guideName) {
if (typeof url === 'string' && url.endsWith('/')) {
this.addError(
`${fieldName} must not end with '/': ${url}`,
'fhir-ig-list.json',
guideName
);
}
}
// Validate fhir-ig-list.json
validateFhirIgList(data) {
if (!data || !data.guides || !Array.isArray(data.guides)) {
this.addError('fhir-ig-list.json must contain a "guides" array', 'fhir-ig-list.json');
return;
}
// Check for '??' placeholders in the entire structure
this.validateNoPlaceholders(data, '', null, 'fhir-ig-list.json');
const npmNames = [];
data.guides.forEach((guide, index) => {
const guideName = guide.name || `Guide #${index + 1}`;
// Check required fields
const requiredFields = ['npm-name', 'name', 'category', 'description', 'authority', 'country'];
requiredFields.forEach(field => {
if (!guide[field]) {
this.addError(`Missing required field: ${field}`, 'fhir-ig-list.json', guideName);
} else {
// Check for placeholder values in required fields
this.validateNotPlaceholder(guide[field], field, guideName);
}
});
// Validate field lengths
this.validateLength(guide.authority, MAX_LENGTHS.authority, 'authority', guideName);
this.validateLength(guide.category, MAX_LENGTHS.category, 'category', guideName);
this.validateLength(guide.name, MAX_LENGTHS.name, 'name', guideName);
// Collect npm-names for uniqueness check
if (guide['npm-name']) {
npmNames.push(guide['npm-name']);
}
// Validate country
if (guide.country && !VALID_COUNTRIES.includes(guide.country)) {
this.addError(
`Invalid country code: ${guide.country}. Must be one of: ${VALID_COUNTRIES.join(', ')}`,
'fhir-ig-list.json',
guideName
);
}
// Validate products
if (guide.product && Array.isArray(guide.product)) {
const duplicateProducts = this.findDuplicates(guide.product, 'product');
if (duplicateProducts.length > 0) {
this.addError(
`Duplicate products found: ${duplicateProducts.join(', ')}`,
'fhir-ig-list.json',
guideName
);
}
guide.product.forEach(product => {
if (!VALID_PRODUCTS.includes(product.toLowerCase())) {
this.addError(
`Invalid product: ${product}. Must be one of: ${VALID_PRODUCTS.join(', ')}`,
'fhir-ig-list.json',
guideName
);
}
});
}
// Validate language array for duplicates
if (guide.language && Array.isArray(guide.language)) {
const duplicateLanguages = this.findDuplicates(guide.language, 'language');
if (duplicateLanguages.length > 0) {
this.addError(
`Duplicate languages found: ${duplicateLanguages.join(', ')}`,
'fhir-ig-list.json',
guideName
);
}
}
// Validate URL formats
['history', 'canonical', 'ci-build'].forEach(field => {
if (guide[field]) {
this.validateUrlFormat(guide[field], field, guideName);
}
});
// Validate editions
if (guide.editions && Array.isArray(guide.editions)) {
const igVersions = [];
guide.editions.forEach((edition, editionIndex) => {
// Collect ig-versions for uniqueness check
if (edition['ig-version']) {
igVersions.push(edition['ig-version']);
}
// Validate edition URL format
if (edition.url) {
this.validateUrlFormat(edition.url, `editions[${editionIndex}].url`, guideName);
}
});
// Check for duplicate ig-versions
const duplicateVersions = this.findDuplicates(igVersions, 'ig-version');
if (duplicateVersions.length > 0) {
this.addError(
`Duplicate ig-versions found in editions: ${duplicateVersions.join(', ')}`,
'fhir-ig-list.json',
guideName
);
}
}
});
// Check for duplicate npm-names
const duplicateNpmNames = this.findDuplicates(npmNames, 'npm-name');
if (duplicateNpmNames.length > 0) {
this.addError(
`Duplicate npm-names found: ${duplicateNpmNames.join(', ')}`,
'fhir-ig-list.json'
);
}
// Validate replacedBy references
const npmNameSet = new Set(npmNames);
data.guides.forEach((guide, index) => {
if (guide.replacedBy) {
const guideName = guide.name || `Guide #${index + 1}`;
if (guide.replacedBy === guide['npm-name']) {
this.addError(
`replacedBy cannot refer to itself: "${guide.replacedBy}"`,
'fhir-ig-list.json',
guideName
);
} else if (!npmNameSet.has(guide.replacedBy)) {
this.addError(
`replacedBy refers to unknown npm-name: "${guide.replacedBy}"`,
'fhir-ig-list.json',
guideName
);
}
}
});
}
// Validate package-feeds.json
validatePackageFeeds(data) {
if (!data || !data.feeds || !Array.isArray(data.feeds)) {
this.addError('package-feeds.json must contain a "feeds" array', 'package-feeds.json');
return;
}
// Check for '??' placeholders in the entire structure
this.validateNoPlaceholders(data, '', null, 'package-feeds.json');
const feedUrls = [];
data.feeds.forEach((feed, index) => {
if (feed.url) {
feedUrls.push(feed.url);
} else {
this.addError(`Feed #${index + 1} is missing required "url" field`, 'package-feeds.json');
}
});
// Check for duplicate feed URLs
const duplicateUrls = this.findDuplicates(feedUrls, 'url');
if (duplicateUrls.length > 0) {
this.addError(
`Duplicate feed URLs found: ${duplicateUrls.join(', ')}`,
'package-feeds.json'
);
}
}
// Get list of changed JSON files (for CI environment)
getChangedJsonFiles() {
const changedFiles = process.env.CHANGED_FILES;
if (changedFiles) {
return changedFiles.split('\n').filter(file => file.endsWith('.json'));
}
// Fallback: validate known JSON files
return ['fhir-ig-list.json', 'package-feeds.json'].filter(file => fs.existsSync(file));
}
// Main validation function
validate() {
console.log('Starting validation...\n');
const changedJsonFiles = this.getChangedJsonFiles();
// Validate JSON syntax for all changed JSON files
const validatedFiles = {};
changedJsonFiles.forEach(file => {
if (fs.existsSync(file)) {
const data = this.validateJsonFile(file);
if (data) {
validatedFiles[file] = data;
}
}
});
// Specific validations for known files
if (validatedFiles['fhir-ig-list.json']) {
console.log('\nValidating fhir-ig-list.json...');
this.validateFhirIgList(validatedFiles['fhir-ig-list.json']);
}
if (validatedFiles['package-feeds.json']) {
console.log('\nValidating package-feeds.json...');
this.validatePackageFeeds(validatedFiles['package-feeds.json']);
}
// Report results
this.reportResults();
}
reportResults() {
console.log('\n' + '='.repeat(50));
console.log('VALIDATION RESULTS');
console.log('='.repeat(50));
if (this.errors.length === 0) {
console.log('✅ All validations passed!');
process.exit(0);
} else {
console.log(`❌ Found ${this.errors.length} validation error(s):\n`);
this.errors.forEach((error, index) => {
console.log(`${index + 1}. ${error.message}`);
if (error.file) {
console.log(` File: ${error.file}`);
}
if (error.guide) {
console.log(` Guide: ${error.guide}`);
}
console.log('');
});
process.exit(1);
}
}
}
// Run validation if this script is executed directly
if (require.main === module) {
const validator = new Validator();
validator.validate();
}
module.exports = Validator;