feat: bhl report validate по схемам отчёта аудита (BHL-023)
- packages/bhl/src/report.ts: схема audit-report + ссылки на требования + сверка сводки - EXEMPT без exemption_ref и отчёт вне схемы отклоняются с диагностикой - пакет: 24/24 тестов
This commit is contained in:
+64
-1
@@ -1,11 +1,13 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { emit, ExitCode, failEnvelope, okEnvelope } from './envelope.ts';
|
||||
import { resolveProfiles } from './resolve.ts';
|
||||
import { validateReport } from './report.ts';
|
||||
import { validateSpec } from './validate.ts';
|
||||
|
||||
const USAGE_MESSAGE =
|
||||
'Usage: bhl <version|validate|spec|profile|report|audit> [args]; ' +
|
||||
'profile resolve <spec-root> [--profile <id>] [--preset <id>]; ' +
|
||||
'report validate <report.json> [--spec-root <path>]; ' +
|
||||
'JSON envelope on stdout; exit codes: 0 ok, 1 validation failed, 2 usage, ' +
|
||||
'3 not implemented, 4 internal.';
|
||||
|
||||
@@ -138,6 +140,64 @@ function runProfile(command: string, rest: readonly string[]): number {
|
||||
return ExitCode.ValidationFailed;
|
||||
}
|
||||
|
||||
function parseReportArgs(
|
||||
args: readonly string[],
|
||||
): { file: string; specRoot: string } | { error: string } {
|
||||
let specRoot = 'spec';
|
||||
const positional: string[] = [];
|
||||
let cursor = 0;
|
||||
while (cursor < args.length) {
|
||||
const arg = args[cursor] ?? '';
|
||||
if (arg === '--spec-root') {
|
||||
const value = args[cursor + 1];
|
||||
if (value === undefined) {
|
||||
return { error: 'missing value for --spec-root' };
|
||||
}
|
||||
specRoot = value;
|
||||
cursor += 2;
|
||||
continue;
|
||||
}
|
||||
if (arg.startsWith('--')) {
|
||||
return { error: `unknown flag ${arg}` };
|
||||
}
|
||||
positional.push(arg);
|
||||
cursor += 1;
|
||||
}
|
||||
if (positional.length !== 1) {
|
||||
return { error: 'report validate requires exactly one report file' };
|
||||
}
|
||||
return { file: positional[0] ?? '', specRoot };
|
||||
}
|
||||
|
||||
function runReport(command: string, rest: readonly string[]): number {
|
||||
const [sub, ...args] = rest;
|
||||
if (sub !== 'validate') {
|
||||
emit(failEnvelope(command, 'USAGE', 'report requires the subcommand validate'));
|
||||
return ExitCode.Usage;
|
||||
}
|
||||
const parsed = parseReportArgs(args);
|
||||
if ('error' in parsed) {
|
||||
emit(failEnvelope(command, 'USAGE', parsed.error));
|
||||
return ExitCode.Usage;
|
||||
}
|
||||
const result = validateReport(parsed.file, parsed.specRoot);
|
||||
const data = { file: parsed.file, spec_root: parsed.specRoot, errors: result.diagnostics };
|
||||
if (result.ok) {
|
||||
emit(okEnvelope(command, data));
|
||||
return ExitCode.Ok;
|
||||
}
|
||||
emit({
|
||||
ok: false,
|
||||
command,
|
||||
data,
|
||||
error: {
|
||||
code: 'VALIDATION_FAILED',
|
||||
message: `${String(result.diagnostics.length)} diagnostic(s)`,
|
||||
},
|
||||
});
|
||||
return ExitCode.ValidationFailed;
|
||||
}
|
||||
|
||||
function runValidate(command: string, rest: readonly string[]): number {
|
||||
const [root] = rest;
|
||||
if (root === undefined || root.startsWith('--')) {
|
||||
@@ -179,7 +239,10 @@ export function run(argv: readonly string[]): number {
|
||||
if (command === 'validate') {
|
||||
return runValidate(command, rest);
|
||||
}
|
||||
if (command === 'spec' || command === 'report' || command === 'audit') {
|
||||
if (command === 'report') {
|
||||
return runReport(command, rest);
|
||||
}
|
||||
if (command === 'spec' || command === 'audit') {
|
||||
emit(failEnvelope(command, 'NOT_IMPLEMENTED', `${command} is not implemented yet`));
|
||||
return ExitCode.NotImplemented;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user