Files
proxmox/scripts/validate/validate-ura-policy-profiles.mjs

66 lines
2.4 KiB
JavaScript

#!/usr/bin/env node
/**
* Validate config/universal-resource-activation/policy-profiles.json against
* universal-resource-activation.policy-profile-registry.v1.schema.json
* and ensure manifest policyProfileRefs[] ids exist in the registry.
*
* Usage: from repo root — node scripts/validate/validate-ura-policy-profiles.mjs
*/
import { readFileSync, existsSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const projectRoot = path.join(__dirname, '../..');
const registryPath = path.join(projectRoot, 'config', 'universal-resource-activation', 'policy-profiles.json');
const registrySchemaPath = path.join(
projectRoot,
'config',
'universal-resource-activation.policy-profile-registry.v1.schema.json',
);
const manifestPath = path.join(projectRoot, 'config', 'universal-resource-activation', 'manifest.json');
function fail(msg) {
console.error(`[validate-ura-profiles] ${msg}`);
process.exit(1);
}
if (!existsSync(registryPath)) fail(`Missing ${registryPath}`);
if (!existsSync(registrySchemaPath)) fail(`Missing ${registrySchemaPath}`);
const ajv = new Ajv({ allErrors: true, strict: false, validateSchema: false });
addFormats(ajv);
const registrySchema = JSON.parse(readFileSync(registrySchemaPath, 'utf8'));
const validateRegistry = ajv.compile(registrySchema);
const registry = JSON.parse(readFileSync(registryPath, 'utf8'));
if (!validateRegistry(registry)) {
console.error('[validate-ura-profiles] policy-profiles.json failed schema:', validateRegistry.errors);
process.exit(1);
}
const ids = new Set(registry.profiles.map((p) => p.policyProfileId));
console.log(`[validate-ura-profiles] OK: ${ids.size} profile(s) in registry`);
if (existsSync(manifestPath)) {
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
const refs = manifest.policyProfileRefs || [];
for (const r of refs) {
const pid = r.id;
if (!pid || !ids.has(pid)) {
fail(`manifest policyProfileRefs contains unknown or missing id: "${pid}"`);
}
}
for (const res of manifest.resources || []) {
const pid = res.policyProfileId;
if (pid && !ids.has(pid)) {
fail(`resource ${res.resourceId} references unknown policyProfileId: "${pid}"`);
}
}
console.log('[validate-ura-profiles] OK: manifest refs match registry');
}