nodex-core 0.2.2

Universal graph-based document tool — core library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
use chrono::NaiveDate;
use serde_json::Value;

use crate::config::{Config, FieldType, WhenPredicate, parse_when};
use crate::model::{Graph, Node};

use super::{Rule, Severity, Violation};

/// Check that nodes have all required frontmatter fields.
pub struct RequiredFieldRule;

impl Rule for RequiredFieldRule {
    fn id(&self) -> &str {
        "required_field"
    }

    fn severity(&self) -> Severity {
        Severity::Error
    }

    fn check(&self, graph: &Graph, config: &Config) -> Vec<Violation> {
        let mut violations = Vec::new();

        for node in graph.nodes().values() {
            let required = config.required_for(node.kind.as_str());

            for field in required {
                if is_field_missing(node, field) {
                    violations.push(Violation {
                        rule_id: self.id().to_string(),
                        severity: self.severity(),
                        node_id: Some(node.id.clone()),
                        path: Some(node.path.to_string_lossy().to_string()),
                        message: format!("missing required field: {field}"),
                    });
                }
            }
        }

        violations
    }
}

/// Check that `attrs` field values conform to configured types.
///
/// Built-in fields (`status`, `created`, etc.) are strongly typed in `Node`
/// so the parser catches their type errors. This rule targets
/// project-specific frontmatter keys that land in `Node::attrs` as
/// `serde_json::Value`.
pub struct FieldTypeRule;

impl Rule for FieldTypeRule {
    fn id(&self) -> &str {
        "field_type"
    }

    fn severity(&self) -> Severity {
        Severity::Error
    }

    fn check(&self, graph: &Graph, config: &Config) -> Vec<Violation> {
        let mut violations = Vec::new();

        for node in graph.nodes().values() {
            let types = config.types_for(node.kind.as_str());
            if types.is_empty() {
                continue;
            }

            for (field, expected) in &types {
                let Some(value) = node.attrs.get(field) else {
                    continue; // missing fields belong to `required_field`
                };
                if let Some(msg) = validate_type(value, *expected) {
                    violations.push(Violation {
                        rule_id: self.id().to_string(),
                        severity: self.severity(),
                        node_id: Some(node.id.clone()),
                        path: Some(node.path.to_string_lossy().to_string()),
                        message: format!("field {field:?}: {msg}"),
                    });
                }
            }
        }

        violations
    }
}

/// Check that field values are members of the configured enumeration.
///
/// Handles project-specific fields declared under
/// `schema.enums` / `schema.overrides.enums` AND the two built-in
/// scalar fields (`kind`, `status`) which are implicitly constrained
/// by the global `kinds.allowed` / `statuses.allowed`. An override
/// enum on `kind` or `status` supersedes the implicit backstop — the
/// override is always a subset of the global (`Config::validate`
/// enforces that), so the stricter rule wins without drift.
pub struct FieldEnumRule;

impl Rule for FieldEnumRule {
    fn id(&self) -> &str {
        "field_enum"
    }

    fn severity(&self) -> Severity {
        Severity::Error
    }

    fn check(&self, graph: &Graph, config: &Config) -> Vec<Violation> {
        let mut violations = Vec::new();

        for node in graph.nodes().values() {
            let mut enums = config.enums_for(node.kind.as_str());

            // Back-fill kind/status with the global allowed lists when no
            // explicit enum was declared for them. Declaring `kinds.allowed`
            // in `nodex.toml` must mean "these and only these kinds are
            // valid"; silently accepting out-of-vocabulary kinds/statuses
            // because the user didn't also write `schema.enums.kind = [...]`
            // would defeat the purpose of the allowed list.
            enums
                .entry("kind".to_string())
                .or_insert_with(|| config.kinds.allowed.clone());
            enums
                .entry("status".to_string())
                .or_insert_with(|| config.statuses.allowed.clone());

            for (field, allowed) in &enums {
                let actual = read_field_as_string(node, field);
                let Some(actual) = actual else {
                    continue; // missing fields belong to `required_field`
                };
                if !allowed.iter().any(|v| v == &actual) {
                    violations.push(Violation {
                        rule_id: self.id().to_string(),
                        severity: self.severity(),
                        node_id: Some(node.id.clone()),
                        path: Some(node.path.to_string_lossy().to_string()),
                        message: format!(
                            "field {field:?} has value {actual:?}; expected one of {allowed:?}"
                        ),
                    });
                }
            }
        }

        violations
    }
}

/// Check cross-field conditional requirements.
///
/// "When predicate holds, `require` field must be present."
pub struct CrossFieldRule;

