-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate-server-json.mjs
More file actions
69 lines (59 loc) · 2.24 KB
/
validate-server-json.mjs
File metadata and controls
69 lines (59 loc) · 2.24 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
#!/usr/bin/env node
import Ajv from 'ajv';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
async function validateServerJson() {
try {
// Read server.json
const serverJsonPath = join(__dirname, 'server.json');
const serverJson = JSON.parse(readFileSync(serverJsonPath, 'utf-8'));
console.log('📄 Loaded server.json');
console.log(` Schema: ${serverJson.$schema}`);
console.log(` Name: ${serverJson.name}`);
console.log(` Version: ${serverJson.version}\n`);
// Fetch the schema
console.log('⬇️ Fetching schema...');
const schemaUrl = serverJson.$schema;
const schemaResponse = await fetch(schemaUrl);
if (!schemaResponse.ok) {
throw new Error(`Failed to fetch schema: ${schemaResponse.status} ${schemaResponse.statusText}`);
}
const schema = await schemaResponse.json();
console.log('✅ Schema fetched successfully\n');
// Validate
console.log('🔍 Validating server.json against schema...');
const ajv = new Ajv({ allErrors: true, strict: false });
const validate = ajv.compile(schema);
const valid = validate(serverJson);
if (valid) {
console.log('✅ server.json is valid!\n');
console.log('📦 Package configuration:');
serverJson.packages.forEach((pkg, idx) => {
console.log(` ${idx + 1}. ${pkg.registryType}: ${pkg.identifier}@${pkg.version}`);
console.log(` Transport: ${pkg.transport.type}`);
});
console.log('');
return true;
} else {
console.error('❌ Validation failed!\n');
console.error('Errors:');
validate.errors.forEach((error, idx) => {
console.error(` ${idx + 1}. ${error.instancePath || '/'}: ${error.message}`);
if (error.params) {
console.error(` Details: ${JSON.stringify(error.params)}`);
}
});
console.error('');
return false;
}
} catch (error) {
console.error('❌ Error during validation:', error.message);
return false;
}
}
// Run validation
const isValid = await validateServerJson();
process.exit(isValid ? 0 : 1);