txtx_core/validation/
validator.rs1use 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
9pub struct ValidatorConfig {
11 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 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
39pub 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 let mut validator = BasicHclValidator::new(&mut result, file_path, source);
51 validator.validate(body);
52 } else {
53 let mut validator = FullHclValidator::new(&mut result, file_path, source, config.addon_specs);
55 validator.validate(body);
56 }
57
58 result
59}