impl Rule for CrossFieldRule {
    fn id(&self) -> &str {
        "cross_field"
    }

    fn severity(&self) -> Severity {
        Severity::Error
    }

    fn check(&self, graph: &Graph, config: &Config) -> Vec<Violation> {
        let mut violations = Vec::new();

        for node in graph.nodes().values() {
            let cross_fields = config.cross_field_for(node.kind.as_str());
            if cross_fields.is_empty() {
                continue;
            }

            for cf in &cross_fields {
                let Ok(predicate) = parse_when(&cf.when) else {
                    continue; // already rejected by Config::validate
                };
                if !predicate_matches_node(&predicate, node) {
                    continue;
                }
                if is_field_missing(node, &cf.require) {
                    violations.push(Violation {
                        rule_id: self.id().to_string(),
                        severity: self.severity(),
                        node_id: Some(node.id.clone()),
                        path: Some(node.path.to_string_lossy().to_string()),
                        message: format!("when {}, field {:?} is required", cf.when, cf.require),
                    });
                }
            }
        }

        violations
    }
}

// ─── helpers ────────────────────────────────────────────────────────────

/// Return true when `field` has no value on the node. Handles both
/// built-in scalar/vector fields and `attrs`.
fn is_field_missing(node: &Node, field: &str) -> bool {
    match field {
        "id" => node.id.is_empty(),
        "title" => node.title.is_empty(),
        "kind" => node.kind.as_str().is_empty(),
        "status" => node.status.as_str().is_empty(),
        "created" => node.created.is_none(),
        "updated" => node.updated.is_none(),
        "reviewed" => node.reviewed.is_none(),
        "owner" => node.owner.is_none(),
        "superseded_by" => node.superseded_by.is_none(),
        "supersedes" => node.supersedes.is_empty(),
        "implements" => node.implements.is_empty(),
        "related" => node.related.is_empty(),
        "tags" => node.tags.is_empty(),
        other => match node.attrs.get(other) {
            None | Some(Value::Null) => true,
            Some(Value::String(s)) => s.is_empty(),
            Some(Value::Array(a)) => a.is_empty(),
            _ => false,
        },
    }
}

/// Read a field's value as a `String` for enum comparison. Returns
/// `None` when the field is absent or cannot be represented as a scalar
/// string (arrays, objects, etc. are not enum candidates).
fn read_field_as_string(node: &Node, field: &str) -> Option<String> {
    match field {
        "id" => none_if_empty(&node.id),
        "title" => none_if_empty(&node.title),
        "kind" => none_if_empty(node.kind.as_str()),
        "status" => none_if_empty(node.status.as_str()),
        "owner" => node.owner.clone(),
        "superseded_by" => node.superseded_by.clone(),
        // Date-valued built-ins were previously missing here. Without
        // them, `cross_field.when = "reviewed=2026-01-01"` validated
        // at load (the field is recognised as built-in) but never
        // fired at runtime — `read_field_as_string` returned `None`
        // so the predicate always saw "field absent". Format as the
        // canonical YAML date string so equality comparisons against
        // user-written predicate values round-trip.
        "created" => node.created.map(|d| d.format("%Y-%m-%d").to_string()),
        "updated" => node.updated.map(|d| d.format("%Y-%m-%d").to_string()),
        "reviewed" => node.reviewed.map(|d| d.format("%Y-%m-%d").to_string()),
        other => match node.attrs.get(other)? {
            Value::String(s) if !s.is_empty() => Some(s.clone()),
            Value::Number(n) => Some(n.to_string()),
            Value::Bool(b) => Some(b.to_string()),
            _ => None,
        },
    }
}

fn none_if_empty(s: &str) -> Option<String> {
    if s.is_empty() {
        None
    } else {
        Some(s.to_string())
    }
}

/// Validate a JSON value against an expected field type. Returns a
/// human-readable error message on mismatch, `None` on success.
///
/// Written as `match expected { Variant => match value { ... } }` so
/// that adding a new `FieldType` variant is a compile error here —
/// silent acceptance of unknown types would defeat the validation.
fn validate_type(value: &Value, expected: FieldType) -> Option<String> {
    match expected {
        FieldType::String => match value {
            Value::String(_) => None,
            other => Some(format!("expected string, got {}", describe_value(other))),
        },
        FieldType::Integer => match value {
            Value::Number(n) if n.is_i64() || n.is_u64() => None,
            other => Some(format!("expected integer, got {}", describe_value(other))),
        },
        FieldType::Bool => match value {
            Value::Bool(_) => None,
            other => Some(format!("expected bool, got {}", describe_value(other))),
        },
        FieldType::Date => match value {
            Value::String(s) => NaiveDate::parse_from_str(s, "%Y-%m-%d")
                .ok()
                .map(|_| None)
                .unwrap_or_else(|| Some(format!("invalid date {s:?}, expected YYYY-MM-DD"))),
            other => Some(format!(
                "expected date (YYYY-MM-DD), got {}",
                describe_value(other)
            )),
        },
    }
}

