reportist/tools/validate.typ

71 lines
4.7 KiB
Plaintext

#let file = sys.inputs.at("report")
#let format = sys.inputs.at("format")
#let report = if format == "json" { json(file) } else { yaml(file) }
#let nonempty(value) = type(value) == str and value.match(regex("\\S")) != none
#let keys-allowed(value, allowed) = type(value) == dictionary and value.keys().all(key => key in allowed)
#let portable(value) = (
nonempty(value) and
not value.starts-with("/") and
not value.contains("\\") and
not value.contains(":") and
value.split("/").all(part => part not in ("", ".", ".."))
)
#let values = ("info", "low", "medium", "high", "critical", "warning", "ok", "success", "unanalysed")
#let validate-block(item) = {
assert(type(item) == dictionary, message: "content block must be an object")
let kind = item.at("type", default: none)
if kind in ("text", "code") {
assert(keys-allowed(item, ("type", "text")) and nonempty(item.at("text", default: none)), message: "invalid text/code block")
} else if kind in ("bullets", "steps") {
let items = item.at("items", default: none)
assert(keys-allowed(item, ("type", "items")) and type(items) == array and items.len() > 0 and items.all(nonempty), message: "invalid list block")
} else if kind == "callout" {
assert(keys-allowed(item, ("type", "title", "kind", "text")) and nonempty(item.at("text", default: none)) and
nonempty(item.at("title", default: "x")) and item.at("kind", default: "info") in values, message: "invalid callout block")
} else if kind == "subsection" {
let content = item.at("content", default: none)
assert(keys-allowed(item, ("type", "title", "content")) and nonempty(item.at("title", default: none)) and
type(content) == array and content.len() > 0, message: "invalid subsection block")
for child in content { validate-block(child) }
} else { assert(false, message: "unsupported content block") }
}
#let validate-section(section) = {
assert(keys-allowed(section, ("title", "content")) and nonempty(section.at("title", default: none)), message: "invalid section")
let content = section.at("content", default: none)
assert(type(content) == array and content.len() > 0, message: "section content must not be empty")
for item in content { validate-block(item) }
}
#assert(keys-allowed(report, ("schema_version", "title", "subtitle", "id", "report_type", "status", "severity", "metadata", "branding", "summary", "closing", "timeline", "findings", "sections", "appendices", "actions_title", "actions", "provenance")), message: "unsupported report field")
#assert(report.at("schema_version", default: none) == 1, message: "unsupported or missing schema_version")
#assert(nonempty(report.at("title", default: none)), message: "title must be a non-empty string")
#let report-metadata = report.at("metadata", default: none)
#assert(type(report-metadata) == dictionary and nonempty(report-metadata.at("Date", default: none)) and nonempty(report-metadata.at("Author", default: none)), message: "metadata.Date and metadata.Author are required")
#assert(report-metadata.Date.match(regex("^[0-9]{4}-[0-9]{2}-[0-9]{2}$")) != none, message: "metadata.Date must use YYYY-MM-DD")
#assert(report.at("report_type", default: "technical") in ("technical", "briefing"), message: "unsupported report_type")
#assert(report.at("severity", default: "info") in values, message: "unsupported severity")
#let branding = report.at("branding", default: (:))
#assert(keys-allowed(branding, ("organization", "logo", "theme")), message: "invalid branding")
#assert(nonempty(branding.at("organization", default: "x")), message: "invalid branding organization")
#assert(portable(branding.at("logo", default: "x")), message: "invalid branding logo path")
#let theme = branding.at("theme", default: (:))
#assert(keys-allowed(theme, ("font", "mono_font", "ink", "muted", "line", "paper_soft", "accent", "info", "warning", "critical", "success")), message: "invalid theme field")
#for pair in theme.pairs() {
if pair.first() in ("font", "mono_font") { assert(nonempty(pair.last()), message: "invalid theme font") }
else { assert(type(pair.last()) == str and pair.last().match(regex("^#[0-9a-fA-F]{6}$")) != none, message: "invalid theme color") }
}
#for section in report.at("sections", default: ()) { validate-section(section) }
#for section in report.at("appendices", default: ()) { validate-section(section) }
#let findings = report.at("findings", default: ())
#let finding-ids = findings.filter(item => "id" in item).map(item => item.id)
#assert(finding-ids.dedup().len() == finding-ids.len(), message: "duplicate finding ID")
#for finding in findings {
assert(keys-allowed(finding, ("id", "title", "severity", "status", "evidence", "assessment", "evidence_refs")) and nonempty(finding.at("title", default: none)), message: "invalid finding")
}
#metadata(true) <validated>