Skip to main content

validate/
validate.rs

1//! Load a YAML contract, validate one document against it and print the
2//! findings. Run it with `cargo run --example validate`.
3
4use markdown_contract::{FindingLevel, load_contract, validate};
5
6const CONTRACT: &str = "\
7mcVersion: 2
8kind: contract
9frontmatter:
10  type: object
11  required: [status]
12  properties:
13    status: { enum: [open, accepted] }
14body:
15  order: strict
16  additionalSections: false
17  sections:
18    - section: Context
19    - section: Decision
20";
21
22const DOCUMENT: &str = "\
23---
24status: done
25---
26
27## Context
28
29Config is checked by hand.
30";
31
32fn main() {
33    let contract = load_contract(CONTRACT).expect("the contract compiles");
34    let findings = validate(DOCUMENT, &contract, "decisions/zod.md");
35
36    for f in &findings {
37        let line = f.pos.map_or(0, |p| p.line);
38        println!("{}:{line} {:?} {}: {}", f.path, f.level, f.id, f.message);
39    }
40
41    // `status: done` is not in the enum and the Decision section is missing.
42    let errors = findings
43        .iter()
44        .filter(|f| f.level == FindingLevel::Error)
45        .count();
46    assert_eq!(errors, 2);
47}