fn describe_value(v: &Value) -> &'static str {
    match v {
        Value::Null => "null",
        Value::Bool(_) => "bool",
        Value::Number(n) if n.is_i64() || n.is_u64() => "integer",
        Value::Number(_) => "float",
        Value::String(_) => "string",
        Value::Array(_) => "array",
        Value::Object(_) => "object",
    }
}

/// Evaluate whether a `when` predicate holds for a given node.
///
/// Public so scaffold can evaluate cross_field predicates against a
/// synthetic default node without reimplementing the predicate logic.
pub fn predicate_matches_node(predicate: &WhenPredicate, node: &Node) -> bool {
    match predicate {
        WhenPredicate::Equals { field, value } => read_field_as_string(node, field)
            .as_deref()
            .map(|actual| actual == value)
            .unwrap_or(false),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{
        CrossFieldSpec, FieldType, KindsConfig, SchemaConfig, SchemaOverride, StatusesConfig,
    };
    use crate::model::{Kind, Status};
    use std::collections::BTreeMap;
    use std::path::PathBuf;

    fn test_config() -> Config {
        Config {
            kinds: KindsConfig {
                allowed: vec!["adr".to_string(), "guide".to_string()],
            },
            statuses: StatusesConfig {
                allowed: vec![
                    "draft".to_string(),
                    "active".to_string(),
                    "superseded".to_string(),
                ],
                terminal: vec!["superseded".to_string()],
            },
            schema: SchemaConfig {
                required: vec!["id".to_string(), "title".to_string()],
                overrides: vec![SchemaOverride {
                    kinds: vec!["adr".to_string()],
                    required: vec!["id".to_string(), "title".to_string(), "status".to_string()],
                    types: [("decision_date".to_string(), FieldType::Date)]
                        .into_iter()
                        .collect(),
                    enums: [(
                        "status".to_string(),
                        vec![
                            "draft".to_string(),
                            "active".to_string(),
                            "superseded".to_string(),
                        ],
                    )]
                    .into_iter()
                    .collect(),
                    cross_field: vec![CrossFieldSpec {
                        when: "status=superseded".to_string(),
                        require: "superseded_by".to_string(),
                    }],
                }],
                ..Default::default()
            },
            ..Config::default()
        }
    }

    fn make_node(id: &str, kind: &str, status: &str) -> Node {
        Node {
            id: id.to_string(),
            path: PathBuf::from(format!("{id}.md")),
            title: id.to_string(),
            kind: Kind::new(kind),
            status: Status::new(status),
            created: None,
            updated: None,
            reviewed: None,
            owner: None,
            supersedes: vec![],
            superseded_by: None,
            implements: vec![],
            related: vec![],
            tags: vec![],
            orphan_ok: false,
            attrs: BTreeMap::new(),
        }
    }

    fn make_graph(nodes: Vec<Node>) -> Graph {
        use indexmap::IndexMap;
        let mut map = IndexMap::new();
        for n in nodes {
            map.insert(n.id.clone(), n);
        }
        Graph::new(map, vec![])
    }

    #[test]
    fn field_types_accepts_valid_date() {
        let mut node = make_node("adr-1", "adr", "active");
        node.attrs.insert(
            "decision_date".to_string(),
            Value::String("2026-04-19".to_string()),
        );
        let graph = make_graph(vec![node]);
        let v = FieldTypeRule.check(&graph, &test_config());
        assert!(v.is_empty());
    }

    #[test]
    fn field_types_rejects_invalid_date() {
        let mut node = make_node("adr-1", "adr", "active");
        node.attrs.insert(
            "decision_date".to_string(),
            Value::String("yesterday".to_string()),
        );
        let graph = make_graph(vec![node]);
        let v = FieldTypeRule.check(&graph, &test_config());
        assert_eq!(v.len(), 1);
        assert_eq!(v[0].rule_id, "field_type");
    }

    #[test]
    fn field_types_skip_missing_field() {
        let node = make_node("adr-1", "adr", "active");
        let graph = make_graph(vec![node]);
        let v = FieldTypeRule.check(&graph, &test_config());
        assert!(v.is_empty()); // required_field handles missing
    }

    #[test]
    fn field_enums_rejects_typo() {
        let node = make_node("adr-1", "adr", "actives");
        let graph = make_graph(vec![node]);
        let v = FieldEnumRule.check(&graph, &test_config());
        assert_eq!(v.len(), 1);
        assert_eq!(v[0].rule_id, "field_enum");
    }

    #[test]
    fn field_enums_accepts_valid() {
        let node = make_node("adr-1", "adr", "active");
        let graph = make_graph(vec![node]);
        let v = FieldEnumRule.check(&graph, &test_config());
        assert!(v.is_empty());
    }

    #[test]
    fn field_enums_fall_back_to_global_allowed() {
        // A "guide" doc has no per-kind enum override, but the global
        // `statuses.allowed` still constrains its `status` field —
        // declaring an allowed list has to mean "these and only these,
        // everywhere," otherwise the list is a lie.
        let node = make_node("guide-1", "guide", "actives");
        let graph = make_graph(vec![node]);
        let v = FieldEnumRule.check(&graph, &test_config());
        assert_eq!(v.len(), 1);
        assert_eq!(v[0].rule_id, "field_enum");
        assert!(v[0].message.contains("\"actives\""));
    }

    #[test]
    fn field_enums_rejects_unknown_kind() {
        // Symmetric to the status check: a kind value outside
        // `kinds.allowed` is flagged even when no explicit enum
        // override on `kind` was declared.
        let node = make_node("x-1", "unlisted-kind", "active");
        let graph = make_graph(vec![node]);
        let v = FieldEnumRule.check(&graph, &test_config());
        assert!(v.iter().any(|v| v.message.contains("\"unlisted-kind\"")));
    }

    #[test]
    fn cross_field_fires_when_predicate_matches() {
        let node = make_node("adr-1", "adr", "superseded");
        // missing superseded_by
        let graph = make_graph(vec![node]);
        let v = CrossFieldRule.check(&graph, &test_config());
        assert_eq!(v.len(), 1);
        assert!(v[0].message.contains("superseded_by"));
    }

    #[test]
    fn cross_field_silent_when_predicate_false() {
        let node = make_node("adr-1", "adr", "draft");
        let graph = make_graph(vec![node]);
        let v = CrossFieldRule.check(&graph, &test_config());
        assert!(v.is_empty());
    }

    #[test]
    fn cross_field_fires_on_date_valued_builtin_predicate() {
        // Regression: `read_field_as_string` used to return `None` for
        // every date-valued built-in (`created`, `updated`,
        // `reviewed`), so predicates like `when = "reviewed=2026-01-01"`
        // validated at config load but silently never fired at
        // runtime. Now the predicate matches and the cross_field rule
        // flags the missing `require` field.
        use chrono::NaiveDate;
        let mut config = test_config();
        config.schema.overrides[0].cross_field = vec![CrossFieldSpec {
            when: "reviewed=2026-01-01".to_string(),
            require: "owner".to_string(),
        }];
        let mut node = make_node("adr-1", "adr", "active");
        node.reviewed = Some(NaiveDate::from_ymd_opt(2026, 1, 1).unwrap());
        // missing owner
        let graph = make_graph(vec![node]);
        let v = CrossFieldRule.check(&graph, &config);
        assert_eq!(v.len(), 1, "expected one violation, got: {v:?}");
        assert!(v[0].message.contains("owner"));
    }

    #[test]
    fn cross_field_silent_when_required_field_present() {
        let mut node = make_node("adr-1", "adr", "superseded");
        node.superseded_by = Some("adr-2".to_string());
        let graph = make_graph(vec![node]);
        let v = CrossFieldRule.check(&graph, &test_config());
        assert!(v.is_empty());
    }

    #[test]
    fn type_and_cross_field_rules_early_return_on_empty_override() {
        // `FieldTypeRule` and `CrossFieldRule` are purely config-driven
        // — no declared constraints, no violations. `FieldEnumRule` is
        // now stricter: even with no override, `kind` and `status` are
        // validated against the global allowed lists, so it is no
        // longer part of this "no constraints configured" test.
        let mut config = test_config();
        config.schema.overrides[0].types.clear();
        config.schema.overrides[0].enums.clear();
        config.schema.overrides[0].cross_field.clear();
        // Use a valid status so the global-backstop enum check stays silent.
        let node = make_node("adr-1", "adr", "active");
        let graph = make_graph(vec![node]);
        assert!(FieldTypeRule.check(&graph, &config).is_empty());
        assert!(CrossFieldRule.check(&graph, &config).is_empty());
    }
}