Skip to main content

txtx_core/validation/
validator.rs

1//! High-level validation API for runbook files
2
3use super::hcl_validator::{BasicHclValidator, FullHclValidator};
4use super::types::ValidationResult;
5use crate::kit::hcl::structure::Body;
6use crate::kit::types::commands::{CommandSpecification, PreCommandSpecification};
7use std::collections::HashMap;
8
9/// Configuration for the validator
10pub struct ValidatorConfig {
11    /// Addon specifications for validation
12    pub addon_specs: HashMap<String, Vec<(String, CommandSpecification)>>,
13}
14
15impl ValidatorConfig {
16    pub fn new() -> Self {
17        Self { addon_specs: HashMap::new() }
18    }
19
20    /// Add specifications from an addon
21    pub fn add_addon_specs(&mut self, namespace: String, specs: Vec<PreCommandSpecification>) {
22        let actions = specs
23            .into_iter()
24            .filter_map(|a| match a {
25                PreCommandSpecification::Atomic(spec) => Some((spec.matcher.clone(), spec)),
26                _ => None,
27            })
28            .collect();
29        self.addon_specs.insert(namespace, actions);
30    }
31}
32
33impl Default for ValidatorConfig {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39/// Validate a runbook file
40pub fn validate_runbook(
41    file_path: &str,
42    source: &str,
43    body: &Body,
44    config: ValidatorConfig,
45) -> ValidationResult {
46    let mut result = ValidationResult::new();
47
48    if config.addon_specs.is_empty() {
49        // Use basic validator when no addon specs are available
50        let mut validator = BasicHclValidator::new(&mut result, file_path, source);
51        validator.validate(body);
52    } else {
53        // Use full validator when addon specs are provided
54        let mut validator = FullHclValidator::new(&mut result, file_path, source, config.addon_specs);
55        validator.validate(body);
56    }
57
58    result
59}