fix: enforce report contract in production validator
Reportist CI Reportist failed CI.
Beffroi/reportist/pipeline/head There was a failure building this commit Details

This commit is contained in:
Dandaedre 2026-09-16 00:31:29 +02:00
parent ec23fb4cf0
commit 76ef076089
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
5 changed files with 41 additions and 3 deletions

View File

@ -109,6 +109,16 @@ class ReportContractTests(unittest.TestCase):
str(ROOT / "examples/example-report.yaml")], cwd=temp, text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
def test_production_validator_rejects_contract_extensions(self):
with tempfile.TemporaryDirectory() as temp:
document = copy.deepcopy(self.document)
document["collect"] = {}
path = Path(temp) / "invalid.json"
path.write_text(json.dumps(document), encoding="utf-8")
result = subprocess.run(["bash", str(ROOT / "tools/validate.sh"), str(path)],
text=True, capture_output=True)
self.assertEqual(result.returncode, 1)
if __name__ == "__main__":
unittest.main()

View File

@ -20,6 +20,7 @@ case $input in *.json) format=json;; *.yaml|*.yml) format=yaml;; *) fail "expect
command -v "$typst_command" >/dev/null 2>&1 || fail "Typst executable not found: $typst_command"
input=$(realpath "$input")
[[ -f "$input" ]] || fail "$input: report does not exist"
"$root/tools/validate.sh" "$input" >/dev/null || fail "invalid report"
if [[ -z "$output" ]]; then output=${input%.*}.pdf; fi
output=$(realpath -m "$output")
[[ "$input" != "$output" ]] || fail "input and output paths must differ"

View File

@ -1,4 +1,5 @@
#let file = sys.inputs.at("report")
#let format = sys.inputs.at("format")
#let report = if format == "json" { json(file) } else { yaml(file) }
#metadata(report) <report-data>
#metadata(report.at("branding", default: (:)).at("logo", default: none)) <report-logo>

23
tools/validate.jq Normal file
View File

@ -0,0 +1,23 @@
def keys_allowed($allowed): type=="object" and ((keys-$allowed)|length)==0;
def text: type=="string" and test("\\S");
def portable: text and (startswith("/")|not) and (contains("\\")|not) and (contains(":")|not) and ([split("/")[]|select(.=="" or .=="." or .=="..")] | length)==0;
def strings: type=="array" and length>0 and all(.[];text);
def block:
keys_allowed(["type","text","items","title","kind","content"]) and
if .type=="text" or .type=="code" then keys_allowed(["type","text"]) and (.text|text)
elif .type=="bullets" or .type=="steps" then keys_allowed(["type","items"]) and (.items|strings)
elif .type=="callout" then . as $b | keys_allowed(["type","title","kind","text"]) and (.text|text) and ((.title//"x")|text) and (["info","low","medium","high","critical","warning","ok","success","unanalysed"]|index($b.kind//"info")!=null)
elif .type=="subsection" then keys_allowed(["type","title","content"]) and (.title|text) and (.content|type=="array" and length>0 and all(.[];block))
else false end;
def section: keys_allowed(["title","content"]) and (.title|text) and (.content|type=="array" and length>0 and all(.[];block));
def valid:
keys_allowed(["schema_version","title","subtitle","id","report_type","status","severity","metadata","branding","summary","closing","timeline","findings","sections","appendices","actions_title","actions","provenance"]) and
.schema_version==1 and (.title|text) and (.metadata|type=="object") and (.metadata.Date|type=="string" and test("^[0-9]{4}-[0-9]{2}-[0-9]{2}$")) and (.metadata.Author|text) and
((.report_type//"technical") as $v|["technical","briefing"]|index($v)!=null) and
((.severity//"info") as $v|["info","low","medium","high","critical","warning","ok","success","unanalysed"]|index($v)!=null) and
((.branding//{})|keys_allowed(["organization","logo","theme"]) and ((.organization//"x")|text) and ((.logo//"x")|portable) and
((.theme//{})|keys_allowed(["font","mono_font","ink","muted","line","paper_soft","accent","info","warning","critical","success"]) and
all(to_entries[]; if (.key=="font" or .key=="mono_font") then (.value|text) else (.value|type=="string" and test("^#[0-9a-fA-F]{6}$")) end))) and
((.sections//[])|type=="array" and all(.[];section)) and ((.appendices//[])|type=="array" and all(.[];section)) and
((.findings//[]) as $f | ($f|type)=="array" and all($f[];keys_allowed(["id","title","severity","status","evidence","assessment","evidence_refs"]) and (.title|text)) and ([$f[]|select(.id).id]|length)==([$f[]|select(.id).id]|unique|length));
valid

View File

@ -2,7 +2,10 @@
# Validate by compiling through the same Typst data and contract path as rendering.
set -euo pipefail
[[ $# -eq 1 ]] || { echo "Usage: $0 INPUT" >&2; exit 2; }
temp=$(mktemp -d "${TMPDIR:-/tmp}/reportist-validate.XXXXXXXX")
trap 'rm -rf "$temp"' EXIT
"$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)/render.sh" "$1" --output "$temp/report.pdf" >/dev/null
root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P); input=$(realpath "$1")
case $input in *.json) format=json;; *.yaml|*.yml) format=yaml;; *) echo "error: expected YAML or JSON" >&2; exit 1;; esac
temp=$(mktemp -d "${TMPDIR:-/tmp}/reportist-validate.XXXXXXXX"); trap 'rm -rf "$temp"' EXIT
cp "$input" "$temp/report.$format"; cp "$root/tools/report-value.typ" "$temp/report-value.typ"
typst query --root "$temp" --input "report=report.$format" --input "format=$format" "$temp/report-value.typ" '<report-data>' --field value --one >"$temp/parsed.json" 2>/dev/null || { echo "error: unable to parse report" >&2; exit 1; }
jq -e -f "$root/tools/validate.jq" "$temp/parsed.json" >/dev/null || { echo "error: invalid report v1" >&2; exit 1; }
echo "Valid report v1: $1"