Skip to main content

a2ui_base/validate/
config.rs

1//! Validation configuration + STRICT/RELAXED presets.
2//!
3//! Ports `ValidationConfig` from Python `validator.py`. The catalog-schema
4//! portions of the Python validator are NOT ported (Rust has no runtime catalog
5//! schema); only the three tolerance flags that govern integrity/topology
6//! behavior are kept.
7
8/// Tolerance flags for component validation. Defaults (= STRICT) reject
9/// orphans, dangling references, and a missing root.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub struct ValidationConfig {
12    /// If true, components unreachable from root are allowed (incremental
13    /// updates, partial trees).
14    pub allow_orphan_components: bool,
15    /// If true, references to component IDs not present in this batch are
16    /// allowed (the referenced component may already live on the client).
17    pub allow_dangling_references: bool,
18    /// If true, a missing `root` component is allowed (incremental update
19    /// without a createSurface).
20    pub allow_missing_root: bool,
21}
22
23impl ValidationConfig {
24    /// Reject everything: orphans, dangling refs, missing root. The default.
25    pub const STRICT: Self = Self {
26        allow_orphan_components: false,
27        allow_dangling_references: false,
28        allow_missing_root: false,
29    };
30
31    /// Allow everything: useful for lenient / best-effort loading.
32    pub const RELAXED: Self = Self {
33        allow_orphan_components: true,
34        allow_dangling_references: true,
35        allow_missing_root: true,
36    };
37}
38
39/// Convenience alias matching the Python constant name.
40pub const STRICT_VALIDATION: ValidationConfig = ValidationConfig::STRICT;
41/// Convenience alias matching the Python constant name.
42pub const RELAXED_VALIDATION: ValidationConfig = ValidationConfig::RELAXED;
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn strict_rejects_everything() {
50        assert!(!STRICT_VALIDATION.allow_orphan_components);
51        assert!(!STRICT_VALIDATION.allow_dangling_references);
52        assert!(!STRICT_VALIDATION.allow_missing_root);
53        assert_eq!(STRICT_VALIDATION, ValidationConfig::STRICT);
54    }
55
56    #[test]
57    fn relaxed_allows_everything() {
58        assert!(RELAXED_VALIDATION.allow_orphan_components);
59        assert!(RELAXED_VALIDATION.allow_dangling_references);
60        assert!(RELAXED_VALIDATION.allow_missing_root);
61        assert_eq!(RELAXED_VALIDATION, ValidationConfig::RELAXED);
62    }
63
64    #[test]
65    fn default_is_strict() {
66        assert_eq!(ValidationConfig::default(), ValidationConfig::STRICT);
67    }
68}