voce_validator/passes/
forms.rs1use std::collections::HashSet;
6
7use crate::errors::{Diagnostic, Severity, ValidationResult};
8use crate::index::NodeIndex;
9use crate::ir::{ChildNode, VoceIr};
10use crate::passes::ValidationPass;
11
12pub struct FormsPass;
13
14impl ValidationPass for FormsPass {
15 fn name(&self) -> &'static str {
16 "forms"
17 }
18
19 fn run(&self, ir: &VoceIr, _index: &NodeIndex, result: &mut ValidationResult) {
20 let root = match &ir.root {
21 Some(r) => r,
22 None => return,
23 };
24
25 if let Some(ref children) = root.children {
26 self.check_children(children, "/root/children", result);
27 }
28 }
29}
30
31impl FormsPass {
32 fn check_children(
33 &self,
34 children: &[ChildNode],
35 parent_path: &str,
36 result: &mut ValidationResult,
37 ) {
38 for (i, child) in children.iter().enumerate() {
39 let path = format!("{parent_path}/{i}");
40
41 if child.type_name() == "FormNode" {
42 if let Some(form) = child.as_type::<crate::ir::FormNode>() {
43 self.check_form(&form, &path, result);
44 }
45 }
46
47 if let Some(grandchildren) = child.children() {
48 self.check_children(&grandchildren, &format!("{path}/children"), result);
49 }
50 }
51 }
52
53 fn check_form(&self, form: &crate::ir::FormNode, path: &str, result: &mut ValidationResult) {
54 let fields = form.fields.as_deref().unwrap_or(&[]);
55
56 if fields.is_empty() {
58 result.diagnostics.push(Diagnostic {
59 severity: Severity::Error,
60 code: "FRM001".to_string(),
61 message: "FormNode must have at least one field".to_string(),
62 node_path: path.to_string(),
63 pass: self.name().to_string(),
64 hint: None,
65 });
66 }
67
68 if form.submission.is_none() {
70 result.diagnostics.push(Diagnostic {
71 severity: Severity::Error,
72 code: "FRM002".to_string(),
73 message: "FormNode must have a submission configuration".to_string(),
74 node_path: path.to_string(),
75 pass: self.name().to_string(),
76 hint: None,
77 });
78 }
79
80 let mut seen_names: HashSet<String> = HashSet::new();
82 for (j, field) in fields.iter().enumerate() {
83 if let Some(ref name) = field.name {
84 if !seen_names.insert(name.clone()) {
85 result.diagnostics.push(Diagnostic {
86 severity: Severity::Error,
87 code: "FRM003".to_string(),
88 message: format!("Duplicate field name \"{name}\" in form"),
89 node_path: format!("{path}/fields/{j}"),
90 pass: self.name().to_string(),
91 hint: None,
92 });
93 }
94 }
95
96 if field.label.as_ref().is_none_or(|l| l.is_empty()) {
98 result.diagnostics.push(Diagnostic {
99 severity: Severity::Error,
100 code: "FRM009".to_string(),
101 message: "FormField must have a label for accessibility".to_string(),
102 node_path: format!("{path}/fields/{j}"),
103 pass: self.name().to_string(),
104 hint: None,
105 });
106 }
107
108 if field.field_type.as_deref() == Some("Email") {
110 let has_email_rule = field.validations.as_ref().is_some_and(|rules| {
111 rules
112 .iter()
113 .any(|r| r.rule_type.as_deref() == Some("Email"))
114 });
115 if !has_email_rule {
116 result.diagnostics.push(Diagnostic {
117 severity: Severity::Warning,
118 code: "FRM004".to_string(),
119 message: "Email field should have an Email validation rule".to_string(),
120 node_path: format!("{path}/fields/{j}"),
121 pass: self.name().to_string(),
122 hint: None,
123 });
124 }
125 }
126 }
127 }
128}