Repository navigation
Expand file tree
/
Copy pathvalidate.mjs
More file actions
47 lines (42 loc) · 2.11 KB
/
Copy pathvalidate.mjs
File metadata and controls
47 lines (42 loc) · 2.11 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
#!/usr/bin/env node
// Checks the rules a JSON Schema cannot express. No dependencies; Node 18 or newer.
// node scripts/validate.mjs
// The schema checks run in CI with ajv (see .github/workflows/ci.yml).
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const root = join(dirname(fileURLToPath(import.meta.url)), '..');
const read = (f) => JSON.parse(readFileSync(join(root, f), 'utf8'));
const catalog = read('catalog.json');
const { licenses } = read('licenses.json');
const errors = [];
const fail = (msg) => errors.push(msg);
const byLicence = new Map(licenses.map((l) => [l.id, l]));
if (byLicence.size !== licenses.length) fail('licenses.json: duplicate licence id');
// Every id and alias resolves to one model, compared case-insensitively (the API matches this way).
const names = new Map();
const usedLicences = new Set();
for (const m of catalog.models) {
for (const key of [m.id, ...m.aliases]) {
const k = key.toLowerCase();
const prev = names.get(k);
if (prev !== undefined && prev !== m.id) fail(`"${key}" is claimed by both ${prev} and ${m.id}`);
names.set(k, m.id);
}
if (m.owned_by !== m.id.split('/')[0]) fail(`${m.id}: owned_by must be "${m.id.split('/')[0]}"`);
const lic = byLicence.get(m.license);
if (!lic) {
fail(`${m.id}: licence "${m.license}" is not in licenses.json`);
continue;
}
usedLicences.add(m.license);
if (lic.license_class !== m.license_class) fail(`${m.id}: license_class "${m.license_class}" differs from licenses.json ("${lic.license_class}")`);
if (m.open_weight !== (m.license !== 'proprietary')) fail(`${m.id}: open_weight must be ${m.license !== 'proprietary'}`);
if (m.servable !== (m.license_class !== 'not_allowed')) fail(`${m.id}: servable must be ${m.license_class !== 'not_allowed'}`);
}
for (const l of licenses) if (!usedLicences.has(l.id)) fail(`licenses.json: "${l.id}" is not used by any model`);
if (errors.length) {
for (const e of errors) console.error(`error: ${e}`);
process.exit(1);
}
console.log(`ok: ${catalog.models.length} models, ${licenses.length} licences`);