53 lines
1.6 KiB
JavaScript
53 lines
1.6 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Validate omnl-ledger-mapping.v1.json against omnl-ledger-mapping.v1.schema.json
|
|
* Usage: node scripts/validate/validate-omnl-ledger-mapping.mjs [path]
|
|
*/
|
|
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 defaultPath = path.join(
|
|
projectRoot,
|
|
'config/universal-resource-activation/integration/omnl-ledger-mapping.v1.example.json'
|
|
);
|
|
const schemaPath = path.join(
|
|
projectRoot,
|
|
'config/universal-resource-activation/integration/omnl-ledger-mapping.v1.schema.json'
|
|
);
|
|
|
|
const fileArg = process.argv.slice(2).filter((a) => a !== '--')[0];
|
|
const file = path.resolve(projectRoot, fileArg || defaultPath);
|
|
|
|
if (!existsSync(file)) {
|
|
console.error(`[validate-ledger-mapping] Missing ${file}`);
|
|
process.exit(1);
|
|
}
|
|
if (!existsSync(schemaPath)) {
|
|
console.error(`[validate-ledger-mapping] Missing schema ${schemaPath}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const ajv = new Ajv({ allErrors: true, strict: false, validateSchema: false });
|
|
addFormats(ajv);
|
|
const validate = ajv.compile(JSON.parse(readFileSync(schemaPath, 'utf8')));
|
|
let data;
|
|
try {
|
|
data = JSON.parse(readFileSync(file, 'utf8'));
|
|
} catch (e) {
|
|
console.error(`[validate-ledger-mapping] Invalid JSON: ${e.message}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
if (!validate(data)) {
|
|
console.error(`[validate-ledger-mapping] FAIL: ${ajv.errorsText(validate.errors, { separator: '\n' })}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`[validate-ledger-mapping] OK: ${file}`);
|
|
process.exit(0);
|